# Solving a differential equation using numerical methods and matlab

(a) Consider the following differential Equation

$$Y'(t)=\frac{1}{1+t^{2}}-2[Y(t)]^2$$ $$Y(0)=0$$

The exact solution is $$Y(t)=\frac{t}{1+t^2}$$

Using the Euler method to solve the following differential equation with the help of matlab (code given below), run the program for $t=10$ and $h=0.1$ compute the error.Then run it for $h=0.05$ compute the error and note how it changes.Then use Richardson Extrapolation to improve the accuracy. Compute the error.

function y=Euler(t,h,y0,t0)

n=floor((t-t0)/h);
y=y0;
for i=1:n
y=y+h*f(t0+h*(i-1),y);
end;

function f=f(t,y)

f=1/(1+t^2)-2*y^2;
return;


(b) Now using the Runge-Kutta method to solve the differential equation in part (a) Run the program for $t=10$ and $h=0.1$ The error should be reduced as compared to part(a). Explain the difference. Run it again for $h=0.05$ and notice how the error is changed. Use Richardson extrapolation to improve the accuracy and compute the error.

function y=Runge_Kutta(t,h,y0,t0)

n=floor((t-t0)/h);
y=y0;
for i=1:n
t=t0+h*(i-1);
y=y+(h/2)*(f(t,y)+f(t+h,y+h*f(t,y)));
end;

function f=f(t,y)

f=1/(1+t^2)-2*y^2;
return;


What i tried

(a) When i ran the program with a step size of $0.1$ i got an error of $0.0986$ but when i ran it with a step size of $0.05$, i got an error of $0.0988$ which dosent make sense to me beacuse a smaller step size should give a smaller error but it does not appear so in this case.(There is nothing wrong with the code because it is given by my prof). Could anyone explain this to me. Thanks

(b) Again when i run the program for this part i got an error of $0.0990$ for both step size $0.1$ and $0.05$ which is larger than that of part(a) which dosent make sense to me beacuse the Runge-Kutta method is supposed to be more accurate than the Euler method and hence have smaller error, but it dosent appear so in this case. Could anyone explain to me what is going wrong here. Thanks

• Please note that this is not a homework help site. Please see this Meta post on asking homework questions and this Meta post for "check my work" problems.
– John Rennie
Sep 13 '16 at 6:13
• In the Euler scheme, you're evaluation $f(t,y)$ at $t=t_{k+1}$ but $y=y(t_k)$, which is wrong. A similar error is in the Runge-Kutta scheme (you're incrementing $t$, and then using $t$ and $t+h$). Sep 13 '16 at 8:26

When you run your program with the parameters you specified, you don't get the error, but the solution at time $t=10$.
To compute an error, you need to compare the numerical solution to the analytical one, that's why the analytical solution $Y_a$ of your EDO is given. You need to compute : $$\vert \vert Y - Y_a\vert \vert _{L^1}$$ for instance, and plot or write the results against $h$. I suggest you to read courses about norms and time convergence. You will be able to do that in MATLAB with the norm command.
Only then you can conclude and notice that when you divide $h$ by 2, the error is divided by 2 if your scheme is order 1 (Euler), or divided by 4 if your scheme is order 2 (Runge-Kutta).