MATLAB
Tutor
Part 8:
Differentiation
MATLAB can find the derivative of symbolic expressions. Higher derivatives can
also be found easily. These derivatives can be evaluated for arbitary values of the
independent variable.
- First we declare x to be
a symbolic variable and define g to be the function given by
g(x) = x2 cos x. Enter:
syms x
g = x^2*cos(x)
-
Now enter:
diff(g, x)
Then enter:
diff(g, x, 2)
How would you calculate the third derivative?
- If you want to
calculate the derivative of an expression that you have not yet
entered, just replace g by the expression. For example,
enter:
diff(x^3-x^2+2, x)
Now insert a symbolic constant a into the expression. Enter:
syms a
diff(x^3 - a*x^2 + 2, x)
Then change the final x to an a. That is, enter:
diff(x^3 - a*x^2 + 2, a)
What is the role of the symbol after the comma in the
differentiation expression?
- Now, suppose you
want to evaluate the derivative dg/dx at x = 2.1. Enter:
gp=diff(g, x)
Then enter:
subs(gp, x, 2.1)
Alternatively, you could enter:
subs( diff(g, x), x , 2.1 )
-
Check your understanding so far by using MATLAB
to calculate the second derivative of
tan(x6 - 3x + 5) at x = 3/2.
(The value is approximately - 4521.)