problems. For the syntax for symbolic differentiation, see
>> help sym/diff
. For example, tofind diff_f =
where f = e
sin(cx) and a, b and c are unspecified constants, we do thefollowing
>> clear, syms a b c x; f=exp(-a*x)*x^(3*b)*sin(c*x), diff_f = diff(f,x)
Notice that the result is quite complex. To ask MATLAB to try to simplify it, type:
>> diff_f_simp = simple(diff_f)
). MATLAB uses several algorithms for algebraic simplification, andchooses as a final answer the shortest of the results. If you prefer some other form listed, selectthe command producing it.MATLAB can take higher order differentials. For example, to find diff2_f =
using the already defined expression for f.
: For the syntax for symbolic integration, see
>> help sym/int
. Thus, forexample, to find the indefinite integral int_g =
, where g = e
sin(cx), we do
>> clear, syms a c x; g=exp(-a*x)*sin(c*x)
int_g = int(g,x)
Check this by taking
. This should give back g, but it doesn’t look thesame because MATLAB doesn’t always give things in the simplest format. In this case, weagain use the simple command
.Note that MATLAB does not add the constant of integration (“C”) customarily shown forindefinite integrals -- this you must do by adding C after the int command.Sometimes MATLAB is not able to integrate an expression symbolically. For example, try tofind
(where, as before, f = e
sin(cx) and a, b and c are unspecified constants).
>> clear, syms a b c x; f = exp(-a*x)*x^(3*b)*sin(c*x), int(f,x)
Notice that when MATLAB cannot find a solution, it gives a warning message and repeats thecommand. When this happens, you’re out of luck.
: For the definite integral, int_def_g =
>> clear, syms a c x; g = exp(-a*x)*sin(c*x)
int_def_g = int(g,x,-pi,pi)
When MATLAB is unable to find an analytical (symbolic) integral, such as with>>
, a numerical solution can nevertheless be found using the “quad”command, e.g.
. Generally speaking, it is best to follow the steps below:
It is good practice to assign a name to each answer, rather than letting MATLAB assign the default“ans,” which changes every time you find a new answer. Having a name for each particular answerenables you to recall it for use elsewhere. Note that “diff_f” is just a name, with no special significance.It’s also good practice to use names that give some indication of what they are, but you could just as welluse, for example, X and Y or A and B.