Max Step that can be used at Eulers Methods
Show older comments
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)
Torsten
on 23 Jun 2016
0 votes
I don't understand the definition of Ea.
What is meant by "new approximation" and "old approximation" ?
Best wishes
Torsten.
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!