2
Most read
http://www.tutorialspoint.com/matlab/matlab_differential.htm Copyright © tutorialspoint.com
MATLAB - DIFFERENTIAL
MATLAB provides the diff command for computing symbolic derivatives. Inits simplest form, youpass the
functionyouwant to differentiate to diff command as anargument.
For example, let us compute the derivative of the functionf(t) = 3t2 + 2t-2
Example
Create a script file and type the following code into it:
syms t
f = 3*t^2 + 2*t^(-2);
diff(f)
Whenthe above code is compiled and executed, it produces the following result:
ans =
6*t - 4/t^3
Following is Octave equivalent of the above calculation:
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 result:
ans =
-(4.0)*t^(-3.0)+(6.0)*t
Verification of Elementary Rules of Differentiation
Let us briefly state various equations or rules for differentiationof functions and verify these rules. For this
purpose, we willwrite f'(x) for a first order derivative and f"(x) for a second order derivative.
Following are the rules for differentiation:
Rule 1
For any functions f and g and any realnumbers a and b the derivative of the function:
h(x) = af(x) + bg(x) withrespect to x is givenby:
h'(x) = af'(x) + bg'(x)
Rule 2
The sumand subtraction rules state that if f and g are two functions, f' and g' are their derivatives
respectively, then,
(f + g)' = f' + g'
(f - g)' = f' - g'
Rule 3
The product rule states that if f and g are two functions, f' and g' are their derivatives respectively, then,
(f.g)' = f'.g + g'.f
Rule 4
The quotient rule states that if f and g are two functions, f' and g' are their derivatives respectively, then,
(f/g)' = (f'.g - g'.f)/g2
Rule 5
The polynomial or elementary power rule states that, if y = f(x) = xn, thenf' = n. x(n-1)
A direct outcome of this rule is derivative of any constant is zero, i.e., if y = k, any constant, then
f' = 0
Rule 6
The chain rule states that, The derivative of the functionof a functionh(x) = f(g(x)) withrespect to x is,
h'(x)= f'(g(x)).g'(x)
Example
Create a script file and type the following code into it:
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)
Whenyourunthe 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
Following is Octave equivalent of the above calculation:
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)
Derivatives of Exponential, Logarithmic and Trigonometric Functions
The following table provides the derivatives of commonly used exponential, logarithmic and trigonometric
functions:
Function Derivative
ca.x ca.x.lnc.a (lnis naturallogarithm)
ex ex
ln x 1/x
lncx 1/x.lnc
xx xx.(1 + lnx)
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 type the following code into it:
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)
Whenyourunthe 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
Following is Octave equivalent of the above calculation:
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 packages does not have this support
%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)
Computing Higher Order Derivatives
To compute higher derivatives of a functionf, we use the syntax diff(f,n).
Let us compute the second derivative of the functiony = f(x) = x .e-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)
Following is Octave equivalent of the above calculation:
pkg load symbolic
symbols
x = sym("x");
f = x*Exp(-3*x);
differentiate(f, x, 2)
Example
Inthis example, let us solve a problem. Giventhat a functiony = f(x) = 3 sin(x) + 7 cos(5x). We willhave to
find out whether the equationf" + f = -5cos(2x) holds true.
Create a script file and type the following code into it:
syms x
y = 3*sin(x)+7*cos(5*x); % defining the function
lhs = diff(y,2)+y; %evaluting 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);
Whenyourunthe file, it displays the following result:
No, the equation does not hold true
Value of LHS is:
-168*cos(5*x)
Following is Octave equivalent of the above calculation:
pkg load symbolic
symbols
x = sym("x");
y = 3*Sin(x)+7*Cos(5*x); % defining the function
lhs = differentiate(y, x, 2) + y; %evaluting the lhs of the equation
rhs = -5*Cos(2*x); %rhs of the equation
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);
Finding the Maxima and Minima of a Curve
If we are searching for the localmaxima and minima for a graph, we are basically looking for the highest or
lowest points onthe graphof the functionat a particular locality, or for a particular range of values of the symbolic
variable.
For a functiony = f(x) the points onthe graphwhere the graphhas zero slope are called stationary points. In
other words stationary points are where f'(x) = 0.
To find the stationary points of a functionwe differentiate, we need to set the derivative equalto zero and solve
the equation.
Example
Let us find the stationary points of the functionf(x) = 2x3 + 3x2 − 12x + 17
Take the following steps:
1. First let us enter the functionand plot its graph:
syms x
y = 2*x^3 + 3*x^2 - 12*x + 17; % defining the function
ezplot(y)
MATLAB executes the code and returns the following plot:
Here is 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
2. Our aimis to find some localmaxima and minima onthe graph, so let us find the localmaxima and minima for
the interval[-2, 2] onthe graph.
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 plot:
Here is 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
3. Next, let us compute the derivative
g = diff(y)
MATLAB executes the code and returns the following result:
g =
6*x^2 + 6*x - 12
Here is 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)
4. Let us 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
Following is 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)
roots([6, 6, -12])
5. This agrees withour plot. So let us evaluate the functionf at the criticalpoints x = 1, -2. We cansubstitute a
value ina symbolic functionby using the subs command.
subs(y, 1), subs(y, -2)
MATLAB executes the code and returns the following result:
ans =
10
ans =
37
Following is 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)
roots([6, 6, -12])
subs(y, x, 1), subs(y, x, -2)
Therefore, The minimumand maximumvalues onthe functionf(x) = 2x3 + 3x2 − 12x + 17, inthe interval[-
2,2] are 10 and 37.
Solving Differential Equations
MATLAB provides the dsolve command for solving differentialequations symbolically.
The most basic formof the dsolve command for finding the solutionto a single equationis :
dsolve('eqn')
where eqn is a text string used to enter the equation.
It returns a symbolic solutionwitha set of arbitrary constants that MATLAB labels C1, C2, and so on.
Youcanalso specify initialand boundary conditions for the problem, as comma-delimited list following the
equationas:
dsolve('eqn','cond1', 'cond2',…)
For the purpose of using dsolve command, derivatives are indicated with a D. For example, anequation
like f'(t) = -2*f + cost(t) is entered as:
'Df = -2*f + cos(t)'
Higher derivatives are indicated by following D by the order of the derivative.
For example the equationf"(x) + 2f'(x) = 5sin3x should be entered as:
'D2y + 2Dy = 5*sin(3*x)'
Let us take up a simple example of a first order differentialequation: y' = 5y.
s = dsolve('Dy = 5*y')
MATLAB executes the code and returns the following result:
s =
C2*exp(5*t)
Let us take up another example of a second order differentialequationas: y" - y = 0, y(0) = -1, y'(0) = 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

