0% found this document useful (0 votes)
148 views

Calculus With Matlab: A Project Work

Uploaded by

Samin Khadka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views

Calculus With Matlab: A Project Work

Uploaded by

Samin Khadka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

`

CALCULUS WITH MATLAB

A Project Work
Submitted to the Office of Controller of Examination
Tribhuvan University, Balkhu in the partial fulfillment of
the requirements for the degree

BACHELOR OF SCIENCE IN MATHEMATICS


BY

Samin Babu Khadka (Roll No. 25)


Saswat Chapagain (Roll No. 26)
Saswat Subedi (Roll No. 27)
Shamila Lamichhane (Roll No. 28)
Sijan Poudel (Roll No. 29)
Sonam Lama (Roll No. 30)

GROUP-5...

DEPARTMENT OF MATHEMATICS
CENTRAL SCHOOL OF MATHEMATICS
TRIBHUVAN UNIVERSITY, NEPAL
(06-21-2021) 1
`

This page is intentionally left blank.

2
`

DEDICATION

This project is dedicated to all our friends, Hon. Dr. Jeevan Kafle, our MATLAB
teacher, and School of Mathematical Sciences, Tribhuvan University.

3
`

CERTIFICATE

This is to certify that the project entitled, “CALCULUS WITH MATLAB”


submitted by Miss SHAMILA LAMICHHANE and GROUP in partial fulfillments for
the requirements for the award of Bachelor of Science Degree in Mathematical Sciences
at Central School of Mathematical Sciences, Tribhuvan University is an authentic work
carried out by him under my supervision and guidance.

To the best of my knowledge, the matter embodied in the project report has not been
submitted to any other University / Institute for the award of any Degree or Diploma.

Mr. Jeevan Kafle


Department of Mathematical Science
Date:- Tribhuvan University
Balkhu, Kathmandu

4
`

ACKNOWLEDGEMENT

This project was carried out under the supervision of Dr. Jeevan Kafle. We would
like to express our sincere gratitude towards him for his excellent supervision,
guidance, encouragement, and constructive criticism for accomplishing this work.
And to the entire faculty of Mathematical Sciences for encouraging, supporting
and providing us this opportunity.
Special appreciation goes to Mr. Amar Bahadur Karki for helping us with some
calculus related problems.
We are indebted to our dear friends and classmates for their continuous support
and help on the completion of our project work.
Lastly, we would like to thank everyone who helped us directly and indirectly
during the duration of completing our project work.

5
`

ABSTRACT

In mathematical stratum, calculus is one of the most crucial topic which is widely
applied in different sectors. We have learnt about the theoretical calculus in our
course, yet we do lack the knowledge of its application using software. Hence, our
project is a demonstration on the application and computation of basics of
calculus using MATLAB.

The principal objective of this project is to inaugurate different codes and scripts
of MATLAB which can be used for calculation of limits, derivatives and integration
along with the verification of various theorems associated with these sub-topics.
Basic syntax used for calculation of limits is presented at the beginning followed
by the verification of basic properties of limits. Then the script for calculation and
verification of derivative portion is presented. Finally, the integration portion is
covered along with new coding initiatives related to our theoretical course.

Ultimately, we believe that our initiative will for sure broaden the horizons on
application of calculus.

6
`

CONTENTS
1. SYMBOLIC VARIABLES AND EXPRESSIONS……....................................................................................... 9
2. COMPUTATION OF LIMITS……............................................................................................................. 10
3. VERIFICATION OF BASIC PROPERTIES OF LIMIT….. ............................................................................. 11
3.1. INTRODUCTION ................................................................................................................................ 11
3.2. BRIEF DISCRIPTION ........................................................................................................................... 11
3.2.1. Property 1(Addition Theorem).................................................................................................. 11
3.2.2. Property 2 (Difference Theorem).............................................................................................. 12
3.2.3. Property 3 (Product Theorem) .................................................................................................. 12
3.2.4. Property 4 (Quotient Theorem) ................................................................................................ 12
3.3. MATLAB SCRIPT FOR VERIFICATION OF PR0PERTIES ....................................................................... 13
3.3.1. Script for verification of Addition Property .............................................................................. 13
3.3.2. Script for verification of Difference Property ........................................................................... 14
3.3.3. Script for verification of Product Property................................................................................ 14
3.3.4. Script for verification of Quotient Property .............................................................................. 15
3.3.5. Script for the verification of all the theorem in one attempt ................................................... 15
4. CALCULATION OF DERIVATIVES….. ..................................................................................................... 17
4.1. DERIVATIVE OF SINGLE VARIABLE FUNCTIONS ................................................................................ 17
4.2. DERIVATIVE OF MULTIPLE VARIABLE FUNCTIONS ........................................................................... 17
4.3. DERIVATTIVE OF CONSTANTS .......................................................................................................... 18
4.4. MIXED DERIVATIVES......................................................................................................................... 19
4.4. HIGHER ORDER DERIVATIVES........................................................................................................... 19
Pretty Command ..................................................................................................................................... 20
6.1. INTRODUCTION ................................................................................................................................ 21
6.2. VERIFICATION OF RULES .................................................................................................................. 22
6.2.1. Constant Rule ............................................................................................................................ 22
6.2.2. Power rule ................................................................................................................................. 23
6.3.3. Addition rule ............................................................................................................................. 24
6.3.4. Difference rule .......................................................................................................................... 26
6.3.5. Constant Multiple rule .............................................................................................................. 27
6.3.7. Quotient rule ............................................................................................................................. 30
6.3.8. Chain rule .................................................................................................................................. 32

7
`

7.1. INTRODUCTION ................................................................................................................................ 34


