Matlab Manual Cs and Is 201
Matlab Manual Cs and Is 201
Department of Mathematics
MATLAB MANUAL
II Semester
2022 Scheme
Prepared By:
Contents
TABLE OF CONTENTS
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 1 | 42
RV Institute of Technology & Management ®
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 2 | 42
RV Institute of Technology & Management ®
2. Put your signature on attendance sheet first, submit the record and then go to your place
3. Without fair record and lab manual you are not allowed to the lab.
4. Handlings of other things, which are not related to the experiment, are strictly prohibited inside
the lab.
5. You may be charged with full cost and additional penalty for destroying the equipment’s.
7. Till the time you are inside the lab you should stick to your place and don’t move here & there.
8. Before leaving the lab, be sure that power supply is switched off and you have returned the
components proper manner otherwise all the group members will be punished.
9. Your duly filled lab manual along with stressing sheet must be verified by any of the instructors
10. You yourself will be responsible for exchange or missing of any precious materials.
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 3 | 42
RV Institute of Technology & Management ®
1. Maintain the contain page with date of performing the experiment, experiment name, page
number.
2. Put the page numbers on the top of the individual page, Date & experiment no.
3. Write the aim of the experiment and required short theoretical description with required
5. Put your full signature at bottom right corner of the last page of that experiment.
6. Record should be very neat and clean and the entire circuits/block diagram should be drawn
FORMAT
2. Theory
4. Program
5. Output
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 4 | 42
RV Institute of Technology & Management ®
PROCEDURE:
1. Open MATLAB
MARK DISTRIBUTION
understood the above rules and regulations of the Simulation Practice on Mat lab and will accept
any kind of punishment given by the instructor for violating the rules mentioned above.
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 5 | 42
RV Institute of Technology & Management ®
LIST OF EXPERIMENTS
4. Computation of basis and dimension for a vector space and graphical representation of
linear transformation
Raphson method.
8. Computation of area under the curve using Trapezoidal, Simpson’s (1/3)rd and (3/8)th
rule
9. Solution of ODE of first order and first degree by Taylor’s series and Modified Euler’s
method
10. Solution of ODE of first order and first degree by Runge-Kutta 4th order and Milne’s
predictor-corrector method.
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 6 | 42
RV Institute of Technology & Management ®
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 7 | 42
RV Institute of Technology & Management ®
EXPERIMENT NO. – 1
AIM OF THE EXPERIMENT: Program to compute area, volume and centre of gravity.
Problem 1: Compute the area between the parabolas 𝑦 2 = 4𝑥 and 𝑥 2 = 4y and plot using
MATLAB software.
Program:
syms x
Area=int(int(1,(x^2/4),(2*sqrt(x))),0,4)
x = linspace(0,4);
y1 = x.^2/4;
y2 = 2*sqrt(x);
figure(1)
plot(x, y1)
hold on
plot(x, y2)
patch([x fliplr(x)], [y1 fliplr(y2)], 'r')
hold off
Output:
Area =16/3
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 8 | 42
RV Institute of Technology & Management ®
Problem 2: Compute the area between the curves 𝑦 2 = 4𝑥 − 𝑥 2 and 𝑦 = x and plot using
MATLAB software.
Program:
syms x
Area=int(int(1,x,(4*x-x.^2)),0,3)
x = linspace(0,3);
y1 = 4*x-x.^2;
y2 = x;
figure(1)
plot(x, y1)
hold on
plot(x, y2)
patch([x fliplr(x)], [y1 fliplr(y2)], 'g')
hold off
Output:
Area =9/2
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 9 | 42
RV Institute of Technology & Management ®
Problem 3: Compute the area between the curves 𝑦 2 = 4𝑥 and 𝑥 + 𝑦 = 3 and plot using
MATLAB software.
Program:
syms x
Area=int(int(1,(y^2/4),(3-y)),0,2)
x = linspace(0,3);
y2 = 3-x;
y1 = 2*sqrt(x);
figure(1)
plot(x, y1)
hold on
plot(x, y2)
patch([x fliplr(x)], [y1 fliplr(y2)], ‘w’)
hold off
Output:
Area =10/3
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 10 | 42
RV Institute of Technology & Management ®
Program:
clear all
clc
syms x y z
xa=0;
xb=2;
ya=0;
yb=3*(1-(x/2));
za=0;
zb=5*(1-(x/2)-(y/3));
I=int(int(int(1+0*z,z,za,zb),y,ya,yb),x,xa,xb)
Output:
Volume=5
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 11 | 42
RV Institute of Technology & Management ®
Program:
clear all
clc
syms x y z
xa=-1;
xb=1;
ya=-sqrt(1-x^2);
yb=sqrt(1-x^2);
za=-sqrt(1-x^2-y^2);
zb=sqrt(1-x^2-y^2);
I=int(int(int(1+0*z,z,za,zb),y,ya,yb),x,xa,xb)
Output:
4𝜋
Volume=
3
Exercise:
1. Compute the area of the region bounded by 𝑦 2 = 4 − 𝑥 and 𝑦 2 = 𝑥 using MATLAB software.
𝑥2 𝑦2
2. Compute the area enclosed by ellipse + = 1 using MATLAB software
4 9
3. A pyramid is bounded by three coordinates planes and the plane 𝑥 + 2𝑦 + 3𝑧 = 6. Find the
volume by triple integral using MATLAB software.
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 12 | 42
RV Institute of Technology & Management ®
4. Evaluate r sin dr d where R is the region bounded by the cardioid r = a (1 − cos ) above
R
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 13 | 42
RV Institute of Technology & Management ®
EXPERIMENT NO. – 2
7
Problem 1: Evaluate 𝛤 (2) using MATLAB software
7 5 5 5
Theory: We know that 𝛤(𝑛 + 1) = 𝑛𝛤(𝑛), hence 𝛤 (2) = 𝛤 (2 + 1) = 2 𝛤 (2)
5 3 3 3
Again 𝛤 (2) = 𝛤 (2 + 1) = 𝛤 (2)
2
7 5 3 1 1 15√𝜋
Therefore 𝛤 (2) = 2 ∗ 2 ∗ 2 ∗ 𝛤 (2) = 8
Program:
Output:
Enter positive number for gamma function Gamma (3.500000) = 3.32335
7/2
1 5
Problem 2: Compute 𝛽 ( , ) using beta and gamma relation using MATLAB software.
2 2
𝛤(𝑚)𝛤(𝑛)
Theory: we know that 𝛽 (𝑚, 𝑛) = and 𝛤(𝑛 + 1) = 𝑛! hence
𝛤(𝑚+𝑛)
1 5 3 1 1
1 5 𝛤 (2) 𝛤 (2) √𝜋 ∗ 2 ∗ 2 ∗ 𝛤 (2) 3𝜋
𝛽 ( , )= = =
2 2 𝛤(3) 2! 8
Program:
Output:
Enter m, n for Beta (m, n) function Beta (0.500000,2.500000) =1.1718
1/2 5/2
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 14 | 42
RV Institute of Technology & Management ®
Exercise:
5 7 1 1
1. Evaluate 𝛤 (2), 𝛤 (− 2), 𝛤(8), 4 𝛤 (4) using MATLAB software.
2 1 3 1 1 7
2. Evaluate 𝛽 (5 , 2), 𝛽 (11, 19), 𝛽 (4 , 4), 𝛽 (2 , 2) using MATLAB software.
3 1
1 3 1 1 3 1 𝛾( )𝛾( )
4 4
3. Compute 2 𝛤 (4) × 2 𝛤 (4) and verify 𝛽 (4 , 4) = using MATLAB software.
𝛾(1)
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 15 | 42
RV Institute of Technology & Management ®
EXPERIMENT NO. – 3
AIM OF THE EXPERIMENT: Finding gradient, divergent, curl and their geometrical
interpretation.
Theory:
𝜕𝑓 𝜕𝑓 𝜕𝑓
𝑔𝑟𝑎𝑑 𝑓 = 𝛻𝑓 = 𝐢+ 𝐣+ 𝐤
𝜕𝑥 𝜕𝑦 𝜕𝑧
= (3 ∗ cos(y) ∗ sin(z) + 2 ∗ y ∗ z ∗ cos(x))𝑖
+ ( 2 ∗ z ∗ sin(x) − 3 ∗ x ∗ sin(y) ∗ sin(z))j
+ (2 ∗ y ∗ sin(x) + 3 ∗ x ∗ cos(y) ∗ cos(z))k
Program:
syms x y z
f = 2*y*z*sin(x) + 3*x*sin(z)*cos(y);
gradient(f, [x, y, z])
Output: ans =
3*cos(y)*sin(z) + 2*y*z*cos(x)
2*z*sin(x) - 3*x*sin(y)*sin(z)
2*y*sin(x) + 3*x*cos(y)*cos(z)
Problem 2: Find the gradient of 𝑓 = 𝑥𝑦 3 + 𝑦𝑧 3 using MATLAB software.
Theory:
𝜕𝑓 𝜕𝑓 𝜕𝑓
𝑔𝑟𝑎𝑑 𝑓 = 𝛻𝑓 = 𝐢+ 𝐣+ 𝐤 = (𝑦 3 )𝑖 + (3𝑦 2 x + 𝑧 3 )j + (3𝑧 2 y)k
𝜕𝑥 𝜕𝑦 𝜕𝑧
Program:
syms x y z
f = x*y.^3+y*z.^3;
gradient(f, [x, y, z])
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 16 | 42
RV Institute of Technology & Management ®
𝜕 𝜕 𝜕 3 2
𝜕(𝑥 𝑦 𝑧)1 𝜕(𝑦 𝑧 3 2 𝑥) 𝜕(𝑧 3 𝑥 2 𝑦)
Theory: 𝑑𝑖𝑣⃗⃗⃗⃗
𝑓 = (𝜕𝑥 𝑖 + 𝜕𝑦 𝑗 + 𝜕𝑧 𝑘) ∙ (𝑓1 𝑖 + 𝑓2 𝑗 + 𝑓3 𝑘) = + 𝜕𝑦 +
𝜕𝑥 𝜕𝑧
∇ ∙ 𝑓 = 3𝑥 2 𝑦 2 𝑧 + 3𝑦 2 𝑧 2 𝑥 + 3𝑥 2 𝑧 2 𝑦
𝑖 𝑗 𝑘
𝜕 𝜕 𝜕
∇ × f = || ||
𝜕𝑥 𝜕𝑦 𝜕𝑧
𝑥3𝑦2𝑧 𝑦3𝑧2𝑥 𝑧3𝑥2𝑦
= (𝑥 2 𝑧 3 – 2𝑥𝑦 3 𝑧)𝑖 + ( 𝑥 3 𝑦 2 – 2𝑥𝑦𝑧 3 )𝑗 + (− 2𝑥 3 𝑦𝑧 + 𝑦 3 𝑧 2 )𝑘
Program:
syms x y z
V = [x^3*y^2*z, y^3*z^2*x, z^3*x^2*y];
X = [x y z];
curl(V,X)
divergence(V,X)
Problem 4: Find the divergence and curl of a vector field 𝑉(𝑥, 𝑦, 𝑧) = (𝑥 3 𝑦 2 𝑧, 𝑦 3 , 3𝑧 2 ) using
MATLAB software.
Theory:
𝜕 𝜕 𝜕 𝜕(𝑥 3 𝑦 2 𝑧)1 𝜕(𝑦 3 ) 𝜕(3𝑧 2 )
𝑑𝑖𝑣⃗⃗⃗⃗
𝑓 =( 𝑖+ 𝑗 + 𝑘) ∙ (𝑓1 𝑖 + 𝑓2 𝑗 + 𝑓3 𝑘) = + +
𝜕𝑥 𝜕𝑦 𝜕𝑧 𝜕𝑥 𝜕𝑦 𝜕𝑧
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 17 | 42
RV Institute of Technology & Management ®
∇ ∙ 𝑓 = 3𝑥 2 𝑦 2 𝑧 + 3𝑦 2 𝑥 + 6𝑧
𝑖 𝑗 𝑘
𝜕 𝜕 𝜕
∇ × f = || ||
𝜕𝑥 𝜕𝑦 𝜕𝑧
𝑥 3 𝑦 2 𝑧 𝑦 3 3𝑧 2
= (0)𝑖 + ( 𝑥 3 𝑦 2 )𝑗 + (− 2𝑥 3 𝑦𝑧 )𝑘
Program:
syms x y z
V = [x^3*y^2*z, y^3, 3*z.^2];
X = [x y z];
curl(V,X)
divergence(V,X)
Output:
ans = x^3*y^2-2*x^3*y*z
ans = 3*z*x^2*y^2 + 3*y^2 + 6*z
Exercise:
( ) ( )
→
3. Show that the vector function f = 2 xyzi + xy − y 2 z j + x 2 − zx k is solenoidal using
MATLAB software.
→ →
4. If f = xy 2 i + 2 x 2 yzj − 3 y 2 zk then find curl f using MATLAB software.
→
5. Show that f = (sin x + z )i + (cos y − z ) j + (x − y )k is irrotational using MATLAB
software.
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 18 | 42
RV Institute of Technology & Management ®
EXPERIMENT NO. – 4
AIM OF THE EXPERIMENT: Computation of basis and dimension for a vector space and
graphical representation of linear transformation
Problem 1: Determine whether the set {(1, 2, 1), (3, 4, −7), (3, 1, 5)} is a basis of 𝑉3 (𝑅),
using MATLAB software.
Theory:
1 3 3
𝐴 = [2 4 1]
1 −7 5
1 3 3
𝐴 ∼ [0 −2 −5]
0 −10 −8
1 3 3
𝐴 ∼ [0 −2 −5] Therefore, the given vectors are linearly independent.
0 0 17
Program:
Output:
The given vectors are linearly independent. Hence the given set form a basis of 𝑉3 (𝑅).
Problem 2: Find the dimension of the subspace spanned by the vectors (2, 4, 2), (1, -1, 0), (1, 2,
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 19 | 42
RV Institute of Technology & Management ®
2 1 1 0 2 1 1 0 2 1 1 0
Theory: 𝐴 = [4 −1 2 3] ∼ [0 −3 0 3] ∼ [0 −3 0 3]. Here two columns are
2 0 1 1 0 −1 0 1 0 0 0 0
Program:
Output:
The dimension of the subspace spanned by the vector is 2.
Exercise:
2. Verify whether the vectors 𝑢1 = (1, 0, 0), 𝑢2 = (0, 1, 0), 𝑢3 = (0, 0, 1)and 𝑢4 = (1, 1, 1)
in 𝑉3 (𝑅) are linearly independent set or not using MATLAB software.
3. Determine whether {(1, 1), (1, -1)} forms basis of 𝑉2 (𝑅) using MATLAB software.
4. Show that 𝑆 = {{(1, 1, 0), (0, 1, 0), (0, 1, 1)} is a basis of the vector space 𝑉3 (𝑅) using
MATLAB software.
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 20 | 42
RV Institute of Technology & Management ®
EXPERIMENT NO. – 5
Problem 1: Compute 𝑢 ∙ 𝑣 and 𝑣 ∙ 𝑢 for the vectors 𝑢 = (2, −5, −1) and 𝑣 = (3, 2, −3) using
MATLAB software
Theory:
3
𝑢 ∙ 𝑣 = [2 −5 −1] [ 2 ] = −1
−3
2
𝑣 ∙ 𝑢 = [3 2 −3] [−5] = −1
−1
Program:
u=[2 -5 -1];
v=[3 2 -3];
dot1=dot(u,v)
dot2=dot(v,u)
Output:
dot1= -1
dot2= -1
1 7
Problem 2: If 𝑢1 = (3, 1, 1); 𝑢2 = (−1, 2, 1); 𝑢3 = (− 2 , −2, 2) then show that {𝑢1 , 𝑢2 , 𝑢3 } in
𝑅 3 is an orthogonal set using MATLAB software.
Theory:
−1
𝑢1 ∙ 𝑢2 = [3 ]
1 1 2 ]=0 [
1
−0.5
𝑢2 ∙ 𝑢3 = [−1 2 1] [ −2 ] = 0
3.5
−0.5
𝑢1 ∙ 𝑢3 = [3 1 1] [ −2 ] = 0
3.5
Hence, {𝑢1 , 𝑢2 , 𝑢3 } in 𝑅 3 is an orthogonal set
Program:
u1=[3,1,1];
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 21 | 42
RV Institute of Technology & Management ®
u2=[-1,2,1];
u3=[-1/2,-2,7/2];
dot1=dot(u1,u2);
dot2=dot(u2,u3);
dot3=dot(u3, u1);
if dot1==dot2==dot3==0
fprintf('Each pair of distinct vectors is orthogonal')
else
fprintf('Each pair of distinct vectors is not orthogonal')
end
Output:
Each pair of distinct vectors is orthogonal
Exercise:
1. Show that 𝑥1 = (1, 1, 1, 1), 𝑥2 = (0, 1, 1, 1) and 𝑥3 = (0, 0, 1, 1) is orthogonal set using
MATLAB software.
2. Compute 𝑢 ∙ 𝑣 and 𝑣 ∙ 𝑢 for the vectors 𝑢 = (1, 3, −1) and 𝑢 = (2, 1, −1)
using MATLAB software.
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 22 | 42
RV Institute of Technology & Management ®
EXPERIMENT NO. – 6
Problem 1: Find by Newton’s method, the real root of the equation x 𝑒 𝑥 − 1 = 0 using
MATLAB software.
Theory:
Given x ex -1= 0.
⇒f(x) = x ex -1and 𝑓 ′ (𝑥) = xe𝑥 + 𝑒 𝑥
Since f (0) = -1<0 and f(1) =1.718 >0 , the root lies in the interval (0,1)
Let x0 = 0.3 be the initial approximation to the root.
The Newton’s iteration formula is
𝑓(𝑥𝑛 )
𝑥𝑛+1 = 𝑥𝑛 −
𝑓 ′ (𝑥𝑛 )
Put n = 0 first approximation 𝑥1 is
𝑓(𝑥 )
𝑥1 = 𝑥0 − 𝑓′ (𝑥0 ) = 0.6390
0
Program:
Solution: syms x;
f=x*exp(x)-1; %Enter the Function here
g=diff(f); %The Derivative of the Function
n=input('Enter the number of decimal places:');
epsilon = 5*10^-(n+1)
x0 = input('Enter the intial approximation:');
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 23 | 42
RV Institute of Technology & Management ®
for i=1:100
f0=vpa(subs(f,x,x0)); %Calculating the value of function at x0
f0_der=vpa(subs(g,x,x0)); %Calculating the value of function derivative at x0
y=x0-f0/f0_der; % The Formula
err=abs(y-x0);
if err<epsilon %checking the amount of error at each iteration
break
end
x0=y;
end
y = y - rem(y,10^-n); %Displaying upto required decimal places
fprintf('The Root is : %f \n',y);
fprintf('No. of Iterations : %d\n',i);
Output:
The Root is : 0.567
'No. of Iterations : 5
Problem 2: Find a root of the equation sin(x) + cos(x) + 𝑒 𝑥 −8 = 0 using Regula Falsi
method using MATLAB software.
Program:
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 24 | 42
RV Institute of Technology & Management ®
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 25 | 42
RV Institute of Technology & Management ®
a b c f(c)
2.000000 3.000000 2.010374 -0.054516
2.010374 3.000000 2.015152 -0.025119
2.015152 3.000000 2.017349 -0.011551
2.017349 3.000000 2.018358 -0.005306
2.018358 3.000000 2.018821 -0.002437
2.018821 3.000000 2.019034 -0.001119
2.019034 3.000000 2.019132 -0.000514
2.019132 3.000000 2.019177 -0.000236
2.019177 3.000000 2.019197 -0.000108
2.019197 3.000000 2.019207 -0.000050
2.019207 3.000000 2.019211 -0.000023
2.019211 3.000000 2.019213 -0.000010
Root is: 2.019214
Output:
Root is: 2.019214
Exercise:
1. Find a real root of the equation 𝑥 3 -2x-5=0 by the method of false position correct to three
decimal places using MATLAB software.
2. Find by Newton’s method, the real root of the equation 3x = cos x+1 using MATLAB
software.
3. Using Newton’s iterative method, find the real root of 𝑥log10 𝑥 = 1.2 using MATLAB
software.
4. Find the root of the equation xe𝑥 =cosx using Regula - falsi method correct to four decimal
places using MATLAB software.
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 26 | 42
RV Institute of Technology & Management ®
EXPERIMENT NO. – 7
Problem 1: The table gives the distance in nautical miles of the visible horizon for the given
heights in feet above the earth’s surface.
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 27 | 42
RV Institute of Technology & Management ®
Program:
clc
clear
x=input('Enter the values of independent variable x in an array: \n');
%x= [100:50:400]; % Write the values of independent variable x.
y=input('Enter the values of dependent variable y in an array: \n');
%y = [10.63 13.03 15.04 16.81 18.42 19.90 21.27]; % Write the values of dependent variable y.
xf=input('Enter the value of x where we want to find the value of f(x): ');
n=length(x); % Number of terms of X or Y
d=zeros(n-1);
h=x(2)-x(1); %step length
%Formula: f(u)?y(n)+u*(del)y(n-1)+u*(u+1)/2!(del)^2y(n-2)+....
%+u*(u+1)+..+(u+n-2)/(n-1)!(del)^(n-1)y(1)
%where h=x(2)-x(1)=step length and u=(xf-x(n))/h. Hree del:= forward
%difference operator.
u=(xf-x(n))/h;
for k=2:n %Calculation of first forward differences
d(k-1,1)=y(k)-y(k-1);
end
for r=2:n-1 %Calculation of second and rest forward differences
for k=1:n-r
d(k,r)= d(k+1,r-1)-d(k,r-1);
end
end
disp('The forward difference table is:')
d
s=y(n); t=u; m=n-1;
for r=1:n-1 %Calculation of result
s=s+t*d(m,r);
t=(u+r)/(r+1)*t;
m=m-1;
end
fprintf('The required value is f(%1.2f)= %3.4f',xf,s);
Output:
f(160)= 13.46 nautical miles
Problem 2: The table gives the distance in nautical miles of the visible horizon for the given
heights in feet above the earth’s surface.
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 28 | 42
RV Institute of Technology & Management ®
Program:
clc
clear
x=input('Enter the values of independent variable x in an array: \n');
%x= [100:50:400]; % Write the values of independent variable x.
y=input('Enter the values of dependent variable y in an array: \n');
%y = [10.63 13.03 15.04 16.81 18.42 19.90 21.27]; % Write the values of dependent variable y.
xf=input('Enter the value of x where we want to find the value of f(x): ');
n=length(x); % Number of terms of X or Y
d=zeros(n-1);
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 29 | 42
RV Institute of Technology & Management ®
Output:
f(410)= 21.53 nautical miles
Exercise:
1. Evaluate Apply Newton’s backward difference interpolation formula to find f(7.5) from the
following table using MATLAB software.
x 1 2 3 4 5 6 7 8
f(x) 1 8 27 64 125 216 343 512
2. Using Newton-Gregory Interpolation formulae, estimate f (0.12) from the following data.
using MATLAB software.
x 0.10 0.15 0.20 0.25 0.30
f(x) 0.1003 0.1511 0.2027 0.2553 0.3093
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 30 | 42
RV Institute of Technology & Management ®
EXPERIMENT NO. – 8
AIM OF THE EXPERIMENT: Computation of area under the curve using Trapezoidal,
Simpson’s (1/3)rd and (3/8)th rule.
𝜋
Problem 1: Compute ∫0 sinx dx by using Trapezoidal.
𝜋−0 𝜋
Theory: : h = =
6 6
x 0 𝜋 2𝜋 𝜋 4𝜋 5𝜋 6𝜋
3
6 6 6 6 6 6
y 0 0.5 0.8660 1 0.8660 0.5 0
ℎ
By Trapezoidal rule, I = [(𝑦0 + 𝑦𝑛 ] + 2 (𝑦1 +𝑦2 + 𝑦3 +……. + 𝑦𝑛−1 ) ]=1.9504
2
Program:
syms x y
x = 0:pi/100:pi;
y = sin(x);
Q = trapz(x,y)
Output:
Ans =1.998
5.2
Problem 2: Compute ∫4 logx dx by using Simpson’s 1/3 rule.
5.2−4 1.2
Theory: : h = = = 0.2
6 6
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 31 | 42
RV Institute of Technology & Management ®
= 1.82784
Program:
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 32 | 42
RV Institute of Technology & Management ®
Output:
Ans = 1.8278
1.5
Problem 3: Compute ∫0 (2 + 2x + x 2 + sin(2𝜋x) + cos(4𝜋x))dx by using Simpson’s 3/8
rule.
1.5−0
Theory: h= = 0.25
6
Program:
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 33 | 42
RV Institute of Technology & Management ®
Output:
Ans =
6.5123
Exercise:
0.6 2
1. Evaluate ∫0 𝑒 −𝑥 𝑑𝑥 by taking 6 intervals using (a) Simpsons 1/3rd rule (b) Simpsons 3/8th
rule using MATLAB software.
6 𝑑𝑥
2. Evaluate ∫0 using (a) Simpsons 1/3rd rule (b) Simpsons 3/8th rule c) Trapezoidal rule
1+𝑥 2
using MATLAB software.
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 34 | 42
RV Institute of Technology & Management ®
EXPERIMENT NO. – 9
AIM OF THE EXPERIMENT: Solution of ODE of first order and first degree by Taylor’s
series and Modified Euler’s method.
𝑑𝑦
Problem 1: Use Taylor’s series method to find y at the points 𝑥 0 1 given that = 𝑥2 + 𝑦2
= . 𝑑𝑥
Program:
syms x
g = x^2 +y^2;
t = taylor(g, 'ExpansionPoint', 2, 'Order', 12);
t = simplify(t);
x=0;y=1;
Y=t
Output:
Y=1.11145
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 35 | 42
RV Institute of Technology & Management ®
dy
Problem 2: Solve = 1 − y with the initial condition y(0)=0 using Euler's modified method
dx
and find y(0.1), using MATLAB software
Theory:
𝒅𝒚
Given 𝒅𝒙 = 𝟏 − 𝒚 = 𝑓(𝑥, 𝑦) , 𝑥0 = 0, 𝑦0 = 0, ℎ = 0.1.
0.1
=0+ [1 − 0 + 1 − 0.09525]
2
= 0.0952375
∴ The approximate value of y is 0.0952.
Program:
clear all;
clc;
df = @(x, y) 1-y;
% for calculating value of dy/dx at some particular pt using modified euler's method
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 36 | 42
RV Institute of Technology & Management ®
n =ceil( (x1-x0)/sqrt(tol));
h = ( x1 - x0)/n;
%loop for calculating values
for k = 1 : n
X(1,1) = x0; Y (1,1) = y0;
X( 1, k+1) = X(1,k) + h;
y_t = Y(1,k) + h* feval( df , X(1,k) , Y(1,k));% Eular's formula
Y(1 ,k+1) = Y(1,k) + h/2* (feval( df , X(1,k) , Y(1,k)) + feval( df , X(1,k) + h , y_t ) ) ;
%improving results obtained by modified Eular's formula
while abs( Y(1,k+1) - y_t ) > h
y_t = Y(1,k) + h*feval( df , X(1,k) , Y(1,k+1));
Y( 1 ,k+1) = Y(1,k) + h/2* (feval( df , X(1,k) , Y(1,k)) + feval( df , X(1,k) + h , y_t ) );
end
end
%displaying results
Output:
The approximate value of y is 0.09513
Exercise:
1. Evaluate using Modified Euler’s method, find an approximate value of y when x = 0.2,
𝑑𝑦
given that 𝑑𝑥 = 𝑥 + 𝑦 &𝑦 = 1when x = 0. Take h = 0.1 using MATLAB software.
𝑑𝑦
2. Given = 3𝑦 + 𝑒 𝑥 , 𝑦(0) = 1. Take ℎ = 0.1 and compute 𝑦(0.2) by Modified Euler’s
𝑑𝑥
𝑑𝑦
4. Obtain the Taylor’s series solution of the problem = 𝑦 𝑠𝑖𝑛 𝑥 + 𝑐𝑜𝑠 𝑥 , 𝑦(0) =
𝑑𝑥
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 37 | 42
RV Institute of Technology & Management ®
EXPERIMENT NO. – 10
AIM OF THE EXPERIMENT: Solution of ODE of first order and first degree by Runge-Kutta
4th order and Milne’s predictor-corrector method.
𝑑𝑦
Problem 1: Solve = 1.5𝑦 − 0.0005 𝑦 2 , 𝑦(0) = 10 using Runge-Kutta 4th order method in
𝑑𝑡
MATLAB.
𝑘1 = ℎ𝑓(𝑥0 , 𝑦0 ) = 1.495
1 1
𝑘2 = ℎ𝑓 (𝑥0 + ℎ, 𝑦0 + 𝑘1 ) = 0.1 × 𝑓(0.05, 10.7475) = 0.1 × (0.05 + 1.052 ) = 1.6063
2 2
1 1
𝑘3 = ℎ𝑓 (𝑥0 + ℎ, 𝑦0 + 𝑘2 ) = 0.1 × 𝑓(0.05, 10.80315) = 1.614637
2 2
𝑘4 = ℎ𝑓(𝑥0 + ℎ, 𝑦0 + 𝑘3 ) = 0.1 × 𝑓(0.1, 11.6164) = 1.735712
1
Then𝑘 = 6 (𝑘1 + 2𝑘2 + 2𝑘3 + 𝑘4 ) =1.612095
Program:
clear all;
clc;
h=0.1; % step size
x(1) = 0;
y(1) = 10; % initial condition
F_xy = @(x,y) 1.5*y - 0.0005*y^2; % change the function as you desire
k_1 = h*F_xy(x(1),y(1))
k_2 = h*F_xy(x(1)+0.5*h,y(1)+0.5*k_1)
k_3 = h*F_xy((x(1)+0.5*h),(y(1)+0.5*k_2))
k_4 = h*F_xy((x(1)+h),(y(1)+k_3))
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 38 | 42
RV Institute of Technology & Management ®
Y = 11.6121.
𝑑𝑦
Problem 2: Solve = 1.5𝑦 − 0.0005 𝑦 2 , 𝑦(0) = 10 using Milne’s predictor-corrector method
𝑑𝑡
in MATLAB.
Program:
clear all;
clc;
syms x y F
F =1.5*y -0.0005*y^2;
x_val = input('Enter the values of x: \n');
y_val = input('Enter the values of y: \n');
h = x_val(2) - x_val(1);
for i = 1:3
z_dash(i+1)=subs(F , [x , y] , [x_val(i+1) , y_val(i+1)])
end
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 39 | 42
RV Institute of Technology & Management ®
Output:
y4_P = 18.17061
y4_C = 18.17157
Exercise:
dy
1. Use Runge – Kutta fourth order method to solve = x 2 + y 2 & y(1) = 1.5 to find y for x =
dx
1.1 using MATLAB software.
2. Apply Runge – Kutta fourth order method to find an approximate value of y for x = 0.1 in steps
𝑑𝑦
of 0.1, if 𝑑𝑥 = 𝑥 + 𝑦 2 , given that y = 1 when x = 0 using MATLAB software.
𝑑𝑦
3. Using Milne’s method find y at x = 0.4 given that 𝑑𝑥 = 𝑥𝑦 + 𝑦 2 , 𝑦(0) = 1, 𝑦(0.1) = 1.1169,
II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 40 | 42