Numerical Analysis: MATLAB Practical (Autumn 2020) B.E. III Semester Thapar Institute of Engineering & Technology Patiala
Numerical Analysis: MATLAB Practical (Autumn 2020) B.E. III Semester Thapar Institute of Engineering & Technology Patiala
Numerical Analysis: MATLAB Practical (Autumn 2020) B.E. III Semester Thapar Institute of Engineering & Technology Patiala
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.
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:
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