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

조회 수: 6 (최근 30일)
David
David 2022년 11월 15일
댓글: David 2022년 11월 23일
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일
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
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개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by