Max Step that can be used at Eulers Methods

I've got dy/dt= -1000y + 3000 -2000e^(-t), y(0)=0. I want to find the max step "Δt" that I can use in 1) Forward Euler, 2) Backward Euler for 0≤ t≤ 6 so that the Ea<5e-2.
Ea=abs(y_old-y_new)/y_new, where y_old is the solution with "the previous Δt" in the loop and and y_new with the next Δt of the loop.
Here is the scripts I made for each method but I was told they were wrong.
  • for forward:*
tend = 0.006;
y_true = @(t) -(997/999)*exp(-1000*t) - (2000/999)*exp(-t) + 3;
j=1;
dt(j) = 1E-4;
ea(j) = 0;
while (1)
i=1;
y(1)=0;
t(1)=0;
y_back(1)=0;
while (1)
t(i+1)=t(i)+dt(j);
%%%%%forward%%%%%%%%
y(i+1) = (-1000*y(i) + 3000 - 2000*exp(-t(i)))*dt(j) + y(i);
if (t(i+1)>tend)
break;
end
i=i+1;
end
j=j+1;
ea(j) = abs(y(i) - y_true(tend));
if ea(j) > 5e-2
break;
else
dt(j) = dt(j-1) + 1e-8;
end
end
fplot(y_true, [0 tend], 'r')
hold on;
plot(t, y, 'b')
plot(t,y_back, 'g')
  • and for Bckward:*
tend = 0.006;
y_true = @(t) -(997/999)*exp(-1000*t) - (2000/999)*exp(-t) + 3;
j=1;
dt(j) = 1E-3;
ea(j) = 0;
while (1)
i=1;
y(1)=0;
t(1)=0;
y_back(1)=0;
while (1)
%%%%%%%backward%%%%%%%
f=@(x) (-1000*x + 3000 - 2000*exp(-t(i)))*dt(j) + y_back(i) -x;
y_back(i+1)=fzero(f, y_back(i));
t(i+1)=t(i)+dt(j);
if (t(i+1)>tend)
break;
end
i=i+1;
end
j=j+1;
ea(j) = abs(y_back(i) - y_true(tend));
if ea(j) > 5e-2
break;
else
dt(j) = dt(j-1) + 1e-7;
end
end
fplot(y_true, [0 tend], 'r')
hold on;
plot(t, y, 'b')
plot(t,y_back, 'g')

Answers (1)

I don't understand the definition of Ea.
What is meant by "new approximation" and "old approximation" ?
Best wishes
Torsten.

3 Comments

You're right, it wasnt clear. I mean abs(y_old-y_new)/y_new, where y_old is the solution with "the previous Δt" in the loop and and y_new with the next Δt of the loop?
Torsten
Torsten on 23 Jun 2016
Edited: Torsten on 23 Jun 2016
Still unclear.
What is "previous deltat" and what is the "new deltat" ?
Are you given values for deltat in advance ?
Best wishes
Torsten.
Lets assume that we start from Δt=1.e-4, we get a solution of y. Then, we increase Δt for example to Δt=1.e-3. Now, we ve got two solutions for y and the Ea= ABS(y(Δt=1.e-4)-y(Δt=1.e-3))/y(Δt=1.e-3) and it has to be smaller than 1.e-5. If it is we increase Δt again and so it goes. Yes we always get new Δt's in advance and we look for the maximum one that wont have Εa>1.e-5.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 23 Jun 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!