7.2. FINDING MAXIMA AND MINIMA OF A CURVE ................................................................................. 35
8.1. INTRODUCTION ................................................................................................................................ 37
8.2. COMPUTATION OF INDEFINITE INTEGRALS ................................................................................. 37
8.3. COMPUTATION OF DEFINITE INTEGRALS .................................................................................... 39
9. VERIFICATION OF BASIC RULES OF INTEGRATION .................................................................................. 42
9.1. INTRODUCTION ............................................................................................................................... 42
9.2. VERIFICATION OF RULES ................................................................................................................. 42
9.2.1. Addition Rule............................................................................................................................. 42
9.2.2. Difference Rule.......................................................................................................................... 44
9.2.3. Constant Multiple rule .............................................................................................................. 46
9.2.4. Reverse Chain rule .................................................................................................................... 47
9.2.5. Integration by parts .................................................................................................................. 49
9.2.6. Integration by parts in definite intervals .................................................................................. 51
10. SOLVIING DIFFERENTIAL EQUATIONS ................................................................................................... 55
10.1. SOLVING ODINARY DIFFERENTIAL EQUATIONS ............................................................................. 55
10.2. SOLVING DIFFERENTIAL EQUATIONS WITH …CONDITIONS ........................................................... 55
10.3. SOLVING DIFFERENTIAL EQUATIONS W.R.T. OTHER ..VARIABLES ................................................. 56
REFERENCES ................................................................................................................................................ 57

8
`

1. SYMBOLIC VARIABLES AND EXPRESSIONS……..

1.1. SYMS COMMAND


MATLAB has a type called sym for symbolic variables and expressions; these
work with character vectors. For example, to create a symbolic variable a
and
perform the addition just described, a symbolic variable would first be
created
by passing the character vector ‘a’ to the sym function:
>> a = sym('a');
>> a+a
ans =
2*a
In Symbolic Math Toolbox, you can declare symbolic objects using
either syms or sym. These two functions are conceptually different.
 The syms function creates a symbolic object that is automatically
assigned to a MATLAB variable with the same name.
 The sym function refers to a symbolic object that can be assigned to a
MATLAB variable with the same name or a different name.
The shortcut for the above symbolic function is syms
>> syms a
>> a+a
ans =
2*a
If more the one variable is to be used
the syms variable1 variable2 variable3 is used as:
>> syms a b c
Where a b and c are variable.

9
`

2. COMPUTATION OF LIMITS……..

2.1. LIMIT () COMMAND


Limit () is used for calculating limits. This function takes expression as an
argument and finds the limit of the expression as the independent variable
goes to zero.
Eg : limt of f(x)=(x3+5)/(x4+7) as x tends to 0 can be calculated as:
>> syms x
>> limit((x^3+5)/(x^4+7))
ans =
5/7
We can also calculate the limit of a function as the variable tends to some
number other than 0 i.e.
Lim (f(x))
x->a
then we must use limit command with arguments where the first argument
is the expression and second is the number that x approaches eg:
>> syms x
>> limit(x^2+5,3)
ans =
14

10
`

3. VERIFICATION OF BASIC PROPERTIES OF LIMIT…..

3.1. INTRODUCTION
Algebraic Limit Theorem provides some basic properties of limits. These
properties are useful for calculating limiting values of different functions. By
using these properties calculations can be more simplified. The basic
properties of limits are stated below :

Property 1: lim 𝑓(𝑥 ) + lim 𝑔(𝑥 ) = lim (𝑓(𝑥 ) + 𝑔(𝑥 ))


𝑥→𝑎 𝑥→𝑎 𝑥→𝑎

Property 2: lim 𝑓(𝑥 ) − lim 𝑔(𝑥 ) = lim (𝑓(𝑥 ) − 𝑔(𝑥 ))


𝑥→𝑎 𝑥→𝑎 𝑥→𝑎

Property 3: lim 𝑓(𝑥 ) ∗ lim 𝑔(𝑥 ) = lim (𝑓(𝑥 ) ∗ 𝑔(𝑥 ))


𝑥→𝑎 𝑥→𝑎 𝑥→𝑎

Property 4: lim 𝑓(𝑥 )/ lim 𝑔(𝑥 ) = lim (𝑓(𝑥 )/𝑔(𝑥 ))


𝑥→𝑎 𝑥→𝑎 𝑥→𝑎

3.2. BRIEF DISCRIPTION


Now, we will have a brief description on each properties listed above.

3.2.1. Property 1(Addition Theorem)


For any two functions f(x) and g(x), the limit of sum of these functions is equal
to the sum of the limits of the functions i.e. the sum of limiting values of two
different functions equals to the limiting value of the composite function
obtained by addition of the given functions at a particular point.
Mathematically,
lim 𝑓(𝑥 ) + lim 𝑔(𝑥 ) = lim (𝑓 (𝑥 ) + 𝑔(𝑥 )) holds true.
𝑥→𝑎 𝑥→𝑎 𝑥→𝑎

11
`

3.2.2. Property 2 (Difference Theorem)


For any two functions f(x) and g(x), the limit of difference of these functions
is equal to the difference of the limits of the functions i.e. the difference of
limiting values of two different functions equals to the limiting value of the
composite function obtained by subtraction of the given functions at a
particular point.
Mathematically,
lim 𝑓(𝑥 ) − lim 𝑔(𝑥 ) = lim (𝑓 (𝑥 ) − 𝑔(𝑥 )) holds true.
𝑥→𝑎 𝑥→𝑎 𝑥→𝑎

3.2.3. Property 3 (Product Theorem)


For any two functions f(x) and g(x), the limit of product of these functions is
equal to the product of the limits of the functions i.e. the product of limiting
values of two different functions equals to the limiting value of the composite
function obtained by multiplication of the given functions at a particular
point.
Mathematically,
lim 𝑓(𝑥 ) ∗ lim 𝑔(𝑥 ) = lim (𝑓 (𝑥 ) ∗ 𝑔(𝑥 )) holds true.
𝑥→𝑎 𝑥→𝑎 𝑥→𝑎

3.2.4. Property 4 (Quotient Theorem)


For any two functions f(x) and g(x), the limit of quotient of these functions is
equal to the quotient of the limits of the functions i.e. the quotient of limiting
values of two different functions equals to the limiting value of the composite
function obtained by division of the given functions at a particular point.
Mathematically,
lim 𝑓 (𝑥 )/ lim 𝑔(𝑥 ) = lim (𝑓(𝑥 )/𝑔(𝑥 )) holds true.
𝑥→𝑎 𝑥→𝑎 𝑥→𝑎

