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
2022 年 11 月 15 日
編集済み: Davide Masiello
2022 年 11 月 15 日
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
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
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
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
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
2022 年 11 月 15 日
N=100
If you set N = 100 with step size 1/100, you arrive at t=N*h = 1, not t=200.
David
2022 年 11 月 15 日
Okay Lets just consider 200 then
Davide Masiello
2022 年 11 月 15 日
I have edited my answer, please check.
David
2022 年 11 月 15 日
Thanks very much
Davide Masiello
2022 年 11 月 15 日
My pleasure. If the answer helped, please consider accepting it.
Good day sir, how do i graphically show the stability regions of the two methods(Backward and Forward Euler) on the same model?
Torsten
2022 年 11 月 23 日
Look at the regions plotted in pink under
David
2022 年 11 月 23 日
Thank you for the information. So they always look the same?
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
2022 年 11 月 23 日
Thank you very much, this helped me so much
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Ordinary Differential Equations についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
