Forward Euler oscillations in plot
    5 views (last 30 days)
  
       Show older comments
    
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');
Answers (1)
  Aashray
 on 26 Jun 2025
        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. 
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