More Related Content

PPT
MATLAB ODE
PPTX
Complex Numbers
PDF
Chapter 2: Relations
PPT
Matrix and Determinants
PDF
Relation Hasse diagram
PPT
Lattice lecture Final DM Updated2.ppt
PPTX
Integration presentation
PPTX
Trapezoidal rule
MATLAB ODE
Complex Numbers
Chapter 2: Relations
Matrix and Determinants
Relation Hasse diagram
Lattice lecture Final DM Updated2.ppt
Integration presentation
Trapezoidal rule

What's hot (20)

PDF
Functions in discrete mathematics
PPTX
Polynomials and Curve Fitting in MATLAB
PDF
Partial Fraction
PPT
complex numbers
PPT
Relations
PPT
Application of integral calculus
PPTX
Maximal and minimal elements of poset.pptx
PDF
Lesson 11: Limits and Continuity
PPTX
Presentation on Numerical Integration
PPTX
Matrices
PPTX
the inverse of the matrix
PPT
Set theory
PPT
Newton divided difference interpolation
PPTX
GAUSS ELIMINATION METHOD
PPTX
Gauss jordan and Guass elimination method
PPT
Riemann sumsdefiniteintegrals
PDF
All pairs shortest path algorithm
PPTX
Numerical differentiation
PPT
MATLAB : Numerical Differention and Integration
PPTX
Greedy Algorithm - Knapsack Problem
Functions in discrete mathematics
Polynomials and Curve Fitting in MATLAB
Partial Fraction
complex numbers
Relations
Application of integral calculus
Maximal and minimal elements of poset.pptx
Lesson 11: Limits and Continuity
Presentation on Numerical Integration
Matrices
the inverse of the matrix
Set theory
Newton divided difference interpolation
GAUSS ELIMINATION METHOD
Gauss jordan and Guass elimination method
Riemann sumsdefiniteintegrals
All pairs shortest path algorithm
Numerical differentiation
MATLAB : Numerical Differention and Integration
Greedy Algorithm - Knapsack Problem
Ad

Viewers also liked (11)

