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

Numerical Analysis: MATLAB Practical (Autumn 2020) B.E. III Semester Thapar Institute of Engineering & Technology Patiala

This document presents the numerical analysis of solving ordinary differential equations using the Modified Euler's Method and the Runge-Kutta Fourth Order Method. It provides the algorithms, MATLAB code, and an input/output table comparing the results of applying each method to equations for problems y'=-y+2cos(x) and y'=(x+y) with initial conditions.

Uploaded by

Aarohan Verma
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)
64 views

Numerical Analysis: MATLAB Practical (Autumn 2020) B.E. III Semester Thapar Institute of Engineering & Technology Patiala

This document presents the numerical analysis of solving ordinary differential equations using the Modified Euler's Method and the Runge-Kutta Fourth Order Method. It provides the algorithms, MATLAB code, and an input/output table comparing the results of applying each method to equations for problems y'=-y+2cos(x) and y'=(x+y) with initial conditions.

Uploaded by

Aarohan Verma
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/ 6

NUMERICAL ANALYSIS

MATLAB Practical (Autumn 2020)


B.E. III Semester
Thapar Institute of Engineering & Technology Patiala

Name: Aarohan Verma


Roll No.: 101903497
Group: COE19
Instructor: Mrs. Mamta Gulati
(ASSIGNMENT 4- Modified Euler’s and
Runge Kutta Fourth Order Method)

Algorithms:
Modified Euler’s Method:
Given: a, b, f(x, y), either h or N, y(a) = yo
Step 1: Assign x(1) = a, y(1) = yo and find h or N and f(x, y)
Step 2: Assign 1 to i
Start the loop and continue till i<=N
Assign h*f(x(i), y(i)) to k1 and h*f(x(i)+h, y(i) + k1) to k2
Assign x(i) + h to x(i+1) and y(i) + (k1+k2)/2 to y(i+1)
Increment the value of i by 1
End of loop
Step 3: Print x and y array.

Runge-Kutta Order Four Method:


Step 1: Assign x(1) = a, y(1) = yo and find h or N and f(x, y)
Step 2: Assign 1 to i
Start the loop and continue till i<=N
Assign h*f(x(i), y(i)) to k1 and h*f(x(i)+h/2, y(i) + k1/2) to k2
Assign h*f(x(i) + h/2, y(i) + k2/2) to k3 and h*f(x(i)+h, y(i) + k3) to k4
Assign x(i) + h to x(i+1) and y(i) + (k1+2*k2 + 2*k3 + k4)/6 to y(i+1)
Increment the value of i by 1
End of loop
Step 3: Print x and y array.
CODE:

Modified Euler’s Method:

clear all
clc
f = @(x,y) (-y+2*cos(x));
a = 0;
b = 1;
h = 0.2;
N = (b-a)/h;
x(1) = a;
y(1) = 1;
for i=1:N
k1 = h*f(x(i), y(i));
k2 = h*f(x(i)+h, y(i)+k1);
x(i+1) = x(i) + h;
y(i+1) = y(i) + (k1+k2)/2;
end
fprintf("x y\n");
for i=1:N
fprintf("%f %f\n",x(i),y(i));
end
Runge-Kutta Order Four Method:

clear all
clc
f = @(x,y) (-y+2*cos(x));
a = 0;
b = 1;
h = 0.2;
N = (b-a)/h;
x(1) = a;
y(1) = 1;
for i=1:N
k1 = h*f(x(i), y(i));
k2 = h*f(x(i)+h/2, y(i)+k1/2);
k3 = h*f(x(i)+h/2, y(i)+k2/2);
k4 = h*f(x(i)+h, y(i)+k3);
x(i+1) = x(i) + h;
y(i+1) = y(i) + (k1 + 2*k2 + 2*k3 + k4)/6;
end
fprintf("x y\n");
for i=1:N+1
fprintf("%f %f\n",x(i),y(i));
end
Input & Output Table:

Input Modified Euler’s Runge-Kutta Order Four


Method Method
3(a) x y x y
y’ = -y +2*cos(x) 0.000000 1.000000 0.000000 1.000000
y(0) = 1 0.200000 1.176013 0.200000 1.178732
0.400000 1.305354 0.400000 1.310472
0.600000 1.382827 0.600000 1.389968
0.800000 1.405313 0.800000 1.414051
1.000000 1.371890 1.000000 1.381759

3(b) x y x y
y’ = √𝑥 + 𝑦 0.000000 0.800000 0.000000 0.800000
0.200000 0.998019 0.200000 0.999291
y(0) = 0.8 0.400000 1.234632 0.400000 1.236964
0.600000 1.507064 0.600000 1.510332
0.800000 1.813386 0.800000 1.817506
1.000000 2.152153 1.000000 2.157066

You might also like