Forward Euler solution plotting for dy/dt=y^2-y^3

Hi,
I am trying to solve the flame propagation model dy/dt=y^2-y^3 with y(0)= 1/100 and 0<t<200, using the forward and backward euler method with step size 0.01. But it has been giving me errors. How should I go about this? Please help I need this for my project
Thank you
Here are my codes for the Forward Euler
h=0.01;
y(0)=2
for n=1:N
t(n+1)=n*h
opts = odeset('RelTol',1.e-4);
y(n+1)= y(n)+h*(y.^2-y.^3);
end
plot(t,y)

 採用された回答

Davide Masiello
Davide Masiello 2022 年 11 月 15 日
編集済み: Davide Masiello 2022 年 11 月 15 日

0 投票

There are a couple of issues with your code
1) Indexes in MatLab start at 1, not 0, so y(0) is not valid syntax and must be replaced with y(1).
2) First index, then raise to the power, i.e. y^2(n) becomes y(n)^2
See example below (since you have not specified the value of delta, I arbitrarily replaced it with 0.1)
N = 100; % number of steps
t = linspace(0,200,N);
h = t(2)-t(1); % step size
y = zeros(1,N); % Initialization of solution (speeds up code)
y(1) = 1/100; % Initial condition
for n = 1:N-1
y(n+1) = y(n)+h*(y(n)^2-y(n)^3); % FWD Euler solved for y(n+1)
end
figure(1)
plot(t,y)
Now, the backward euler method is a bit more complicated because it's an implicit method.
You can use a root finder algorithm like fzero and do
N = 100; % number of steps
t = linspace(0,200,N);
h = t(2)-t(1); % step size
y = zeros(1,N); % Initialization of solution (speeds up code)
y(1) = 1/100; % Initial condition
for n = 1:N-1
f = @(x) x-y(n)-h*(x.^2-x.^3);
y(n+1) = fzero(f,y(n)); % BWD Euler solved for y(n+1)
end
figure(2)
plot(t,y)

15 件のコメント

David
David 2022 年 11 月 15 日
Thanks for your help but the graph does not look right. The given differential y^2-y^3 is coming from the frame propagation model. Since I am working on solving stiff ode i am now trying to solve the given ode which is stiff with both Backward and forward euler to show the differences in stability isn the two solutions, Pls help if you have any clue on how to go about it. Thank you
Torsten
Torsten 2022 年 11 月 15 日
編集済み: Torsten 2022 年 11 月 15 日
Change
h = 0.01; % step size
N = 6; % number of steps
y = zeros(1,N); % Initialization of solution (speeds up code)
y(1) = 0.1; % Initial condition
to
h = 0.01; % step size
N = 100; % number of steps
y = zeros(1,N); % Initialization of solution (speeds up code)
y(1) = 2; % Initial condition
in Davide's code.
syms y(t)
eqn = diff(y,t) ==y^2-y^3;
cond = y(0)==2;
sol = dsolve(eqn,cond);
y = matlabFunction(sol);
t = 0:0.01:1;
plot(t,y(t))
David
David 2022 年 11 月 15 日
Sorry I made a mistake there by the initial condition the right pne is y(0)=1/100 and 0<t<200 Thank you
Torsten
Torsten 2022 年 11 月 15 日
And does it work with the new setting
h = 0.01; % step size
N = 200*100; % number of steps
y = zeros(1,N); % Initialization of solution (speeds up code)
y(1) = 0.01; % Initial condition
?
David
David 2022 年 11 月 15 日
N=100
Torsten
Torsten 2022 年 11 月 15 日
編集済み: Torsten 2022 年 11 月 15 日
If you set N = 100 with step size 1/100, you arrive at t=N*h = 1, not t=200.
David
David 2022 年 11 月 15 日
Okay Lets just consider 200 then
Davide Masiello
Davide Masiello 2022 年 11 月 15 日
I have edited my answer, please check.
David
David 2022 年 11 月 15 日
Thanks very much
Davide Masiello
Davide Masiello 2022 年 11 月 15 日
My pleasure. If the answer helped, please consider accepting it.
David
David 2022 年 11 月 23 日
編集済み: David 2022 年 11 月 23 日
Good day sir, how do i graphically show the stability regions of the two methods(Backward and Forward Euler) on the same model?
Torsten
Torsten 2022 年 11 月 23 日
David
David 2022 年 11 月 23 日
Thank you for the information. So they always look the same?
Torsten
Torsten 2022 年 11 月 23 日
They are not dependent on the model you solve - stability regions only depend on the discretization method for the ODE
y' = f(t,y)
David
David 2022 年 11 月 23 日
Thank you very much, this helped me so much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2020b

タグ

質問済み:

2022 年 11 月 15 日

コメント済み:

2022 年 11 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by