PDF
Mathematical Model of Varicella Zoster Virus - Abbie Jakubovic
PDF
Disease spreading & control in temporal networks
PDF
Temporal Networks of Human Interaction
PDF
The SIR Model and the 2014 Ebola Virus Disease Outbreak in Guinea, Liberia an...
PDF
Åpningssesjon: Infectious disease modelling
PDF
Modelling malaria transmission dynamics in irrigated areas of Tana River Coun...
PPTX
Delay-Differential Equations. Tools for Epidemics Modelling
PPTX
Markov chain and SIR epidemic model (Greenwood model)
PPTX
Vaccines
PPT
Vaccination
PPTX
Vaccination ppt
Mathematical Model of Varicella Zoster Virus - Abbie Jakubovic
Disease spreading & control in temporal networks
Temporal Networks of Human Interaction
The SIR Model and the 2014 Ebola Virus Disease Outbreak in Guinea, Liberia an...
Åpningssesjon: Infectious disease modelling
Modelling malaria transmission dynamics in irrigated areas of Tana River Coun...
Delay-Differential Equations. Tools for Epidemics Modelling
Markov chain and SIR epidemic model (Greenwood model)
Vaccines
Vaccination
Vaccination ppt
Ad

Similar to Matlab differential (20)

PDF
Matlab tutorial 4
PPT
LECTURE 5-Function in Matlab how to use mathlab
PPTX
9_Symbolic Math in MATLAB for Engineers.pptx
PPT
Derivatives
PPTX
Chapter 1 MATLAB Fundamentals easy .pptx
PDF
Diff eq
PPTX
Continuity and Differentiability functions
PDF
Difrentiation 140930015134-phpapp01
PPTX
matlab presentation fro engninering students
PPTX
Lectue five
PDF
3. DERIVATIVE BY INCREMENT IN CALULUS 01
PPT
Introduction to MATLAB
PDF
PPTX
Lesson 6 differentials parametric-curvature
PDF
Advanced-Differentiation-Rules.pdf
PDF
Matlab functions
DOCX
A practical work of matlab
PPT
Week 3-Using functions Week 3-Using functions
PPTX
03 Chapter MATLAB finite precision arithmatic
DOCX
More instructions for the lab write-up1) You are not obli.docx
Matlab tutorial 4
LECTURE 5-Function in Matlab how to use mathlab
9_Symbolic Math in MATLAB for Engineers.pptx
Derivatives
Chapter 1 MATLAB Fundamentals easy .pptx
Diff eq
Continuity and Differentiability functions
Difrentiation 140930015134-phpapp01
matlab presentation fro engninering students
Lectue five
3. DERIVATIVE BY INCREMENT IN CALULUS 01
Introduction to MATLAB
Lesson 6 differentials parametric-curvature
Advanced-Differentiation-Rules.pdf
Matlab functions
A practical work of matlab
Week 3-Using functions Week 3-Using functions
03 Chapter MATLAB finite precision arithmatic
More instructions for the lab write-up1) You are not obli.docx

More from pramodkumar1804 (20)

PDF
Matlab syntax
PDF
Matlab strings
PDF
Matlab simulink
PDF
Matlab polynomials
PDF
Matlab plotting
PDF
Matlab overview 3
PDF
Matlab overview 2
PDF
Matlab overview
PDF
Matlab operators
PDF
Matlab variables
PDF
Matlab numbers
PDF
Matlab matrics
PDF
Matlab m files
PDF
Matlab loops 2
PDF
Matlab loops
PDF
Matlab integration
PDF
Matlab graphics
PDF
Matlab gnu octave
PDF
Matlab operators
PDF
Matlab decisions
Matlab syntax
Matlab strings
Matlab simulink
Matlab polynomials
Matlab plotting
Matlab overview 3
Matlab overview 2
Matlab overview
Matlab operators
Matlab variables
Matlab numbers
Matlab matrics
Matlab m files
Matlab loops 2
Matlab loops
Matlab integration
Matlab graphics
Matlab gnu octave
Matlab operators
Matlab decisions

Recently uploaded (20)

