0% found this document useful (0 votes)
193 views42 pages

Matlab Manual Cs and Is 201

Uploaded by

ks753159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
193 views42 pages

Matlab Manual Cs and Is 201

Uploaded by

ks753159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Rashtreeya Sikshana Samithi Trust

RV Institute of Technology and Management


(Institution Affiliated to VTU, Belagavi)

JP Nagar, Bengaluru - 560076

Department of Mathematics

Course Name: Mathematics-2 for Computer Science and


Engineering Stream
Course Code: BMATS201

MATLAB MANUAL

II Semester
2022 Scheme

Prepared By:

Dr. Gangadharaiah YH Dr. Sivasankar S Dr. Naghabhushana P Dr. Sushma M P


Professor Assistant Professor Assistant Professor Assistant Professor
RV Institute of Technology & Management ®

Contents
TABLE OF CONTENTS

RULES & REGULATIONS ................................................................................... 3

FORMAT FOR RECORD WRITING ..................................................................... 4

MARK DISTRIBUTION ....................................................................................... 5

LIST OF EXPERIMENTS ...................................................................................... 15

EXPERIMENT NO. -1 .................................................................................... 17

EXPERIMENT NO. -2 .................................................................................... 25

EXPERIMENT NO. - 3 ...................................................................................... 27

EXPERIMENT NO. - 4 ...................................................................................... 31

EXPERIMENT NO. - 5 ................................................................................... 35

EXPERIMENT NO. - 6 ................................................................................... 39

EXPERIMENT NO. - 7 ...................................................................................... 43

EXPERIMENT NO. - 8 ...................................................................................... 45

EXPERIMENT NO. - 9 ................................................................................... 51

EXPERIMENT NO. - 10 ................................................................................... 55

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 ®

RULES & REGULATIONS

1. Don’t be late to the lab.

2. Put your signature on attendance sheet first, submit the record and then go to your place

correspondence to experiment kit.

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.

6. You should put on your own identity card.

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

present there else your record will not be considered.

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 ®

FORMAT FOR RECORD WRITING

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

circuit/block diagram, observation and conclusion.

4. Attached the printout sheet to the left side of the paper.

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

with pencil only.

7. Bad hand writing will affect the record mark.

8. Incomplete record will lead towards negative marking

9. Data on lab manual should match with fair record.

10. Don’t submit loose sheets without record at any condition.

FORMAT

1. Aim of the experiment

2. Theory

3. Circuit simulation diagram /block diagram (if any)

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

2. Open new M-file

3. Type the program

4. Save in current directory

5. Compile and Run the program

6. For the output see command window/Figure window

MARK DISTRIBUTION

Sl. No. Parts Marks


2 Record 15
3 Experiment 15
4 Test 10
TOTAL 40

I hereby Mr./Ms………………………………………………..with USN… ......................... have

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.

Date: Signature of the student

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

EXP. LIST OF EXPERIMENT


NO.
1. Program to compute area, surface area, volume and centre of gravity

2. Evaluation of improper integrals

3. Finding gradient, divergent, curl and their geometrical interpretation

4. Computation of basis and dimension for a vector space and graphical representation of

linear transformation

5. Computing the inner product and orthogonality

6. Solution of algebraic and transcendental equations by Regula-Falsi and Newton-

Raphson method.

7. Interpolation/Extrapolation using Newton’s forward and backward difference formula

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.

Theory: Area of the plane region R is given by Area= ∬𝑅 𝑑𝑥𝑑𝑦


4 √4y 4 y2 4 y2 16
Area = ∫0 ∫y2 dx dy= ∫0 x √4ydy= ∫0 [√4y- 4 ] dy=
4 3
4

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.

Theory: Area of the plane region R is given by Area= ∬𝑅 𝑑𝑥𝑑𝑦


3 4𝑥−𝑥 2 3
Area = ∫𝑥−0 ∫𝑦=𝑥 𝑑𝑥𝑑𝑦 = ∫𝑥−0(3𝑥 − 𝑥 2 )𝑑𝑥 = 9/2

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.

Theory: Area of the plane region R is given by Area= ∬𝑅 𝑑𝑥𝑑𝑦


