Assignment Chef icon Assignment Chef
All English tutorials

Programming lesson

CMPSC431W Phase 2 Progress Review: Pendulum Simulation with Explicit and Semi-Implicit Euler Methods

Learn how to discretize the pendulum ODEs using explicit and semi-implicit Euler methods, simulate angular motion in MATLAB, and analyze energy conservation. Includes code, plots, and a discussion on timestep effects.

pendulum simulation MATLAB explicit Euler method pendulum semi-implicit Euler method CMPSC431W phase 2 pendulum ODE discretization energy conservation numerical methods simple pendulum tutorial MATLAB pendulum code forward Euler energy drift symplectic integrator pendulum numerical stability pendulum game physics integration AI simulation pendulum computational physics assignment pendulum angular position plot Euler method comparison

Introduction to the Pendulum Problem

In CMPSC431W Phase 2, you are tasked with simulating a simple pendulum using numerical methods. The pendulum's motion is described by the second-order ODE: d²θ/dt² = -(g/L) sin(θ). By introducing angular velocity ω = dθ/dt, we obtain two first-order ODEs: dθ/dt = ω and dω/dt = -(g/L) sin(θ). Your goal is to discretize these equations using the explicit (forward) Euler method and later a semi-implicit approach, then plot kinematics and total energy per unit mass.

Discretized Governing Equations (Explicit Euler)

Using subscripts k and k+1 for current and next time steps, the explicit Euler discretization is:

ω_{k+1} = ω_k + Δt * (-g/L * sin(θ_k))
θ_{k+1} = θ_k + Δt * ω_k

Notice that the right-hand side uses only known values from step k. For L = 1 m, g = 9.81 m/s², initial θ₀ = π/3, ω₀ = 0, and Δt = 0.005 s, simulate up to t = 20 s.

MATLAB Implementation: Explicit Euler

Below is a script to compute θ, ω, and α (angular acceleration = -g/L sin(θ)). The total energy per unit mass is E = 0.5 * L² * ω² + g * L * (1 - cos(θ)).

% Parameters
g = 9.81;
L = 1;
theta0 = pi/3;
omega0 = 0;
dt = 0.005;
t_final = 20;
t = 0:dt:t_final;
n = length(t);

% Initialize arrays
theta = zeros(1, n);
omega = zeros(1, n);
alpha = zeros(1, n);
energy = zeros(1, n);

theta(1) = theta0;
omega(1) = omega0;
alpha(1) = -g/L * sin(theta0);
energy(1) = 0.5 * L^2 * omega0^2 + g * L * (1 - cos(theta0));

% Explicit Euler loop
for k = 1:n-1
    omega(k+1) = omega(k) + dt * (-g/L * sin(theta(k)));
    theta(k+1) = theta(k) + dt * omega(k);
    alpha(k+1) = -g/L * sin(theta(k+1));
    energy(k+1) = 0.5 * L^2 * omega(k+1)^2 + g * L * (1 - cos(theta(k+1)));
end

% Plot kinematics
figure;
plot(t, theta, 'b', t, omega, 'r', t, alpha, 'g');
xlabel('Time (s)'); ylabel('Value');
legend('θ (rad)', 'ω (rad/s)', 'α (rad/s²)');
title('Pendulum Kinematics - Explicit Euler');

% Plot energy
figure;
plot(t, energy);
xlabel('Time (s)'); ylabel('Total Energy per Unit Mass (J/kg)');
title('Energy vs Time - Explicit Euler');

Semi-Implicit Euler Method

In the semi-implicit (symplectic) Euler, we update velocity first, then use the new velocity to update position:

ω_{k+1} = ω_k + Δt * (-g/L * sin(θ_k))
θ_{k+1} = θ_k + Δt * ω_{k+1}

This small change often improves energy conservation. Modify the loop accordingly.

% Semi-implicit Euler loop
for k = 1:n-1
    omega(k+1) = omega(k) + dt * (-g/L * sin(theta(k)));
    theta(k+1) = theta(k) + dt * omega(k+1);
    alpha(k+1) = -g/L * sin(theta(k+1));
    energy(k+1) = 0.5 * L^2 * omega(k+1)^2 + g * L * (1 - cos(theta(k+1)));
end

Analyzing Energy Conservation

For the explicit Euler, the total energy tends to increase over time (drift upward), indicating that the method does not conserve energy. The semi-implicit Euler, however, shows bounded energy oscillations around a constant mean, mimicking true physical behavior. Reducing Δt (e.g., to 0.001 s) reduces the drift in explicit Euler but still shows a trend; semi-implicit remains stable.

Discussion: Does Forward Euler Conserve Energy?

No, the explicit (forward) Euler method does not conserve energy for the pendulum problem. The numerical energy increases monotonically, which is unphysical. Using a smaller timestep reduces the rate of energy drift but does not eliminate it. In contrast, the semi-implicit Euler (symplectic) conserves energy on average, making it suitable for long-term simulations.

Trend Connection: Simulation in Game Physics

Just as game engines like Unity or Unreal use symplectic integrators for realistic physics (e.g., swinging ropes in God of War or grappling hooks in Fortnite), your pendulum simulation demonstrates why numerical method choice matters. The same principles apply to AI-driven simulations of robotic arms or financial models predicting market oscillations.

Conclusion

In this CMPSC431W Phase 2 progress review, you implemented explicit and semi-implicit Euler methods for the pendulum ODEs. You plotted kinematics and energy, observing that explicit Euler fails to conserve energy while semi-implicit Euler performs better. This exercise underscores the importance of numerical stability and energy conservation in computational physics.