12
`

3.3. MATLAB SCRIPT FOR VERIFICATION OF PR0PERTIES


The basic algorithm for the verification of the properties using MATLAB script
is given below:
1.Assigning symbolic value to MATLAB variable.
 “syms” function is used for this purpose to assign symbolic value to
the variable used in the functions used in verification.
2. Defining the functions used in verification.
 It simply means stating the functions used for verification and there
is no compulsion of using a particular type of function. e.g. f = x +1
3. Using limit command.
 Now, the “limit” command is used e.g. for calculating the addition
property the limit command is used as
Ladd= limit(f+g,1)
4. Run
Here we have chosen two functions f(x) and g(x) defined as
f=(x^2) and g=(2*x^2-1) respectively. Firstly, scripts for verification of
individual theorem are listed below and finally the script for verification of all
the theorem in single attempt is presented.

3.3.1. Script for verification of Addition Property


The script is as follows
syms x
f=(x^2);
g=(2*x^2-1);
l1=limit(f,1);
l2=limit(g,1);
ladd=limit(f+g,1);
if ladd==(l1+l2)
disp('property 1 verified')

13
`

else
disp('property 1 not verified')
end
After execution, command window displays the statement 'property 1
verified'. Thus, addition theorem is verified.

3.3.2. Script for verification of Difference Property


The script is as follows:
syms x
f=(x^2);
g=(2*x^2-1);
l1=limit(f,1);
l2=limit(g,1);
ladd=limit(f-g,1);
if ladd==(l1-l2)
disp('property 1 verified')
else
disp('property 1 not verified')
end
After execution, command window displays the statement 'property 2
verified'. Thus, difference theorem is verified.

3.3.3. Script for verification of Product Property


The script is as follows:
syms x
f=(x^2);
g=(2*x^2-1);
l1=limit(f,1);
l2=limit(g,1);
ladd=limit(f*g,1);

14
`

if ladd==(l1*l2)
disp('property 3 verified')
else
disp('property 3 not verified')
end
After execution, command window displays the statement 'property 3
verified'. Thus, product theorem is verified.

3.3.4. Script for verification of Quotient Property


The script is as follows:-
syms x
f=(x^2);
g=(2*x^2-1);
l1=limit(f,1);
l2=limit(g,1);
ladd=limit(f\g,1);
if ladd==(l1\l2)
disp('property 4 verified')
else
disp('property 4 not verified')
end
After execution, command window displays the statement 'property 4
verified'. Thus, quotient theorem is verified.

3.3.5. Script for the verification of all the theorem in one attempt
syms x
f=input(‘Enter first function:’);
g=input(‘Enter second function:’);
a=input(‘Enter the limiting point:’)
l1=limit(f, a);
l2=limit(g,a);

15
`

ladd=limit(f+g,a);
lsub=limit(f-g,a);
lmul=limit(f*g,a);
ldiv=limit(f/g,a);
if ladd==(l1+l2)
disp('property 1 verified')
else
disp('property 1 not verified')
end
if lsub==(l1-l2)
disp('property 2 verified')
else
disp('property 2 not verified')
end
if lmul==(l1*l2)
disp('property 3 verified')
else
disp('property 3 not verified')
end
if ldiv==(l1/l2)
disp('property 4 verified')
else
disp('property 4 not verified')
end

16
`

4. CALCULATION OF DERIVATIVES…..

4.1. DERIVATIVE OF SINGLE VARIABLE FUNCTIONS

The derivative of a function can be calculated by using diff command in


MATLAB. Since differentiation is a symbolic calculation, have to
use syms command to define the variables. The derivative of a single
variable function can be calculated in the following ways:
 Use syms to define the variable as a symbol.
 Define the function
 Use diff(function) to calculate the Derivative.
For Example:
Calculate the derivative of f(x)=8x2+5x3

>> syms x
>> f=8*x^2+5*x^3;
>> diff(f)

Here, MATLAB calculates the derivative of f with respect to x by default and


gives the answer:
ans=
16*x+15*x^2

4.2. DERIVATIVE OF MULTIPLE VARIABLE FUNCTIONS


To calculate the derivative of a function with multiple variables, you must
specify the variable you want to differentiate the function with respect to or
else MATLAB will use a default variable.
The syntax for calculative Derivative of a function with respect to a certain
variable is:
diff(function,var)
For Example:

17
`

Calculate the derivative of the function, f(x,z)= x2+9xz+z3 w.r.t. z

>> syms x z
>>f= x^2+9*x*z+z^3;
>>diff(f,x)

Here, MATLAB calculates the derivative of f with respect to x and gives the
answer:
ans=
2*x+9*z

4.3. DERIVATTIVE OF CONSTANTS


To calculate the derivative of a constant, we must first determine the
constant as a symbolic expression
using sym command i.e. c=sym(‘constant’)
For example:
Calculate the derivative of 5
If we just enter diff(5), MATLAB doesn’t give the required answer because 5
is not a symbolic expression. So, we must determine 5 as a symbolic
expression by using sym.

>> c=sym(‘5’);
>>diff(c)

Here, MATLAB calculates the derivative of 5 with respect to a default


variable and gives the answer:
ans=
0

18
`

4.4. MIXED DERIVATIVES


Mixed derivative means derivative of a function with respect to two or more
variables. We can calculate the Derivative of a function with respect to
multiple variables in the following way:
diff(function,var1,var2,..)
For example:
Calculate the derivative of xsin(xy) with respect to x and y

>> syms x y
>>f=x*sin(x*y);
>>diff(f,x,y)

Here, MATLAB calculates the derivative of f with respect to x and y and gives
the answer:
ans=
x^2*cos(x*y)
h

4.4. HIGHER ORDER DERIVATIVES


To calculate higher orden derivative of a function in MATLAB, we must
specify which orfer derivative we want to calculate. The syntax to calculate
the nth order derivative of a function is:
diff(function,var,n)
For example:
Calculate the 4th order derivative of the function, f(x,z)=
x2+9xz5+z7 with respect to z.

>>syms x z
>>f=x^2+9*x*z^5+z^7;
>>diff(f,z,4)

Here, MATLAB calculates the 4thderivative of f with respect to z and gives


the answer:
19
`