2 𝑦−3 2 10
Area = ∫0 ∫𝑦2/4 𝑑𝑥𝑑𝑦 = ∫0 (𝑦 − 3 − 𝑦 2 /2)𝑑𝑦 = 3

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 ®

Problem 4: Compute the volume of the tetrahedron bounded by the plane, x = 0, y = 0, z =


x y z
0, 2 + 3 + 5 = 1 using MATLAB software.

Theory: Volume V is given by Volume= ∭𝑉 𝑑𝑥𝑑𝑦𝑑𝑧.


𝑥 𝑦
5(1− − )
𝑥 2 3 𝑥
2 3(1− ) 2 3(1− )
2 2 𝑥 𝑦
Volume = ∫ ∫ ∫ 𝑑𝑧𝑑𝑦𝑑𝑥 = ∫ ∫ 5(1 − − )𝑑𝑧𝑑𝑦 = 5
0 𝑦=0 0 𝑦=0 2 3
0

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 ®

Problem 5: Compute by triple integration, the volume of the sphere 𝑥 2 + 𝑦 2 + 𝑧 2 = 1 using


MATLAB software.

Theory: Volume V is given by Volume= ∭𝑉 𝑑𝑥𝑑𝑦𝑑𝑧.


√1−𝑥 2 −𝑦 2
1 √1−𝑥 2 1 √1−𝑥 2
4𝜋
Volume = ∫ ∫ ∫ 𝑑𝑧𝑑𝑦𝑑𝑥 = ∫ ∫ √1 − 𝑥 2 −𝑦 2 𝑑𝑧𝑑𝑦 =
0 𝑦=0 0 𝑦=0 3
0

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

the initial line using MATLAB software.


5. Compute area between y = x2 and the line y = x using MATLAB software.
x2 y 2
6. Compute the area of an ellipse 2 + 2 = 1 bounded in positive quadrant using MATLAB
a a
software.
7. Compute the area of a circle x2 + y 2 = 1 bounded in positive quadrant using MATLAB
software.
 r dr d where R is the region bounded by the circles r = 2sin  & r = 4sin  .
3
8. Evaluate
R

above the initial line using MATLAB software.

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

AIM OF THE EXPERIMENT: Evaluation of Beta and Gamma functions

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:

n=input('Enter positive number for gamma function');


y=gamma(n);
fprintf('Gamma(%f)=%g',n,y)

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:

m=input('Enter m, n for Beta(m,n) function');


n1=input('')
z=beta(m,n1);
fprintf('Beta(%f,%f)=%g',m,n1,z)

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.

Problem 1: Find the gradient of 𝑓 = 2𝑦𝑧𝑠𝑖𝑛(𝑥) + 3𝑥𝑠𝑖𝑛(𝑧)cos(𝑦) using MATLAB software.

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 ®

Output: ans = y^3


3*x*y^2 + z^3
3*y*z^2

Problem 3: Find the divergence and curl of a vector Field 𝑉(𝑥, 𝑦, 𝑧) = (𝑥 3 𝑦 2 𝑧, 𝑦 3 𝑧 2 𝑥, 𝑧 3 𝑥 2 𝑦)


using MATLAB software.

𝜕 𝜕 𝜕 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)

Output: ans = x^2*z^3 - 2*x*y^3*z


x^3*y^2 - 2*x*y*z^3
- 2*x^3*y*z + y^3*z^2
ans =
3*x^2*y^2*z + 3*x^2*y*z^2 + 3*x*y^2*z^2

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:

1. Evaluate gradient of the following scalar function 𝑓(𝑥, 𝑦, 𝑧) = 𝑥 2 𝑦 2 + 𝑥𝑦 2 − 𝑧 2 at


(3,1,1) using MATLAB software
→ xi + yj →
2. If f = then find div f using MATLAB software.
x+ y

( ) ( )

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

Operating, R′2 → 𝑅2 − 2𝑅1 , 𝑅3′ → 𝑅3 − 𝑅1 ,

1 3 3
𝐴 ∼ [0 −2 −5]
0 −10 −8

Operating, R′3 → 𝑅3 − 5𝑅2 ,

1 3 3
𝐴 ∼ [0 −2 −5] Therefore, the given vectors are linearly independent.
0 0 17

Hence the given set is a basis of 𝑉3 (𝑅).

