Runge-Kutta 4th Order method
clc;
clear all;
close all;
h=0.01;
a=0;
b=1.8;
x = a:h:b;
y = zeros(1,numel(x));
y(1)= 1;
Fyx = @(x,y) (y^2-x^2)/(y^2+x^2);
fprintf('%7s %7s %7s %7s %7s %7s %7s \n','i','x(i)','k1','k2','k3', 'k4','y(i)');
for i=1:1:numel(x)
k1 = h*Fyx(x(i),y(i));
k2 = h*Fyx(x(i)+0.5*h,y(i)+0.5*k1);
k3 = h*Fyx((x(i)+0.5*h),(y(i)+0.5*k2));
k4 = h*Fyx((x(i)+h),(y(i)+k3));
y(i+1) = y(i) + (1/6)*(k1+2*k2+2*k3+k4);
if x(i)==0.2 || x(i)==1.8
fprintf('%7d %7.2f %7.3f %7.3f',i, x(i), k1, k2);
fprintf(' %7.3f %7.3f %7.3f \n', k3, k4, y(i+1));
end
end
y(numel(x))=[ ];
plot(x,y)
title('RK-4');
ylabel('y');
xlabel('x');
grid on
Good job bro.. 👌👌
ReplyDelete