Numerical 3
Numerical 3
function y1 = fn(x,y)
y1 = x.^2 + y;
end
4th Order Runge-Kutta Method
clc
clear all
x0 = 0;
y0 = 1;
h = .05;
xn = input(' enter the value of the point ');
n = (xn - x0) / h;
for i = 1 : n
k1 = h*fn(x0,y0);
k2 = h*fn(x0+h/2,y0+k1/2);
k3 = h*fn(x0+h/2,y0+k2/2);
k4 = h*fn(x0+h,y0+k3);
y1 = y0 + (1/6)*(k1+2*k2+2*k3+k4);
y0 = y1;
x0=x0+h;
end
disp(y1)
Modified Euler’s Method
clc
clear all
x0 = 0;
y0 = 1;
h = .05;
xn = input(' enter the value of the point ');
n = (xn - x0) / h;
for i = 1 : n
y1 = y0 + h*fn(x0,y0); %initial
while (1)
y2 = y0 + h*[fn(x0,y0)+fn(x0+h,y1)]/2;
if abs(y2-y1) < 0.00001
break
else y1 = y2
end
end
x0 = x0 + h
y0 = y1
end
disp (y1)