Program:

A=[1 2 1; 3 4 -7; 3 1 5];


b= det(A);
if b==0
fprintf('The given vectors are linearly dependent. Hence the given set is not a basis of V3(R)')
else
fprintf('The given vectors are linearly independent. Hence the given set is a basis of V3(R)')
end

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,

1), and (0, 3, 1) in 𝑉3 (𝑅), using MATLAB software

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

independent. Hence the dimension is 2.

Program:

A=[2 4 2; 1 -1 0; 1 2 1;0 3 1];


b= rank(A);
fprintf('The dim of the subspace spanned by the vector is %f \n',b)

Output:
The dimension of the subspace spanned by the vector is 2.

Exercise:

1. Determine whether 𝑢1 = (1, 1, 1), 𝑢2 = (1, 2, 3), 𝑢3 = (1, 5, 8) spans 𝑉3 (𝑅)


using MATLAB software.

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

AIM OF THE EXPERIMENT: Computing the inner product and orthogonality.

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

AIM OF THE EXPERIMENT: Solution of algebraic and transcendental equations by Newton-


Raphson method and Regula-Falsi Method.

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

Put n = 1 second approximation 𝑥2 is


𝑓(𝑥 )
𝑥2 = 𝑥1 − 𝑓′ (𝑥1 ) = 0.5711
1

Put n = 2 third approximation 𝑥3 is


𝑓(𝑥 )
𝑥3 = 𝑥2 − 𝑓′ (𝑥2 ) = 0.5671
2
Put n = 3 third approximation 𝑥3 is
𝑓(𝑥 )
𝑥4 = 𝑥3 − 𝑓′ (𝑥3 ) = 0.5671
3

Hence, x = 0.5671 is the approximate root.

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.

Theory: Let 𝑓(𝑥) = sin(x) + cos(x) + 𝑒 𝑥 −8


𝑓(1) = −𝑣𝑒, 𝑓(2) = −𝑣𝑒 𝑎𝑛𝑑 𝑓(3) = +𝑣𝑒
Therefore, a root lies between 2 and 3
Now, 𝑥0 = 2 , 𝑥1 = 3 , 𝑓(𝑥0 ) = −0.1177 , 𝑓(𝑥1 ) = 11.236
By Regular-Falsi method
𝒙 𝒇(𝒙 )−𝒙𝟏 𝒇(𝒙𝟎 )
𝒙𝟐 = 𝟎 𝒇(𝒙𝟏 )−𝒇(𝒙
𝟏 𝟎)
= 2.0103
𝑓(𝑥2 ) = 𝑓(2.0103) = -ve
⇒ the root lies between 2.0103 and 3
𝑥0 𝑓(𝑥1 ) − 𝑥1 𝑓(𝑥0 )
𝑥3 = = 2.0151
𝑓(𝑥1 ) − 𝑓(𝑥0 )
Repeating this process, the successive approximations are
𝑥4 = 2.017349,𝑥5 = 2.018358
Hence the root is 2.019214correct to 4 decimal places.

Program:

clc % Clearing Screen


% Setting x as symbolic variable
syms x;
% Input Section

II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 24 | 42
RV Institute of Technology & Management ®

y = input('Enter non-linear equations: ');


a = input('Enter first guess: ');
b = input('Enter second guess: ');
e = input('Tolerable error: ');
% Finding Functional Value
fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));
% Implementing Bisection Method
if fa*fb > 0
disp('Given initial values do not bracket the root.');
else
c = a - (a-b) * fa/(fa-fb);
fc = eval(subs(y,x,c));
fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\n');
while abs(fc)>e
fprintf('%f\t%f\t%f\t%f\n',a,b,c,fc);
if fa*fc< 0
b =c;
fb = eval(subs(y,x,b));
else
a =c;
fa = eval(subs(y,x,a));
end
c = a - (a-b) * fa/(fa-fb);
fc = eval(subs(y,x,c));
end
fprintf('\nRoot is: %f\n', c);
end
Output:
Enter non-linear equations: sin(x)+cos(x)+exp(x)-8
Enter first guess: 2
Enter second guess: 3
Tolerable error: 0.00001

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

AIM OF THE EXPERIMENT: Interpolation/Extrapolation using Newton’s forward and


