English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MATLAB Differentiation

MATLAB providesdiffThe command used to calculate symbolic derivatives. In its simplest form, the function to be differentiated is passed as a parameter to the diff command.

For example, let us calculate the derivative of the function f(t) = 3t 2 + 2t -2

Online Examples

Create a script file and enter the following code-

syms t
f = 3*t^2 + 2*t^(-2);
diff(f)

After compiling and executing the above code, the following results will be produced-

ans =
6*t - 4/t^3

The following is the equivalent of the above calculation in Octave-

pkg load symbolic
symbols
t = sym("t");
f = 3*t^2 + 2*t^(-2);
differentiate(f,t)

Octave executes the code and returns the following results-

ans =
   -(4.0)*t^(-3.0)+(6.0)*t

Verification of basic rules

Let us briefly explain various equations or rules used for differentiation and verify these rules. For this purpose, we will write f'(x) for the first derivative and f''(x) for the second derivative.

The following are differentiation rules-

Rule1

for any function f and g and any real numbers a and b are the derivatives of the function-

h(x) = af(x) + bg(x) with respect to x by-

h'(x) = af'(x) + bg'(x)

Rule Two

sumandsubtractionThe rule states that if f and g are two functions, f’ and g’ are their derivatives, then,

(f + g)' = f' + g'

(f - g)' = f' - g'

Rule Three

productThe rule states that if f and g are two functions, then f' and g' are their derivatives, then,

(f.g)' = f'.g + g'.f

Rule Four

quotientThe rule states that if f and g are two functions, then f' and g' are their derivatives, then,

(f/g)' = (f'.g - g'.f)/g2

Rule Five

polynomialor the basic power rule specifies that if, theny = f(x) = xnf' = n. x(n-1)

The direct result of this rule is that the derivative of any constant is zero, that is, ify = k, any constant, then

f' = 0

Rule Six

chainThe rule states that the derivative of a function of a function with respect to x is:h(x) = f(g(x))

h'(x) = f'(g(x)).g'(x)

Example

Create a script file and enter the following code-

syms x
syms t
f = (x + 2)*(x^2 + 3)
der1 = diff(f)
f = (t^2 + 3)*(sqrt(t) + t^3)
der2 = diff(f)
f = (x^2 - 2*x + 1)*(3*x^3 - 5*x^2 + 2)
der3 = diff(f)
f = (2*x^2 + 3*x)/(x^3 + 1)
der4 = diff(f)
f = (x^2 + 1))17
der5 = diff(f)
f = (t^3 + 3* t^2 + 5*t -9))^(-6)
der6 = diff(f)

When running the file, MATLAB displays the following result-

f =
   (x^2 + 3)*(x + 2)
 
   der1 =
   2*x*(x + 2) + x^2 + 3
  
f =
   (t^(1/2) + t^3)*(t^2 + 3)
 
   der2 =
   (t^2 + 3)*(3*t^2 + 1/(2*t^(1/2)) + 2*t*(t^(1/2) + t^3)
  
f =
   (x^2 - 2*x + 1)*(3*x^3 - 5*x^2 + 2)
  
der3 =
   (2*x - 2)*(3*x^3 - 5*x^2 + 2) - (- 9*x^2 + 10*x)*(x^2 - 2*x + 1)
 
f =
   (2*x^2 + 3*x)/(x^3 + 1)
  
der4 =
   (4*x + 3)/(x^3 + 1) - (3*x^2*(2*x^2 + 3*x))/(x^3 + 1))2
  
f =
   (x^2 + 1))17
  
der5 =
   34*x*(x^2 + 1))16
  
f =
   1/(t^3 + 3*t^2 + 5*t - 9))6
  
der6 =
   -(6*(3*t^2 + 6*t + 5))/(t^3 + 3*t^2 + 5*t - 9))7

The following is the equivalent of the above calculation in Octave-

