MATLAB: Code of euler’s method

errorerror in euler's methodeuler's methodfloating derivativesMATLAB

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.

Best Answer

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)
You can adjust your problem according to the above algorithm.