ans=
1080*x*z+840z^3

PRETTY COMMAND
Sometimes during the calculation of derivatives, the answer is complicated
and hard to understand. So we can pretty command to make the answer
look pretty and understandable.
For example:
Calculate the derivative of sin5x2/ √8x^3

>> syms x
>> f=sin(5*x^2)/sqrt(8*x^3);
>> diff(f)

ans=
5/2*cos(5*x^2)*x*2^(1/2)/(x^3)^(1/2)-
3/8*sin(5*x^2)*2^(1/2)/(x^3)^(3/2)*x^2

Here, the answer is very lengthy and complicated, so we can use pretty
command to make the answer look easily understandable

>>pretty(ans)
2 1/2 2 1/2 2
cos(5 x ) x 2 sin(5 x ) 2 x
5/2 ---------------- - 3/8 -----------------
3 1/2 3 3/2
(x ) (x )

20
`
6. PROOF OF BASIC RULES OF DIFFERENTIATION

6.1. INTRODUCTION
The 8 basic rules of differentiation are listed below:

21
`

6.2. VERIFICATION OF RULES


The 8 basic rules of differentiation are discussed below along with their
respective examples and MATLAB codes.

6.2.1. Constant Rule


Let c be a constant. If f(x)=c, then f'(x)=0.
Alternatively, this may be expressed as:
Question
Find the derivative of f(x)=15.
Solution
Since 8 is a constant, f'(8) =0.

MATLAB
If the direct differentiation via MATLAB is the same as 0, we can prove that
the law stands.

Order of code for proof


 Define the variables used in the equation as symbolic variables.
>>syms x
 Define the function that is to be differentiated.
>> f=10
 Ask MATLAB to differentiate the function directly and store it in a
variable.
>>df=diff(f)
 Using conditional statement(if), equate the differentiated
value/function to the formula.
Our formula states that the differentiated value must be 0.
if df==0
disp(‘rule’ proved)
end
22
`

MATLAB code
f=input('enter the function f: ');
syms x %necessary because we differentiate w.r.t x
derivative=diff(f,x) %differentiating f w.r.t x
if derivative==0 %derivative of constant is 0
display('derivative of constant rule proved')
else
display('rule not proved')
end

6.2.2. Power rule


Let n be a positive integer. If f(x)=xn ,then
f'(x)=nxn-1
This can be expressed as:
Example:
f(x)= x7
f'(x)=7 x7-1
f'(x)=7x6

MATLAB
If the direct differentiation via MATLAB is the same as 7x6, we can prove that
the law stands.
Note: In newer versions of MATLAB, MATLAB directly gives n xn-1 as the
derivative for xn.
Order of code for proof 
 Define the variables used in the equation as symbolic variables.  
>>syms x 
 Define the function that is to be differentiated. 
>> f=x^7

23
`

 Ask MATLAB to differentiate the function directly and store it in a


variable. 
>>df=diff(f) 
 Using conditional statement(if), equate the differentiated
value/function to the formula.  
Our formula states that the differentiated value must be 7x^6. 
if df==7*x^6
disp(‘rule’ proved) 
end 

MATLAB Code
syms x
f=x^7; %placing the function
df=diff(f)%direct derivative
a=7*x^6 %derivative= n×x^(n-1) i.e 7*x^6
if df==a
disp('power rule proved')
else disp('power rule not proved')
end

6.3.3. Addition rule


For differentiable functions f(x) and g(x), the derivative of the sum of a
function f and a function g is the same as the sum of the derivative of f and
the derivative of g.
If h(x)=f(x)+g(x)
h'(x)=f'(x)+g'(x)
Example:
f(x)= 5x
g(x)= 3x
h(x)=f(x)+g(x)=5x+3x=8x
h'(x)=8

24
`

MATLAB
If the direct differentiation via MATLAB is the same as f'(x)+g'(x), we can
prove that the law stands.Page BreakOrder of code for proof 
 Define the variables used in the equation as symbolic variables.  
>>syms x 
 Define the functions that are to be added. 
>> f=
>>g=
 Store the sum of the defined functions in another function
>>h= f+g
 Ask MATLAB to differentiate the cumulative function and directly and
store it in a variable. 
>>dh=diff(h) 
 Using conditional statement(if), equate the differentiated
value/function to the formula.  
Our formula states that the differentiated value must be f’(x)+g’(x). 
if dh==df+dg
disp(‘rule’ proved) 
end 

MATLAB code
syms x %creating symbolic variable x
f=input('enter the function f:'); %putting in the function f(x)
g=input('enter the function g:'); %putting in function g(x)
Df=diff(f)
Dg=diff(g)
a=diff(f+g) % derivative after addition (f(x)+g(x))’
a1=Df+Dg %addition of derivatives i.e f'(x)+ g'(x)
if a==a1
disp('Addition rule proved')

25
`

else disp('Addition rule not proved')


end

6.3.4. Difference rule


For differentiable functions f and g, the derivative of the difference of a
function f and function g is the same as the difference of the derivative of f
and the derivative of g
If h(x)=f(x)−g(x)
h'(x)=f'(x)−g'(x)
Example
f(x)= 5x
g(x)= 3x
h(x)=5x-3x=2x
h'(x)=2

MATLAB
If the direct differentiation via MATLAB is the same as f'(x)-g'(x), we can
prove that the law stands.
Order of code for proof 
Define the variables used in the equation as symbolic variables.  
>>syms x 
Define the functions of which difference is to be found. 
>>f=
>>g=
Store the difference of the defined functions in another function
>>h= f-g
Ask MATLAB to differentiate the combined function and directly and store it
in a variable. 
>>dh=diff(h) 
 Using conditional statement(if), equate the differentiated