pkg load symbolic
symbols
x = sym("x");
t = sym("t");
f = (x + 2)*(x^2 + 3) 
der1 = differentiate(f,x) 
f = (t^2 + 3)*(t^(1/2) + t^3) 
der2 = differentiate(f,t) 
f = (x^2 - 2*x + 1)*(3*x^3 - 5*x^2 + 2) 
der3 = differentiate(f,x) 
f = (2*x^2 + 3*x)/(x^3 + 1) 
der4 = differentiate(f,x) 
f = (x^2 + 1))17 
der5 = differentiate(f,x) 
f = (t^3 + 3* t^2 + 5*t -9))^(-6) 
der6 = differentiate(f,t)

Octave executes the code and returns the following results-

f =
(2.0+x)*(3.0+x^(2.0))
der1 =
3.0+x^(2.0)+(2.0)*(2.0+x)*x
f =
(t^(3.0)+sqrt(t))*(3.0+t^(2.0))
der2 =
(2.0)*(t^(3.0)+sqrt(t))*t+((3.0)*t^(2.0)+(0.5)*t^(-0.5))*(3.0+t^(2.0))
f =
(1.0+x^(2.0)-(2.0)*x)*(2.0-(5.0)*x^(2.0)+(3.0)*x^(3.0))
der3 =
(-2.0+(2.0)*x)*(2.0-(5.0)*x^(2.0)+(3.0)*x^(3.0))+((9.0)*x^(2.0)-(10.0)*x)*(1.0+x^(2.0)-(2.0)*x)
f =
(1.0+x^(3.0)^(-1)*((2.0)*x^(2.0)+(3.0)*x)
der4 =
(1.0+x^(3.0)^(-1)*(3.0+(4.0)*x)-(3.0)*(1.0+x^(3.0)^(-2)*x^(2.0)*((2.0)*x^(2.0)+(3.0)*x)
f =
(1.0+x^(2.0)^(17.0)
der5 =
(34.0)*(1.0+x^(2.0)^(16.0)*x
f =
(-9.0+(3.0)*t^(2.0)+t^(3.0)+(5.0)*t)^(-6.0)
der6 =
-(6.0)*(-9.0+(3.0)*t^(2.0)+t^(3.0)+(5.0)*t)^(-7.0)*(5.0+(3.0)*t^(2.0)+(6.0)*t)

Derivatives of exponential, logarithmic, and trigonometric functions

The table below provides derivatives of commonly used exponential, logarithmic, and trigonometric functions-

Function
Derivative
ca.xca.x.lnc.a(ln is the natural logarithm)
exex
ln x1/x
lncx1/x.ln c
xxxx.(1 + ln x)
sin(x)cos(x)
cos(x)-sin(x)
tan(x)sec2(x), or  1/cos2(x),  or  1 + tan2(x)
cot(x)-csc2(x), or  -1/sin2(x), or -(1 + cot2(x))
sec(x)sec(x).tan(x)
csc(x)-csc(x).cot(x)

Example

Create a script file and enter the following code-

syms x
y = exp(x)
diff(y)
y = x^9
diff(y)
y = sin(x)
diff(y)
y = tan(x)
diff(y)
y = cos(x)
diff(y)
y = log(x)
diff(y)
y = log10(x)
diff(y)
y = sin(x)^2
diff(y)
y = cos(3*x^2 + 2*x + 1)
diff(y)
y = exp(x)/sin(x)
diff(y)

When running the file, MATLAB displays the following result-

y =
   exp(x)
   ans =
   exp(x)
y =
   x^9
   ans =
   9*x^8
  
y =
   sin(x)
   ans =
   cos(x)
  
y =
   tan(x)
   ans =
   tan(x)^2 + 1
 
y =
   cos(x)
   ans =
   -sin(x)
  
y =
   log(x)
   ans =
   1/x
  
y =
   log(x)/log(10)
   ans =
   1/(x*log(10))
 
y =
   sin(x)^2
   ans =
   2*cos(x)*sin(x)
 
y =
   cos(3*x^2 + 2*x + 1)
   ans =
   -sin(3*x^2 + 2*x + 1)*(6*x + 2)
  
y =
   exp(x)/sin(x)
   ans =
   exp(x)/sin(x) - (exp(x)*cos(x))/sin(x)^2

The following is the equivalent of the above calculation in Octave-

pkg load symbolic
symbols
x = sym("x");
y = Exp(x)
differentiate(y,x)
y = x^9
differentiate(y,x)
y = Sin(x)
differentiate(y,x)
y = Tan(x)
differentiate(y,x)
y = Cos(x)
differentiate(y,x)
y = Log(x)
differentiate(y,x)
% symbolic package does not support this feature
%y = Log10(x)
%differentiate(y,x)
y = Sin(x)^2
differentiate(y,x)
y = Cos(3*x^2 + 2*x + 1)
differentiate(y,x)
y = Exp(x)/Sin(x)
differentiate(y,x)

Octave executes the code and returns the following results-

y =
exp(x)
ans =
exp(x)
y =
x^(9.0)
ans =
(9.0)*x^(8.0)
y =
sin(x)
ans =
cos(x)
y =
tan(x)
ans =
1+tan(x)^2
y =
cos(x)
ans =
-sin(x)
y =
log(x)
ans =
x^(-1)
y =
sin(x)^(2.0)
ans =
(2.0)*sin(x)*cos(x)
y =
cos(1.0+(2.0)*x+(3.0)*x^(2.0))
ans =
-(2.0+(6.0)*x)*sin(1.0+(2.0)*x+(3.0)*x^(2.0))
y =
sin(x)^(-1)*exp(x)
ans =
sin(x)^(-1)*exp(x)-sin(x)^(-2)*cos(x)*exp(x)

Calculate higher-order derivatives

To calculate the higher-order derivatives of the function f, we use the syntaxdiff(f,n)

Let's calculate the second derivative of the function y = f(x) = x^2. -3x

f = x*exp(-3*x);
diff(f, 2)

MATLAB executes the code and returns the following result-

ans =
9*x*exp(-3*x) - 6*exp(-3*x)

The following is the equivalent of the above calculation in Octave-

pkg load symbolic
symbols
x = sym("x");
f = x*Exp(-3*x);
differentiate(f, x, 2)

Octave executes the code and returns the following results-

ans =
(9.0)*exp(-(3.0)*x)*x-(6.0)*exp(-(3.0)*x)

Example

In this example, let's solve a problem. Given a function. We will have to find out if the equation holds.y = f(x) = 3 sin(x) + 7 cos(5x)f" + f = -5cos(2x)

Create a script file and enter the following code-

syms x
y = 3*sin(x)+7*cos(5*x);  % defining the function
lhs = diff(y,2)+y;        % evaluating the lhs of the equation
rhs = -5*cos(2*x);        % rhs of the equation
if(isequal(lhs,rhs))
   disp('Yes, the equation holds true');
else
   disp('No, the equation does not hold true');
end
disp('Value of LHS is: '), disp(lhs);

When running the file, it displays the following result-

No, the equation does not hold true
Value of LHS is: 
-168*cos(5*x)

The following is the equivalent of the above calculation in Octave-

pkg load symbolic
symbols
x = sym("x");
y = 3*Sin(x)+7*Cos(5*x);           % defining the function
lhs = differentiate(y, x, 2) + y;  % calculating the equation lhs
rhs = -5*Cos(2*x);                 % equation rhs
if(lhs == rhs)
   disp('Yes, the equation holds true');
else
   disp('No, the equation does not hold true');
end
disp('Value of LHS is: '), disp(lhs);

Octave executes the code and returns the following results-

No, the equation does not hold true
Value of LHS is: 
-(168.0)*cos((5.0)*x)

Find the maximum and minimum values of the curve

If you want to search for local maxima and minima in the graph, it is basically to find the highest or lowest points within a specific range of values of the function graph at specific positions or symbolic variables.

For the function y = f(x), points on the graph with zero slope are calledstationary points(stationary points/Critical points). In other words, fixed points are f'(x) = 0.

To find the stationary points of the function we are differentiating, we need to set the derivative to zero and solve the equation.

Example

Let's find the fixed point f(x) = 2x 3 + 3x 2 − 12x + 17

Take the following steps-

First, let's enter the function and draw its graph.

syms x
y = 2*x^3 + 3*x^2 - 12*x + 17; %define function
ezplot(y)

MATLAB executes the code and returns the following chart-

This is the Octave equivalent code for the above example-

pkg load symbolic
symbols
x = sym('x');
y = inline("2*x^3 + 3*x^2 - 12*x + 17);
ezplot(y)
print -deps graph.eps

Our goal is to find some local maximum and minimum values on the graph, so let's find the interval [-2,2] to find the local maximum and minimum values.

syms x
y = 2*x^3 + 3*x^2 - 12*x + 17; % defining the function
ezplot(y, [-2, 2])

MATLAB executes the code and returns the following chart-

This is the Octave equivalent code for the above example-

pkg load symbolic
symbols
x = sym('x');
y = inline("2*x^3 + 3*x^2 - 12*x + 17);
ezplot(y, [-2, 2])
print -deps graph.eps

Next, let's calculate the derivative.

g = diff(y)

MATLAB executes the code and returns the following result-

g =
   6*x^2 + 6*x - 12

This is the octave equivalent of the above calculation-

pkg load symbolic
symbols
x = sym("x");
y = 2*x^3 + 3*x^2 - 12*x + 17;
g = differentiate(y, x)

Octave executes the code and returns the following results-

g =
   -12.0+(6.0)*x+(6.0)*x^(2.0)

Let's solve the derivative function g to get the values where it becomes zero.

s = solve(g)

MATLAB executes the code and returns the following result-

s =
   1
   -2

The following is the equivalent of the above calculation in Octave-

pkg load symbolic
symbols
x = sym("x");
y = 2*x^3 + 3*x^2 - 12*x + 17;
g = differentiate(y, x)
roots([6, 6, -12])

Octave executes the code and returns the following results-

g =
-12.0+(6.0)*x^(2.0)+(6.0)*x
ans =
  -2
   1

This is consistent with our chart, so let's find the critical point x = 1,-2to calculate the function f.

subs(y, 1), subs(y, -2)

MATLAB executes the code and returns the following result-

ans =
   10
ans =
   37

The following is the equivalent of the above calculation in Octave-

pkg load symbolic
symbols
x = sym("x");
y = 2*x^3 + 3*x^2 - 12*x + 17;
g = differentiate(y, x)
roots([6, 6, -12])
subs(y, x, 1), subs(y, x, -2)
ans =
   10.0
ans =
   37.0-4.6734207789940138748E-18*I

Therefore, the minimum and maximum values of the function f(x) are = 2x 3 + 3x 2 − 12x + 17,in[-2,2]interval as10and37。

solving differential equations

MATLAB providesdsolveThe command used for symbolic solution of differential equations.

dsolveThe most basic form of the command to find the solution of a single equation is

dsolve('eqn')

whereeqnis the text string used to input the equation.

It returns a symbolic solution with a set of arbitrary constants, marked as C by MATLAB1,C2etc.

You can also specify the initial and boundary conditions of the problem as a comma-separated list after the equation-

dsolve('eqn','cond1', 'cond2',…)

For the purpose of using the dsolve command, derivatives are represented by D. For example, like f'(t)= -2 * f +Such equations as cost(t) are entered as-

'Df = -2*f + cos(t)'

Higher-order derivatives are represented by the order of derivatives after D.

For example, the equation f“(x)+ 2f'(x)= 5sin3x should be entered as-

'D2y + 2Dy = 5*sin(3*x)'

Let's take an example of a simple first-order differential equation: y'= 5y。

s = dsolve('Dy = 5*y')

MATLAB executes the code and returns the following result-

s =
   C2*exp(5*t)

Let's take another example of a second-order differential equation: y“-y = 0,y= -1,y'= 2。

dsolve('D2y - y = 0','y(0) = -1','Dy(0) = 2)

MATLAB executes the code and returns the following result-

ans =
   exp(t)/2 - (3*exp(-t))/2