Forward Euler oscillations in plot

3 次查看(过去 30 天)
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
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.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by