MATLAB: Solve an equation using eulers method and two different derivatives.

eulereuler method

Hi! I need to write a matlab code to solve an equation using eulers method.
The given data is:
y'1= y1 + y2
y'2 = – y1 + y2
y1(0)= 1
y2(0)= 0
The step length is 0.1 and t goes to 1 (the interval is [0, 1])
If have written this code but there is a problem in the loop.
clear all
y1=1 %y1(0)=1
y2=0 %y2(0)=0
h=0.25
t_end=1
t=0
while(t<t_slut)
y1= y1 + h*(y1+y2)
y2= y2 + h*(-y1+y2)
t=t+h
end
Since y2 is using the already calculated value of y1 in the loop the answer is incorrect…
How to i structure my code to solve this problem?
Answers would be extremely appreciated.
Thank you in beforehand
Oskar

Best Answer

Since you want to see the result over the complete interval [0 1] and not only for the last t-value, I guess, you should use arrays for y1 and y2:
y1(1) = 1;
y2(1) = 0;
for k=1:4
y1(k+1) = y1(k)+h*(y1(k)+y2(k));
y2(k+1) = y2(k)+h*(-y1(k)+y2(k));
end
Best wishes
Torsten.