PDF
FAMILY PLANNING (preventative and social medicine pdf)
PPTX
Neurological complocations of systemic disease
PPTX
pharmaceutics-1unit-1-221214121936-550b56aa.pptx
PPTX
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
PDF
English 2nd semesteNotesh biology biopsy results from the other day and I jus...
PDF
faiz-khans about Radiotherapy Physics-02.pdf
PDF
GIÁO ÁN TIẾNG ANH 7 GLOBAL SUCCESS (CẢ NĂM) THEO CÔNG VĂN 5512 (2 CỘT) NĂM HỌ...
PPTX
PAIN PATHWAY & MANAGEMENT OF ACUTE AND CHRONIC PAIN SPEAKER: Dr. Rajasekhar ...
PDF
BSc-Zoology-02Sem-DrVijay-Comparative anatomy of vertebrates.pdf
PPSX
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
PPTX
Power Point PR B.Inggris 12 Ed. 2019.pptx
PPTX
Copy of ARAL Program Primer_071725(1).pptx
PPTX
GW4 BioMed Candidate Support Webinar 2025
PDF
IS1343_2012...........................pdf
PDF
Review of Related Literature & Studies.pdf
PPTX
ACFE CERTIFICATION TRAINING ON LAW.pptx
PPTX
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
PDF
anganwadi services for the b.sc nursing and GNM
PDF
LATAM’s Top EdTech Innovators Transforming Learning in 2025.pdf
PDF
HSE 2022-2023.pdf الصحه والسلامه هندسه نفط
FAMILY PLANNING (preventative and social medicine pdf)
Neurological complocations of systemic disease
pharmaceutics-1unit-1-221214121936-550b56aa.pptx
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
English 2nd semesteNotesh biology biopsy results from the other day and I jus...
faiz-khans about Radiotherapy Physics-02.pdf
GIÁO ÁN TIẾNG ANH 7 GLOBAL SUCCESS (CẢ NĂM) THEO CÔNG VĂN 5512 (2 CỘT) NĂM HỌ...
PAIN PATHWAY & MANAGEMENT OF ACUTE AND CHRONIC PAIN SPEAKER: Dr. Rajasekhar ...
BSc-Zoology-02Sem-DrVijay-Comparative anatomy of vertebrates.pdf
namma_kalvi_12th_botany_chapter_9_ppt.ppsx
Power Point PR B.Inggris 12 Ed. 2019.pptx
Copy of ARAL Program Primer_071725(1).pptx
GW4 BioMed Candidate Support Webinar 2025
IS1343_2012...........................pdf
Review of Related Literature & Studies.pdf
ACFE CERTIFICATION TRAINING ON LAW.pptx
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
anganwadi services for the b.sc nursing and GNM
LATAM’s Top EdTech Innovators Transforming Learning in 2025.pdf
HSE 2022-2023.pdf الصحه والسلامه هندسه نفط

