Calculus With Matlab: A Project Work
Calculus With Matlab: A Project Work
A Project Work
Submitted to the Office of Controller of Examination
Tribhuvan University, Balkhu in the partial fulfillment of
the requirements for the degree
GROUP-5...
DEPARTMENT OF MATHEMATICS
CENTRAL SCHOOL OF MATHEMATICS
TRIBHUVAN UNIVERSITY, NEPAL
(06-21-2021) 1
`
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
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.
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
`
8
`
9
`
2. COMPUTATION OF LIMITS……..
10
`
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 :
11
`
12
`
13
`
else
disp('property 1 not verified')
end
After execution, command window displays the statement 'property 1
verified'. Thus, addition theorem is verified.
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.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…..
>> syms x
>> f=8*x^2+5*x^3;
>> diff(f)
17
`
>> 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
>> c=sym(‘5’);
>>diff(c)
18
`
>> 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
>>syms x z
>>f=x^2+9*x*z^5+z^7;
>>diff(f,z,4)
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
`
MATLAB
If the direct differentiation via MATLAB is the same as 0, we can prove that
the law stands.
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
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
`
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
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
`
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
`
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
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.
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
`
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
`
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
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
`
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
`
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.)
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.
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
`
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
`
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
`
>>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
`
41
`
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 -∫𝑎 𝑓 ′ (𝑥 ). ∫ 𝑔(𝑥 ). 𝑑𝑥
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
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
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
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
`
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
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
`
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
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
`
>>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
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
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
57