value/function to the formula.  
26
`

Our formula states that the differentiated value must be f’(x)-g’(x). 


if dh==df-dg
disp(‘rule’ proved) 
end 

MATLAB code
syms x %creating symbolic variable x
f=input('enter the function f:'); %putting in the function f(x)
g=input('enter the function g:'); %putting in function g(x)
Df=diff(f);
Dg=diff(g);
a=diff(f-g)% derivative after addition i.e diff(h) if h=f-g
a1=Df-Dg %derivative= f'(x)- g'(x)
if a==a1
disp('Difference rule proved')
else disp('Difference rule not proved')
end

6.3.5. Constant Multiple rule


For a differentiable function f and constant k, the derivative of a constant k
multiplied by a function f is the same as the constant multiplied by the
derivative
If h(x)=k x f(x)
h'(x)=k x f'(x)

Example
f(x)=x2
k=2
h(x)=k.f(x)=2 x f(x)=2x2
h'(x)=k.f'(x)=2(2x)=4x

27
`

MATLAB
If the direct differentiation via MATLAB is the same as k x f'(x), we can prove
that the law stands.

Order of code for proof 


 Define the variables used in the equation as symbolic variables.  
>>syms x 
 Define the function that is to be multiplied by a constant. 
>>f=
 Store the constant value in a variable.
>>k=
 Store the constant multiplied by the function f in a different function
>>h= k*f
 Ask MATLAB to differentiate the combined function and directly and
store it in a variable. 
>>dh=diff(h) 
 Using conditional statement(if), equate the differentiated
value/function to the formula.  
Our formula states that the differentiated value must be k x f’(x). 
if dh==k*diff(f)
disp(‘rule’ proved) 
end 

MATLAB code
syms x %creating symbolic variable x
f=input('enter the function f: '); %putting in the function f(x)
k=input('enter the constant k:'); %defining the constant
a=diff(k*f)% direct first derivative of k.f(x)
b=k*diff(f) %formula= k.f'(x)
if a==b

28
`

disp('constant multiple rule proved')


else disp('constant multiple rule not proved')
end

6.3.6. Product rule


If f(x) and g(x) are two differentiable functions and the derivative of their
product is to be found, the derivative is the derivative of the first function
times the second function plus the derivative of the second function times
the first function.
If h(x)=f(x)g(x)
h'(x)=f'(x)g(x)+g'(x)f(x)
Example
f(x)= (x2+2)
g(x)= (3x3−5x)
h(x)=(x2+2)(3x3−5x)
h'(x)= f'(x)g(x)+g'(x)f(x)
=(2x)(3x3−5x)+(9x−5)(x2+2)
=15x4+3x2−10

MATLAB
If the direct differentiation via MATLAB is the same as f'(x)g(x)+g'(x)f(x), we
can prove that the law stands.
Order of code for proof
 Define the variables used in the equation as symbolic variables.
>>syms x
 Define the functions that are to be multiplied.
>>f=
>>g=
 Ask MATLAB to differentiate the product directly and store it in a
variable.
>>dh=diff(f*g)

29
`

 Using conditional statement(if), equate the differentiated


value/function to the formula.
Our formula states that the differentiated value must be f'(x)g(x)+g'(x)f(x).
if dh== diff(f)*g+diff(g)*f
disp(‘rule’ proved)
end

MATLAB code
syms x %creating symbolic variable x
f=input('enter the function f: '); %putting in the function f(x)
g=input('enter the function g: '); %putting in function g(x)
a=diff(f*g)% direct derivative of product
a1=diff(f)*g+diff(g)*f %formula=f'(x).g(x)+g'(x).f(x)
if a==a1
disp('product rule proved')
else disp('product rule not proved')
end

6.3.7. Quotient rule


If f(x) and g(x) are differentiable functions and q(x) is the quotient of the two
functions with f(x) as the denominator and g(x) as the numerator, the
derivative of q(x) can be written as:
q'(x)=[f'(x)g(x)−g'(x)f(x)]/ g(x)2

MATLAB
If the direct differentiation via MATLAB is the same as f'(x)g(x)-
g'(x)f(x)/ g(x)2, we can prove that the law stands.
Order of code for proof
 Define the variables used in the equation as symbolic variables.
>>syms x

30
`

 Define the functions that are to be divided.


>>f=
>>g=
 Ask MATLAB to differentiate the quotient directly and store it in a
variable.
>>dh=diff(f/g)
 Simplify the derivative
>>dh=simplify(dh)
 Using conditional statement(if), equate the differentiated
value/function to the formula.
Our formula states that the differentiated value must be f'(x)g(x)-
g'(x)f(x)/g(x)2
if dh== simplify( (diff(f)*g-f*diff(g))/(g^2))
disp(‘rule’ proved)
end

MATLAB code
syms x %creating symbolic variable x
f=input('enter the function f: '); %putting in the function f(x)
g=input('enter the function g: '); %putting in function g(x)
a=diff(f/g);% direct derivative of quotient
a=simplify(a)
b=(diff(f)*g-f*diff(g))/(g^2);
b=simplify(b)
if a==b
disp('quotient rule proved')
else disp('quotient rule not proved')
end

31
`

6.3.8. Chain rule