backward difference formula.

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.

x =height: 100 150 200 250 300 350 400

y=distance: 10.63 13.03 15.04 16.81 18.42 19.90 21.27

Find the values of y when x=160ft using MATLAB software.

Theory: The forward difference table is


x y First Second Third Fourth
difference difference difference difference
100 10.63
2.40
150 13.03 -0.39
2.01 0.15
200 15.04 -0.24 -0.07
1.77 0.08
250 16.81 -0.16 -0.05
1.61 0.03
300 18.42 -0.13 -0.01
1.48 0.02
350 19.90 -0.11
1.37
400 21.27

𝑥0 =160, 𝑦0 =13.03, ∆𝑦0 = 2.01, ∆2 𝑦0 = -0.24, ∆3 𝑦0 =0.08, ∆4 𝑦0 =-0.05 , h=50


𝑥−𝑥0 10
𝑝= ℎ
= 50
=0.2
Using Newton’s forward interpolation formula we get
𝑝(𝑝−1) 𝑝(𝑝−1)(𝑝−2)
f(160) = 𝑦0 + p∆𝑦0 + + ∆2 𝑦0 + ∆3 𝑦0 +…….
2! 3!
f(160)=13.03+0.402+0.0192+0.00384+0.00168
=13.46 nautical miles.

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 ®

x =height: 100 150 200 250 300 350 400

y=distance: 10.63 13.03 15.04 16.81 18.42 19.90 21.27

Find the values of y when x=410ft using MATLAB software.

Theory: The difference table is


x y First Second Third Fourth
difference difference difference difference
100 10.63
2.40
150 13.03 -0.39
2.01 0.15
200 15.04 -0.24 -0.07
1.77 0.08
250 16.81 -0.16 -0.05
1.61 0.03
300 18.42 -0.13 -0.01
1.48 0.02
350 19.90 -0.11
1.37
400 21.27

x=410, 𝑥𝑛 =400, 𝑦𝑛 =21.27, ∇𝑦𝑛 =1.37,∇2 𝑦𝑛 =-0.11, ∇3 𝑦𝑛 =0.02,h=50


𝑥−𝑥𝑛 10
𝑝= ℎ
= 50
=0.2
Using Newton’s backward interpolation formula we get,
𝑝(𝑝+1) 𝑝(𝑝+1)(𝑝+2)
f(410) = 𝑦𝑛 + p∇𝑦𝑛 + ∇2 𝑦𝑛 + ∇3 𝑦𝑛 …….
2! 3!
0.2(1.2)
f(410)=21.27+0.2(1.37)+ 2
(-0.11)+…………….
=21.53 nautical miles.

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 ®

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(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

x 4 4.2 4.4 4.6 4.8 5.0 5.2


y 1.386 1.43508 1.48160 1.52605 1.5686 1.6094 1.6486

By Simpson’s 1/3rd rule

I= ℎ3⌊[(𝑦0 + 𝑦6 ) + 4(𝑦1 + 𝑦3 + 𝑦5 ) + 2(𝑦2 + 𝑦4 )]⌋


= 0.2
3
[(1.38629 + 1.6486) + 4(1.43508 + 1.52605 + 1.60943) + 2(1.48160 + 1.56861)]
0.2
= [3.034953 + 18.28224 + 6.10042]
3
0.2
= (27.41761)
3

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:

% MATLAB code for syms function that creates a variable


% dynamically and automatically assigns
% to a MATLAB variable with the same name
syms x
% Lower Limit
a = 4;
% Upper Limit
b = 5.2;
% Number of Segments
n = 6;
% Declare the function
f1 = log(x);
% inline creates a function of string containing in f1
f = inline(f1);
% h is the segment size
h = (b - a)/n;
% X stores the summation of first and last segment
X = f(a)+f(b);
% variables Odd and Even to store
% summation of odd and even
% terms respectively
Odd = 0;
Even = 0;
for i = 1:2:n-1
xi=a+(i*h);
Odd=Odd+f(xi);
end
for i = 2:2:n-2
xi=a+(i*h);
Even=Even+f(xi);
end
% Formula to calculate numerical integration

II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 32 | 42
RV Institute of Technology & Management ®

% using Simpsons 1/3 Rule


I = (h/3)*(X+4*Odd+2*Even);
disp('The approximation of above integral is: ');
disp(I);

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

x 0 0.25 0.5 0.75 1.0 1.25 1.5


y 3 2.5625 4.25 2.0625 6 6.0625 8.25

Simpson’s 3/8th Rule


3ℎ
= [(𝑦0 + 𝑦6 ) + 3(𝑦1 + 𝑦2 + 𝑦4 + 𝑦5 ) + 2𝑦3 )] = 6.5123
8

