Did I implement the backwards-euler Methode correctly?
Show older comments
Hello everyone :)
I struggle a little bit with Backwards Euler Method and an excercise our professor gave us:

With h (step size between two adjacent points) = 1/49
Our Task is to plot this. I tried with my Backwards-euler script:
function [t,y] = Euler_backward(t0,T,y0,h,f)
%h = (T-t0)/m ; %I usually let him calculate h but since it is given I have percentaged(?) it out
t = t0:h:T ; % my time vector
y = zeros(length(y0), length(t)) ; % To be able to calculate a system, I want it to make a matrix with y0 rows and t columns
y(:,1) = y0 ;
for i = 2:length(t)-1
ye(:,i) = y(:,i) + h * f(t(i),y(:,i)) ; % forward Euler Method : y(i+1) = y(i) + h*f(t(i),y(i)) to provide y(i+1) for backwards
y(:,i+1) = y(:,i) + h*f(t(:,i),ye(:,i)) ; % Backwards part with y(i+1) = y(i) + h*f(t(i+1),y(i+1))
end
plot(t,y)
legend('y1','y2')
end
and my separate script (I like to separate script and function):
t0 = 0;
T = 1;
y0 = [1;1]; %Starting conditions
f =@(y,t) [0*y(1)+1*y(2);... %Made a function from the matrix
-101*(y1)-100*y(2)];
h = 1/49 ;
Euler_backward(t0,T,y0,h,f)
It then gives me following graph:

and the graph from our professor looks like this:

I didn't graph the solutions but it is pretty clear, that my solution hardly resembles the solution of my professor.
Now I am kinda lost where my mistake is. Did I implement the backwards euler wrong? It would be very kind if someone could help with this :)
Have a great day,
Marcus
2 Comments
darova
on 29 Nov 2019
How does it work?
y = zeros(length(y0), length(t)) ; % To be able to calculate a system, I want it to make a matrix with y0 rows and t columns
Why do you need 2D matrix?
Marcus Schlitter
on 7 Dec 2019
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!