# The Euler method code in Matlab

The Euler method is a Runge-Kutta method with order 1, it is the simpliest Runge-Kutta method. We' ll show the code for a program written in Matlab for the initial value problem

$\begin{matrix} y'=y \\ y(0)=1 \end{matrix}$

We want to know the y value at t = 1. The obvious solution is

$y(t) = e^t$ and therefore $y(1) = e$

### Matlab code for the Euler method: File euler.m

function [y,f_calls]=euler(fun,t0,t1,y0,h,rpar)
t=t0;
y=y0;
fc=0;

while t < t1
if t+h>t1;h=t1-t;end
k1=feval(fun,t,y,rpar);
y=y+h*k1;
fc=fc+1;
t=t+h;
end

if nargout > 1 fcalls=fc; end

### File fun.m

This is a file containing the function to evaluate, at this case y'=y

function[y] = fun(x, y, rpar)
u=x;

### Running

Save both files at same directory, and form matlab console use the cd command to go this directory. Run the following command from console

>>y =euler('fun', 0, 1, y0, 0.01,0);

Now we have the result stored in the variable y, run this to see it

>> y

and that gives us the result

y = 2.7048 ...

Execute here the Euler's method in our Runge kutta calculator

Post here

Post here