Program
function[]=rk4(x0,y0,xn,h,f)
y(1)=y0
j=(xn-x0)/h
for i=1:j
k1=h*f(x0,y(i));
k2=h*f(x0+h/2,y(i)+k1/2);
k3=h*f(x0+h/2,y(i)+k2/2);
k4=h*f(x0+h,y(i)+k3);
y(i+1)=y(i)+(k1+2*k2+2*k3+k4)/6;
x0=x0+h;
printf('\ny(%g)=%g\n',x0,y(i+1));
end
endfunction
Output:
deff('y=f(x,y)','y=2+sqrt(x*y)')
rk4(1,1,1.6,0.2,f)
y(1.2)=1.64015
y(1.4)=2.36202
y(1.6)=3.16872
function[]=rk4(x0,y0,xn,h,f)
y(1)=y0
j=(xn-x0)/h
for i=1:j
k1=h*f(x0,y(i));
k2=h*f(x0+h/2,y(i)+k1/2);
k3=h*f(x0+h/2,y(i)+k2/2);
k4=h*f(x0+h,y(i)+k3);
y(i+1)=y(i)+(k1+2*k2+2*k3+k4)/6;
x0=x0+h;
printf('\ny(%g)=%g\n',x0,y(i+1));
end
endfunction
Output:
deff('y=f(x,y)','y=2+sqrt(x*y)')
rk4(1,1,1.6,0.2,f)
y(1.2)=1.64015
y(1.4)=2.36202
y(1.6)=3.16872
0 Comments