I am dealing with the following 2nd order ODE:
With initial conditions of: , , .
and should be 3.24
I have found the system as:
For the life of me I cannot see to get the below code to produce the correct output… is it the code or my calculus?
clear all;fy=@(x,y,z) 6*x*z-5*zfz=@(x,y,z) z;x(1)=0;z(1)=2/3;y(1)=0;h=0.5;xfinal=3;N=ceil((xfinal-x(1))/h);for j=1:N x(j+1)=x(j)+h; k1y=fy(x(j),y(j),z(j)); k1z=fz(x(j),y(j),z(j)); k2y=fy(x(j)+h/2,y(j)+h/2*k1y,z(j)+h/2*k1z); k2z=fz(x(j)+h/2,y(j)+h/2*k1y,z(j)+h/2*k1z); k3y=fy(x(j)+h/2,y(j)+h/2*k2y,z(j)+h/2*k2z); k3z=fz(x(j)+h/2,y(j)+h/2*k2y,z(j)+h/2*k2z); k4y=fy(x(j)+h,y(j)+h*k3y,z(j)+h*k3z); k4z=fz(x(j)+h,y(j)+h*k3y,z(j)+h*k3z); y(j+1)=y(j)+h/6*(k1y+2*k2y+2*k3y+k4y); z(j+1)=z(j)+h/6*(k1z+2*k2z+2*k3z+k4z);enddisp(y(N));figure;plot(x,y,'LineWidth',2);xlabel('X');ylabel('Y');
Best Answer