Program:

f = @(x) 2 + 2.*x + x.^2 + sin(2*pi()*x) + cos(4*pi()*x);


a = 0;
b = 1.5;
n = 3;
h = (b-a)/(3*n); %3h = (b-a)/n
x1 = [a:h:b];
% Exact Integral
I = integral(f,a,b);
xplot = linspace(a,b);
yactual = f(xplot);
% Numerical Integral
IS2=0;
for i=1:n

II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 33 | 42
RV Institute of Technology & Management ®

IS2 = IS2+(f(a+(3*i-3)*h) + 3*f(a+(3*i-2)*h) + 3*f(a+(3*i-1)*h) + f(a+(3*i)*h))*3*h/8;


end
eS2 = I - IS2;
disp('Ans =')
disp(IS2)

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
= . 𝑑𝑥

y(0)=1 using MATLAB software

Theory: Here 𝑥0 = 0, the Taylor’s series solution of the given problem is


𝑥2 𝑥3 𝑥4
𝑦 = 𝑦(0) + 𝑥𝑦 ′ (0) + 𝑦 ′′ (0) + 𝑦 ′′′ (0) + 𝑦 𝑖𝑣 (0)+. . . . . with y(0) = 1…. (i)
2! 3! 4!
′ 2 2 '' ′ ''' ′′ ′ 2 iv ′′′ ′ ′′
Also, 𝑦 = 𝑥 + 𝑦 , y = 2𝑥 + 2𝑦𝑦 , y = 2 + 2{𝑦𝑦 + (𝑦 ) }, y = 2{𝑦𝑦 + 3𝑦 𝑦 } and so on.
Using the condition y(0) = 1 in these we obtain,
𝑦 ′ (0) = 1, y '' (0) = 2, y ''' (0) = 8, y iv (0) = 28.
Using these and the condition y(0) = 1 in (i) we get,
4 7
𝑦 = 1 + 𝑥 + 𝑥 2 + 𝑥 3 + 𝑥 4 +. . . . . . (ii)
3 6
Under the given condition𝑦(𝑥0 ) = 𝑦(0) = 1, this is the Taylor’s series solution for y at a point x
in a neighbourhood of 𝑥0 = 0.
For𝑥 = 𝑥1 = 0.1, expression (ii) yields the solution at 𝑥1 = 0.1 as,
4 7
𝑦(0.1) = 1 + (0.1) + (0.1)2 + (0.1)3 + (0.1)4 +. . . . . . . ≈ 1.11145
3 6

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.

Let us first find y(0.1) as a first approximation using Euler's method.


(0)
∴ 𝑦1 = 𝑦0 + ℎ𝑓(𝑥0, 𝑦0 )
= 0 + 0.1(1 − 0) = 0.1
(1) ℎ (0)
Apply Euler's modified method , 𝑦1 = 𝑦0 + 2 [𝑓(𝑥0 , 𝑦0 ) + 𝑓(𝑥1 , 𝑦1 )]
0.1
=0+ [1 − 0 + 1 − 0.1]
2
= 0.095
(2) ℎ (1)
Second iteration, 𝑦1 = 𝑦0 + 2 [𝑓(𝑥0 , 𝑦0 ) + 𝑓(𝑥1 , 𝑦1 )]
0.1
= 0+ [1 − 0 + 1 − 0.095]
2
= 0.09525
(3) ℎ (2)
Third iteration, 𝑦1 = 𝑦0 + [𝑓(𝑥0 , 𝑦0 ) + 𝑓(𝑥1 , 𝑦1 )]
2

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

% asking intial conditions

x0 = input('Enter initial value of x : ');


y0 = input ('Enter initial value of y : ');
x1 = input('Enter thevalue of x at which y is to be calculated : ');