Chain rule is used for differentiating a composite function. Composite
function is a function made of two or more functions. It comprises of inner
and outer function. If f(x) is the outer function and g(x) is the inner function,
the composite function can be written as fog(x) or f(g(x)).
The composite function f(g(x)) is calculated for a value of x by first
evaluating g(x) and then evaluating the function f at this value of g(x), thus
“chaining” the results together1.
The derivative of composite function is expressed as:
[f(g(x)]’= f′(g(x))⋅g′(x)

Point to consider during programming: The f′(g(x)) part in the derivative is


again a composite function of f’(x) and g(x) with f’(x) as the outer function
and g(x) as the inner function.
Example
f(x)=x2 f’(x)=2x
g(x)=x+2 g’(x)=1
h(x)=f(g(x))= (x+2)2
h'(x)= f′(g(x))⋅g′(x)
= 2(x+2)*1
= 2x+4

MATLAB
If the direct differentiation via MATLAB is the same as f′(g(x))⋅g′(x), we can
prove that the law stands.
Order of code for proof
 Define the variables used in the equation as symbolic variables.
>>syms x
 Define the functions that are to be used to make a composite
function.
>>f=

32
`

>>g=
 Ask MATLAB to compose the functions and store it in a variable.
>>h=compose(f,g)
Here, the order in which the function is entered is in the order of inner and
outer function.
 Differentiate the composite function directly and store it in a variable.
>>dh=diff(h)
 Simplify the derivative
>>dh=simplify(dh)
 Store the composite part of the derivative (f′(g(x))) in a variable
>>b=compose(diff(f),g)
 Using conditional statement(if), equate the differentiated
value/function to the formula.
Our formula states that the differentiated value must be f′(g(x))⋅g′(x).
if dh== b*diff(g)
disp(‘rule’ proved)
end

MATLAB code
syms x %creating symbolic variable x
g=input('enter the inner function:')
f=input('enter the outer function:')
h=compose(g,h) %h(x)=f(g(x))
a=diff(h) %direct differentiation
b=compose(diff(f),g); %b=f'(g(x))
b=simplify(b)
%formula
c=b*diff(g) %h'(x)=f'(g(x))×g'(x)
if a==c
disp('chain rule proved')
else disp('chain rule not proved')
end

33
`
7. MAXIMA AND MINIMA OF A CURVE

7.1. INTRODUCTION
If we are searching for the local maxima and minima for a graph, we are
basically looking for the highest or lowest points on the graph of the
function at a particular locality, or for a particular range of values of the
symbolic variable.

Some of the functions that we are using to find maxima and minima of a
curve are:

Derivative/Differentiate:diff(f)
diff (f) will differentiate ‘f’ with the variable identified by sym var.

solve(g)
solves the equation equ for the variable var. If you do not specify var,
the sym var function
determines the variable to solve for.
For example:
syms x
g= x^2-1;
solve (g)
ans = -1,1

Symbolic substitution:subs(s,new)
subs(s,new) returns a copy of ’s’, replacing all occurrences of the default
variable in ’s’ with new, and
then evaluates ’s’.
For example:
syms x

34
`

y=x^2+5
subs(y,2)
It gives
Y=9 (it substitute value of x by 2 in the equation.)

7.2. FINDING MAXIMA AND MINIMA OF A CURVE


Q. Find the maxima and minima of 2x3+3x2-12x+17 for the interval [-2, 2] on
the graph.
>>clc
>>clear
>>syms x
>>y = 2*x^3 + 3*x^2 - 12*x + 17
>>ezplot(y,[2,-2])
>>grid on
>>g=diff(y)
>>s=solve(g)
>>subs(y,s)

35
`

36
`

8. COMPUTATION OF INTEGRALS..

8.1. INTRODUCTION
There are two types of integrals
Indefinite integrals:
By definition, if the derivative of a function f(x) is f'(x), then we say that an
indefinite integral of f'(x) with respect to x is f(x).
Definite integrals:
By definition, definite integral is basically the limit of a sum. Definite
integrals are used for finding area, volume, center of gravity, moment of
inertia, work done by a force, and in numerous other applications.

8.2. COMPUTATION OF INDEFINITE INTEGRALS


Since the derivative (with respect to x) of x2 is 2x, we can say that an
indefinite integral of 2x is x2.
In symbols −
f’(x2) = 2x, therefore,
∫ 2xdx = x^2.
Indefinite integral is not unique, because derivative of x^2 + c, for any value
of a constant c, will also be 2x.
This is expressed in symbols as −
∫ 2xdx = x^2 + c.
Where, c is called an 'arbitrary constant'.

Integration: int(f)
MATLAB provides an int command for calculating integral of an expression.
To derive an expression for the indefinite integral of a function, we write −
int(f);

37
`

For example, from our previous example −


syms x int(2*x)
MATLAB executes the above statement and returns the following result −
ans = x^2

Example 1
>>clc
>>syms x
>>f(x)=x.^2+2*x
>>int(f(x))
>>pretty(ans)

Example 2
>>clc
>>syms x
>>f(x)= sin(x)+cos(x)^2
>>int(f(x))
>>pretty(ans)

38
`

8.3. COMPUTATION OF DEFINITE INTEGRALS


By definition, definite integral is basically the limit of a sum.
The int function can be used for definite integration by passing the limits
over which you want to calculate the integral.
To calculate:

We write: int(x,a,b)
For example, to calculate the value of

We write:
Int(x,4,9)
MATLAB executes the above statement and returns the following result −
ans = 65/2

Example 1
To calculate the area enclosed between the x-axis, and the curve y =
x3−2x+5 and the ordinates x = 1 and x = 2.

39
`

The required area is given by −

>>clc
>>clear
>>syms x
>>f(x) = x^3 - 2*x +5;
>>a = int(f(x), 1, 2)
>>display('Area: ')
>>disp(double(a))

Example 2
Find the area under the curve: f(x) = x2 cos(x) for −4 ≤ x ≤ 9.
The required area is given by −

>>clc
>>clear
>>syms x
>>f(x) = x^2*cos(x);
>>ezplot(f(x), [-4,9])

40
`

>>a = int(f(x), -4, 9)


>>disp('Area: ')
>>disp(double(a))

41
`

9. VERIFICATION OF BASIC RULES OF INTEGRATION

9.1. INTRODUCTION
The eight basic theorems of integration are as follows:
 ∫ [𝑓(𝑥) + 𝑔(𝑥)]𝑑𝑥 = ∫ 𝑓(𝑥)𝑑𝑥 + ∫ 𝑔(𝑥)𝑑x
 ∫ [𝑓(𝑥) − 𝑔(𝑥)]𝑑𝑥 = ∫ 𝑓(𝑥)𝑑𝑥 − ∫ 𝑔(𝑥)𝑑𝑥
 ∫ 𝑘𝑓(𝑥)𝑑𝑥 = 𝑘∫ 𝑓(𝑥)𝑑𝑥
 ∫ 𝑓(𝑎𝑥 + 𝑏)𝑑𝑥 = 1/𝑎 𝐹(𝑎𝑥 + 𝑏) + c
 ∫ 𝑓(𝑥)𝑓 ′ (𝑥)𝑑𝑥 = 1/2.f 2 (x)-c [c= 1/2*f 2(0)]
 ∫f(x) g(x)dx=f(x).∫g(x)dx-∫(f’(x).∫g(x)dx)dx + c
𝑏
 If ∫f(x)=F(x), then ∫𝑎 𝑓(𝑥)= F(b)-F(a)
𝑏 b 𝑏
 ∫𝑎 𝑓 (𝑥 ). 𝑔(𝑥)= [f(x)∫g(x)dx]a -∫𝑎 𝑓 ′ (𝑥 ). ∫ 𝑔(𝑥 ). 𝑑𝑥

9.2. VERIFICATION OF RULES


The 8 basic rules of differentiation are discussed below along with their
respective examples and MATLAB codes.

9.2.1. Addition Rule


For functions f(x) and g(x), the integration of the sum of a function f and a
function g is the same as the sum of the integration of f and the integration
of g.
∫ [(𝑥) + 𝑔(𝑥)]𝑑𝑥 = ∫ 𝑓(𝑥)𝑑𝑥 + ∫ 𝑔(𝑥)𝑑x

42
`

Example:
f(x)= 5x
g(x)= 3x
h(x)=f(x)+g(x)=5x+3x=8x
∫f(x)=5/2.x^2
∫g(x)=3/2.x^2
∫h(x)=4.x^2

MATLAB
If the direct integration of f(x)+g(x) is equal to the sum of individual
integration, we can verify the theorem
Orders to follow:
 Define the variable used in the equations as symbolic variables
>>syms x
 Define the functions f and g
>>f=
>>g=
 Integrate the individual function f and g using int() function and assign
the value to some variables
>>a=int(f)
>>b=int(g)
 Add the integrated individual function, and simplify it using the
simplify() command and assign it to another variable
>>c=simplify(a+b)

43
`

 Integrate the sum of f and using int() function, and assign it to another
variable
>>d=int(f+g)
 Use if/else command to check whether the value of c and d are equal
or not
[Note: Before equating, we must use simplify command to reduce the
equation to the simplest form because while equating MATLAB checks
whether the terms are symbolically equal or not, regardless of their
mathematically equivalency]

MATLAB Code
syms x
f=x.^2+2*x+3;
g=4*x+5;
a=int(f(x))
b=int(g(x))
c=simplify(b+c)
d=simplify(int(f(x)+g(x)))
a
if a==d
disp('true')
else disp('false')
end

9.2.2. Difference Rule


For functions f(x) and g(x), the integration of the difference of a function f
and a function g is the same as the difference of the integration of f and the
integration of g.
∫ [(𝑥)-𝑔(𝑥)]𝑑𝑥 = ∫ 𝑓(𝑥)𝑑𝑥 - ∫ 𝑔(𝑥)𝑑x
Example:
44
`

f(x)= 5x
g(x)= 3x
h(x)=f(x)-g(x)=5x-3x=2x
∫f(x)=5/2.x^2
∫g(x)=3/2.x^2
∫h(x)=x^2=∫f(x)- ∫g(x)

MATLAB
If the direct integration of f(x)-g(x) is equal to the difference of individual
integration, then theorem stands.
Orders to follow:
 Define the variable used in the equations as symbolic variables
>>syms x
 Define the functions f and g
>>f=
>>g=
 Integrate the individual function f and g using int() function and assign
the value to some variables
>>a=int(f)
>>b=int(g)
 Subtract the integrated individual function, and simplify it using the
simplify() command and assign it to another variable
>>c=simplify(a-b)
 Integrate the difference of f and g using int() function, and assign it to
another variable

45
`

>>d=int(f-g)
 Use if/else command to check whether the value of c and d are equal
or not

MATLAB Code
syms x
f(x)=x.^2+2*x+3;
g(x)=4*x+5;
a=int(f(x))
b=int(g(x))
c=simplify(b-c)
d=simplify(int(f(x)-g(x)))
if a==d
disp('true')
else disp('false')
end

9.2.3. Constant Multiple rule


For a function f and constant k, the integration of a constant k multiplied by
a function f is the same as the constant multiplied by the integration.
Example:
f(x)=x^2
k=2
∫f(x)=x^3/3
∫f(x).k=2/3.x^3=k.∫f(x)

MATLAB
If the direct integration of multiplication of the function and the constant is
equal to the multiplication of integration of function and constant, then the
theorem stands.

46
`

Order
 Define the variable used in the equations as symbolic variables
>>syms x
 Define function f
>>f=
 Define constant k
>>k=
 Integrate the multiplication of the function and the constant and
assign it to a variable a
>>a=int(f*k)
 Integrate the function f, multiply it with the constant, simplify the
term, and assign it to another variable b
>>b=simplify(int(f)*k)
 Use if/else command to see whether they equate or not.

MATLAB CODE
syms x
f(x)=x.^2+2*x+3;
k=2
a=int(k*f(x))
b=simplify(int(f)*k)
if a==b
disp('true')
else disp('false')
end

9.2.4. Reverse Chain rule


Reverse chain rule is used for integrating a composite function. Composite
function is a function made of two or more functions. It comprises of inner
and outer function. If f(x) is the outer function and g(x) is the inner function,
then the composite function can be written as fog(x) or f(g(x)).
According to the composite function rule:
∫fog(x)=compose(∫f(x),g(x))/diff(g(x))
47
`

The use of composite function is made to integrate the function of


h=(a.x+b)^2 type.

An important step to follow while programming:


 Converting the function h into a composite function by layering out
two functions f and g and composing them
g(x)=a.x+b
f(x)=x^2
h(x)=fog(x)

MATLAB
If the direct integration of the composite function fog(x)=h(x) is same as the
composition of the integration of the function f with the function g whole
divided by the differentiation of g, then the theorem stands.

Orders
 Define the variable used in the equations as a symbolic variable
>>syms x
 Define the function f and g according to the bigger composite function
>>f=
>>g=
 Compose functions f and g to get the actual function h
>>h=compose(f,g)
 Integrate the function g and assign the value to a variable
>>a=int(h)
 Integrate f and assign to a variable
>>b=intf(x)
 Compose the integrated f with g, divide the whole expression by
differentiation of g, assign it to a variable
>>c=compose(b,g(x))/diff(g(x))
 Simplify the expression
>>c=simplify(c)

48
`

 Use if/else command to see whether a and c are equal or not

MATLAB Code
syms x
f(x)=x.^2;
g(x)=(2*x+3);
h(x)=compose(f,g)
a=int(h(x));
b=int(f(x));
c=compose(b,g(x))/diff(g(X))
c=simplify(c)
a
if a==c
disp('true')
else
disp ('false')
end

9.2.5. Integration by parts


For functions f and g, the integration of the multiplication of functions is
same as the multiplication of f with the integrated value of g subtracted to
integration of multiplication of derivative of f and integration of g.
∫f(x) g(x)dx=f(x).∫g(x)dx-∫(f’(x).∫g(x)dx)dx + c

MATLAB
If the integration of the multiplication of functions is same as the
multiplication of f with the integrated value of g subtracted to integration of
multiplication of derivative of f and integration of g.
Orders:
 Define the variable used in the equations as a symbolic variable
>>syms x

49
`

 Define functions f and g


>>f(x)=
>>g(x)=
 Multiply the functions f and g, integrate the whole expression and
assign it to a variable
>>a=int(f(x)*g(x))
 Integrate the function g and differentiate the function f and assign the
values to some variables respectively
>>b=int(g(x))
>>c=diff(f(x))
 Multiply f with the integrated value of g and subtract the expression
with integration of multiplication of derivative of f and integration of
g, and assign the whole value to a variable
>>d=f(x)*b-int(c*b)
 Simplify the expression
>>d=simplify(d)
 Use if/else statement to equate a and d

MATLAB Code:
syms x
f(x)=log(x);
g(x)=sin(x);
a=int(f(x)*g(x));
b=int(g(x));
c=diff(f(x));
d=f(x)*b-int(c*b);
e=simplify(d)
a

50
`

if a==e
disp('true')
else
disp('false')
end

9.2.6. Integration by parts in definite intervals


For functions f and g, the integration of the multiplication of functions under
definite interval is same as the multiplication of f with the integrated value
of g with upper and lower limits subtracted to integration of multiplication
of derivative of f and integration of g under definite intervals.
𝑏 𝑏
∫𝑎 𝑓 (𝑥 ). 𝑔(𝑥)= [f(x)∫g(x)dx]ab-∫𝑎 𝑓 ′ (𝑥 ). ∫ 𝑔(𝑥 ). 𝑑𝑥

MATLAB:
If the integration of the multiplication of functions under definite interval is
same as the multiplication of f with the integrated value of g with upper and
lower limits subtracted to integration of multiplication of derivative of f and
integration of g under definite intervals, then the theorem stands.
Order
• Define the variable used in the equations as a symbolic variable
>>syms x
 Define the functions f and g
>>f(x)=
>>g(x)=
 Set upper limit and lower limit
>>a=

51
`

>>b=
 Multiply functions f and g, integrate it under upper and lower limits,
use double() function for double precision, and assign it to a variable.
>>y=double(int(f(x)*g(x)), a, b)
 Integrate g(x) and differentiate f(x) and assign in to some variables
>>c=int(g(x))
>>d=int(f(x))
 Create a function h(x) for the first expression
>>h(x)=f(x)*c
 Subtract the functional upper limit value and functional lower limit
value, and assign it to a variable
>>e=h(a)-h(b)
 Integrate the multiplication of derivative of f and integration of g
under upper and lower limit value, and assign it to a variable
>>i=int(d*c,a,b)
 Subtract the first expression (e) and the second expression (i), use
double function, and assign it to a random variable j.
>>j=double(e-i)
 Use if/else statement to check whether the value of y and j is equal or
not.

MATLAB Code
syms x
f(x)=x;
g(x)=log(x);
a=4

52
`

b=15
y=double(int(f(x)*g(x),a,b))

c=int(g(x));
d=diff(f(x));
h(x)=f(x)*c
e=double(h(b)-h(a))
i=double(int(d*c,a,b))
j=e-i
y
if j==y
disp('true')
else
disp('false')
end

53
`

54
`

10. SOLVIING DIFFERENTIAL EQUATIONS

10.1. SOLVING ODINARY DIFFERENTIAL EQUATIONS


Differential equation can be solved in matlab using dsolve command. The
most basic form of the dsolve command for finding the solution to a
single differential equation is dsolve(‘equation’). For the purpose of
using dsolve command, derivative are indicated with a ‘D’.
For example:
Solve y’=5y

>>dsolve(‘Dy=5*y’)

Here, MATLAB solves the equation for y with respect to a default variable ‘t’
and gives the answer:
ans=
C1*exp(5*t)
where, c1 is an arbitrary constant assigned by MATLAB
h

10.2. SOLVING DIFFERENTIAL EQUATIONS WITH


…CONDITIONS
Differential Equations with conditions can be solved easily using matlab. the
conditions an be written after the equation in dsolve command in the
following way:
dsolve(‘equation’ , ’condition1’ , ’condition2’ ,……..)
For example:
Solve y’’-y=0, y(0)=-1, y’(0)=2

>>dsolve(‘D2y-y=0’ , ‘y(0)=-1’ , ‘Dy(0)=2’)

55
`

Here, MATLAB solves the equation for y with respect to a default variable ‘t’
using the given conditions and gives the answer:
ans=
1/2*exp(t)-3/2*exp(-t)
g

10.3. SOLVING DIFFERENTIAL EQUATIONS W.R.T. OTHER


..VARIABLES
Differential equation can be solved with respect to other variables
using dsolve command in the following way:
dsolve(‘equation’ , ’condition1’ , ’condition2’ ,.....…, ‘variable’)
For example:
Solve f’’-y=0,f(0)=-1,f’(0)=2 with respect to y

>>dsolve(‘D2f-y-0’ , ’f(0)=-1’ , ‘Df(0)=2’ , ‘y’)

Here, MATLAB solves the equation for f with respect the variable ‘y’ and
gives the answer:
ans=
1/6*y^3+2*y-1

56
`

REFERENCES

Attaway, Stormy. 2019. MATLAB® A Practical Introduction to


Programming and Problem Solving . Fifth. s.l. : Katey Birtcher, 2019.
2021.
Math Works. [Online] May 2021. https://www.mathworks.com.

57

You might also like