## code of euler's method

### Joaquim (view profile)

on 22 May 2014
Latest activity Answered by George Papazafeiropoulos

on 23 May 2014

### George Papazafeiropoulos (view profile)

Hi, i follow every protocol steps for euler's method, but my results are too increased and they are not correct. Anyone could see if i´m doing anything wrong? i think it happens because my derivatives are floating too much.

Sara

### Sara (view profile)

on 22 May 2014
What's the expected result? What are the functions you're trying to solve?

### Products ### George Papazafeiropoulos (view profile)

on 23 May 2014

A simple application of Euler method:
Define the function:
function E=euler(f,a,b,ya,M)
h=(b-a)/M;
Y=zeros(1,M+1);
T=a:h:b;
Y(1)=ya;
for j=1:M
Y(j+1)=Y(j)+h*f(T(j));
end
E=[T' Y'];
end
where - f is the function entered as function handle
- a and b are the left and right endpoints
- ya is the initial condition E(a)
- M is the number of steps
- E=[T' Y'] where T is the vector of abscissas and Y is the vector of ordinates
Then run the code:
f=@(x) x^2;
a=0;
b=10;
ya=0;
M=200;
YY=euler(f,a,b,ya,M)