II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 36 | 42
RV Institute of Technology & Management ®

tol = input('Enter desired level of accuracy in the final result : ');


% calaulating the value of h

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

fprintf( 'for \t x = %g \n \ty = %g \n' , x1,Y(1,n+1))

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
𝑑𝑥

method using MATLAB software.


3. By using Taylor’s series method find an approximate solution at 𝑥 = 1.02of the initial –
value problem 𝑦 ′ = 𝑥𝑦 − 1, 𝑦(1) = 2using MATLAB software.

𝑑𝑦
4. Obtain the Taylor’s series solution of the problem = 𝑦 𝑠𝑖𝑛 𝑥 + 𝑐𝑜𝑠 𝑥 , 𝑦(0) =
𝑑𝑥

0using MATLAB software.

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.

Theory: Here 𝑓(𝑥, 𝑦) = 1.5𝑦 − 0.0005 𝑦 2 , x0 = 0, 𝑦0 = 10 & ℎ = 0.1. Then

𝑘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

Hence𝑦1 = 𝑦(0.1) = 𝑦0 + 𝑘 = 10 + 1.612095 = 11.61209.

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))

disp('The requires solution is:')


Y = y(1) + (1/6)*(k_1+2*k_2+2*k_3+k_4) % main equation

II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 38 | 42
RV Institute of Technology & Management ®

Output: The requires solution is:

Y = 11.6121.

𝑑𝑦
Problem 2: Solve = 1.5𝑦 − 0.0005 𝑦 2 , 𝑦(0) = 10 using Milne’s predictor-corrector method
𝑑𝑡

in MATLAB.

Theory: Here 𝑓(𝑥, 𝑦) = 1.5𝑦 − 0.0005 𝑦 2 , 𝑥0 = 0, 𝑦0 = 10, x1 = 0.1, 𝑦1 = 11.61, x2 = 0.2,

𝑦2 = 13.4829, x3 = 0.3, 𝑦3 = 15.6543 obtained by Taylor’s series

 𝑓0 = 𝑓(𝑥0 , 𝑦0 ) = 14.95 𝑓2 = 𝑓(𝑥2 , 𝑦2 ) = 20.133


𝑓1 = 𝑓(𝑥1 , 𝑦1 ) = 17.34 𝑓3 = 𝑓(𝑥3 , 𝑦3 ) = 23.3589
Now using predictor formula, we get,
(𝑝) 4ℎ 4 × 0.1
𝑦4 = 𝑦0 + (2𝑓1 − 𝑓2 + 2𝑓3 ) = 10 + (2 × 17.34 − 20.133 + 2 × 23.3589)
3 3
= 18.1686
(𝑝)
Now𝑓4 = 𝑓(0.4, 18.1686) = 27.0878.
Again, using corrector formula, we get
(𝑐) ℎ (𝑝) 0.1
𝑦4 = 𝑦2 + (𝑓2 + 4𝑓3 + 𝑓4 ) = 13.4829 + (20.133 + 4 × 23.3589 + 18.1686)
3 3
= 18.17157

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 ®

%CALCULATING PREDICTOR VALUES%


x4_P=h+x_val(4);
y4_P = y_val(1) + 4*h*(2*z_dash(2) - z_dash(3) + 2*z_dash(4))/3;
z_dash(5)=subs(F , [x, y] , [x4_P , y4_P])

%CALCULATING CORRECTOR VALUES%


y4_C = y_val(3) + h*(z_dash(3) + 4*z_dash(4) + z_dash(5))/3;
%PRINTING VALUES%
fprintf('y4_P = %.5f \n', y4_P);
fprintf('y4_C = %.5f \n', y4_C);

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,

𝑦(0.2) = 1.2773&𝑦(0.3) = 1.504 using MATLAB software.


𝑑𝑦 2𝑥𝑦
4. Obtain y(0.5) for the IVP = 1+𝑥 2 , y(0.1) = 1, y(0.2) = 1.0199, y(0.3) =
𝑑𝑥

1.04 and y(0.4) = 1.0603, by Milne’s method using MATLAB software.

II-Semester: MATLAB Manual-Mathematics-II for Computer Science and Engineering Stream (BMATS201)
P a g e 40 | 42

You might also like