The following code ought to apply the Forward/Explicit Euler method to solve the ODE and plot a graph. However it displays oscillations.
clc
syms t
S = solve((10 - (10+t)*exp(-t)) + 10*exp(-200*t) == 10, t); % Solve to get start of domain.
h = 0.01; % Step Size.
x = S:h:S+10; % Take a domain of 10 and divide into steps.
z = zeros(1,length(x)); % Pre-allocate.
z(1) = 10; % Initial Condition.
Y = @(t,r) -200*(r - (10 - (10+t)*exp(-t))) + exp(-t)*(9 + t); % Function.
for i=1:(length(x)-1) % Iteration loop.
y(i+1) = y(i) + h * Y(x(i),y(i)); % https://en.wikipedia.org/wiki/Euler_method#Informal_geometrical_description
end
plot(x,y,'-or','DisplayName','FE-code approximation');

답변 (1개)

Aashray
Aashray 2025년 6월 26일

0 개 추천

The oscillations in the plot are due to the stiffness of the ODE. The Forward (Explicit) Euler method is conditionally stable.
  • The ODE contains the term -200*(r - ...), which is a very large negative coefficient (the stiffness).
  • In explicit Euler, the stability condition for linear ODEs like dy/dt = λy is:
Here, λ ≈ -200, so the step size should be:
You are using h = 0.01, which is exactly on the stability boundary. So, reducing h value will help reduce the oscillations.
You can compare the screenshots of plots I obtained with h=0.01 and h=0.099 respectively.
The second plot here converges and does not oscillate.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

질문:

2021년 1월 11일

답변:

2025년 6월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by