Fazal Maths

3
function [y] = homework2(xo,yo,h,xn) %code for runge kutta starts here n = (xn- xo)/h for i=1:n k1=h*(xo(i)*(yo(i)*yo(i))) k2=h*((xo(i)+0.5*h)*(yo(i)+0.5*k1)^2) k3=h*((xo(i)+0.5*h)*(yo(i)+0.5*k2)^2) k4=h*((xo(i)+h)*(yo(i)+k3)^2) xo(i+1)=xo(i)+ h yo(i+1)=yo(i)+1/6*(k1+k4+2*k2+2*k3) end plot (xo,yo,'g ') hold on %code for euler method starts here for i=1:n xo(i+1)= xo(i)+ h yo(i+1)= yo(i)+ h*(xo(i)*yo(i)^2) end plot(xo,yo,'r') %code for exact integral starts here for i=1:n xo(i+1)= xo(i)+ h yo(i+1)= -1/((xo(i)^2*0.5)-1) end plot(xo,yo,'b') legend('Runge-Kutta','Euler','Analytic') xlabel('X'), ylabel('Y') %end of program.

description

solution to the RungeKutta Method using Matlab

Transcript of Fazal Maths

function [y] = homework2(xo,yo,h,xn) %code for runge kutta starts here n = (xn- xo)/h for i=1:n k1=h*(xo(i)*(yo(i)*yo(i))) k2=h*((xo(i)+0.5*h)*(yo(i)+0.5*k1)^2) k3=h*((xo(i)+0.5*h)*(yo(i)+0.5*k2)^2) k4=h*((xo(i)+h)*(yo(i)+k3)^2) xo(i+1)=xo(i)+ h yo(i+1)=yo(i)+1/6*(k1+k4+2*k2+2*k3)end plot (xo,yo,'g ') hold on%code for euler method starts here for i=1:n xo(i+1)= xo(i)+ h yo(i+1)= yo(i)+ h*(xo(i)*yo(i)^2) endplot(xo,yo,'r') %code for exact integral starts here for i=1:n xo(i+1)= xo(i)+ h yo(i+1)= -1/((xo(i)^2*0.5)-1)end plot(xo,yo,'b')legend('Runge-Kutta','Euler','Analytic')xlabel('X'), ylabel('Y')%end of program.

Figure 1 showing graph

Table 1 showing Comparison of the Accuracy of the Three Methods under Consideration.XEuler Runge-Kutta

01.0000 1.0000 1.0000

0.11.0000 1.00001.0050

0.21.00501.01001.0204

0.31.0204 1.03041.0471

0.41.04711.06231.0870

0.51.0870 1.10741.1429

0.61.14291.16871.2195

0.71.2195 1.25071.3245

0.81.32451.36011.4706

0.91.4706 1.50811.6807

1.01.68071.71292.0000