Matlab differential

  • 1. http://www.tutorialspoint.com/matlab/matlab_differential.htm Copyright © tutorialspoint.com MATLAB - DIFFERENTIAL MATLAB provides the diff command for computing symbolic derivatives. Inits simplest form, youpass the functionyouwant to differentiate to diff command as anargument. For example, let us compute the derivative of the functionf(t) = 3t2 + 2t-2 Example Create a script file and type the following code into it: syms t f = 3*t^2 + 2*t^(-2); diff(f) Whenthe above code is compiled and executed, it produces the following result: ans = 6*t - 4/t^3 Following is Octave equivalent of the above calculation: 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 result: ans = -(4.0)*t^(-3.0)+(6.0)*t Verification of Elementary Rules of Differentiation Let us briefly state various equations or rules for differentiationof functions and verify these rules. For this purpose, we willwrite f'(x) for a first order derivative and f"(x) for a second order derivative. Following are the rules for differentiation: Rule 1 For any functions f and g and any realnumbers a and b the derivative of the function: h(x) = af(x) + bg(x) withrespect to x is givenby: h'(x) = af'(x) + bg'(x) Rule 2 The sumand subtraction rules state that if f and g are two functions, f' and g' are their derivatives respectively, then, (f + g)' = f' + g' (f - g)' = f' - g' Rule 3
  • 2. The product rule states that if f and g are two functions, f' and g' are their derivatives respectively, then, (f.g)' = f'.g + g'.f Rule 4 The quotient rule states that if f and g are two functions, f' and g' are their derivatives respectively, then, (f/g)' = (f'.g - g'.f)/g2 Rule 5 The polynomial or elementary power rule states that, if y = f(x) = xn, thenf' = n. x(n-1) A direct outcome of this rule is derivative of any constant is zero, i.e., if y = k, any constant, then f' = 0 Rule 6 The chain rule states that, The derivative of the functionof a functionh(x) = f(g(x)) withrespect to x is, h'(x)= f'(g(x)).g'(x) Example Create a script file and type the following code into it: 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) Whenyourunthe 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)
  • 3. 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 Following is Octave equivalent of the above calculation: 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) Derivatives of Exponential, Logarithmic and Trigonometric Functions The following table provides the derivatives of commonly used exponential, logarithmic and trigonometric functions: Function Derivative ca.x ca.x.lnc.a (lnis naturallogarithm) ex ex ln x 1/x lncx 1/x.lnc xx xx.(1 + lnx) 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)
  • 4. Example Create a script file and type the following code into it: 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) Whenyourunthe 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 =
  • 5. 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 Following is Octave equivalent of the above calculation: 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 packages does not have this support %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) Computing Higher Order Derivatives To compute higher derivatives of a functionf, we use the syntax diff(f,n). Let us compute the second derivative of the functiony = f(x) = x .e-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) Following is Octave equivalent of the above calculation: pkg load symbolic symbols x = sym("x"); f = x*Exp(-3*x);
  • 6. differentiate(f, x, 2) Example Inthis example, let us solve a problem. Giventhat a functiony = f(x) = 3 sin(x) + 7 cos(5x). We willhave to find out whether the equationf" + f = -5cos(2x) holds true. Create a script file and type the following code into it: syms x y = 3*sin(x)+7*cos(5*x); % defining the function lhs = diff(y,2)+y; %evaluting 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); Whenyourunthe file, it displays the following result: No, the equation does not hold true Value of LHS is: -168*cos(5*x) Following is Octave equivalent of the above calculation: pkg load symbolic symbols x = sym("x"); y = 3*Sin(x)+7*Cos(5*x); % defining the function lhs = differentiate(y, x, 2) + y; %evaluting the lhs of the equation rhs = -5*Cos(2*x); %rhs of the equation 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); Finding the Maxima and Minima of a Curve If we are searching for the localmaxima and minima for a graph, we are basically looking for the highest or lowest points onthe graphof the functionat a particular locality, or for a particular range of values of the symbolic variable. For a functiony = f(x) the points onthe graphwhere the graphhas zero slope are called stationary points. In other words stationary points are where f'(x) = 0. To find the stationary points of a functionwe differentiate, we need to set the derivative equalto zero and solve the equation. Example Let us find the stationary points of the functionf(x) = 2x3 + 3x2 − 12x + 17 Take the following steps: 1. First let us enter the functionand plot its graph: syms x y = 2*x^3 + 3*x^2 - 12*x + 17; % defining the function ezplot(y)
  • 7. MATLAB executes the code and returns the following plot: Here is 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 2. Our aimis to find some localmaxima and minima onthe graph, so let us find the localmaxima and minima for the interval[-2, 2] onthe graph. 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 plot:
  • 8. Here is 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 3. Next, let us compute the derivative g = diff(y) MATLAB executes the code and returns the following result: g = 6*x^2 + 6*x - 12 Here is 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) 4. Let us 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 Following is Octave equivalent of the above calculation:
  • 9. 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]) 5. This agrees withour plot. So let us evaluate the functionf at the criticalpoints x = 1, -2. We cansubstitute a value ina symbolic functionby using the subs command. subs(y, 1), subs(y, -2) MATLAB executes the code and returns the following result: ans = 10 ans = 37 Following is 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) roots([6, 6, -12]) subs(y, x, 1), subs(y, x, -2) Therefore, The minimumand maximumvalues onthe functionf(x) = 2x3 + 3x2 − 12x + 17, inthe interval[- 2,2] are 10 and 37. Solving Differential Equations MATLAB provides the dsolve command for solving differentialequations symbolically. The most basic formof the dsolve command for finding the solutionto a single equationis : dsolve('eqn') where eqn is a text string used to enter the equation. It returns a symbolic solutionwitha set of arbitrary constants that MATLAB labels C1, C2, and so on. Youcanalso specify initialand boundary conditions for the problem, as comma-delimited list following the equationas: dsolve('eqn','cond1', 'cond2',…) For the purpose of using dsolve command, derivatives are indicated with a D. For example, anequation like f'(t) = -2*f + cost(t) is entered as: 'Df = -2*f + cos(t)' Higher derivatives are indicated by following D by the order of the derivative. For example the equationf"(x) + 2f'(x) = 5sin3x should be entered as:
  • 10. 'D2y + 2Dy = 5*sin(3*x)' Let us take up a simple example of a first order differentialequation: y' = 5y. s = dsolve('Dy = 5*y') MATLAB executes the code and returns the following result: s = C2*exp(5*t) Let us take up another example of a second order differentialequationas: y" - y = 0, y(0) = -1, y'(0) = 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