Week 4.1 - Solving second order ODEs
Aim: Solving a second order ODE using MATLAB.
Introduction
The motion
of a pendulum is a classic problem in physics and engineering, providing
insights into the dynamics of oscillatory systems. When damping is introduced,
the system models real-world scenarios more accurately, such as the behavior of
a pendulum clock, the motion of a child on a swing, or the suspension systems
in vehicles. This report outlines the simulation of a damped pendulum's motion
using MATLAB, with the aim of visualizing its behavior over time and creating
an animation to demonstrate its dynamics.
Practical
Applications
Understanding
the motion of a damped pendulum has numerous practical applications:
1. Clocks
and Timekeeping:
Pendulums are used in clocks where precise timing is crucial. Damping effects must be understood and minimized to maintain accuracy.
2. Engineering
and Design:
In automotive suspension systems, the principles of a damped pendulum help in designing shock absorbers that reduce oscillations and provide a smooth ride.
3. Seismic
Analysis:
Pendulum models are used to simulate and analyze the behavior of buildings and structures during earthquakes, where damping helps in mitigating vibrations.
4. Control
Systems:
In robotics and aerospace, controlling the oscillations of various
components is essential for stability and performance.
Steps Taken
and Approach
1. Model
Definition:
The mathematical model of a damped pendulum is defined by a second-order ordinary differential equation (ODE). The equation considers the effects of gravity, damping, and the pendulum's physical characteristics.
2. ODE
Solver:
MATLAB's `ode45` solver is used to numerically solve the ODE over a specified time span.
3. Animation
Setup:
An animation is created to visualize the pendulum's motion. This involves plotting the pendulum's position at each time step and saving the frames to a video file.
4. Plotting
Results:
The angular displacement and velocity are plotted over time to analyze
the system's behavior.
Matlab Code With Explanation
Function Definition (`ode_func.m`)
*****
% ode_func.m
function [dtheta_dt] = ode_func(t, theta, b, g, l, m)
% Extract the first and second components of theta
theta1 = theta(1);
theta2 = theta(2);
% Define the derivatives
dtheta1_dt = theta2;
dtheta2_dt = -(b/m) * theta2 - (g/l) * sin(theta1);
% Return the derivatives as a column vector
dtheta_dt = [dtheta1_dt; dtheta2_dt];
end
*****
This function defines the dynamics of the damped pendulum. It takes the current time 't', state vector 'theta' (containing angular displacement and velocity), and system parameters (damping coefficient 'b', gravity 'g', length 'l', and mass 'm'). It returns the derivatives of the state vector.
Main Script (`main.m`)
***
% main.m
% Parameters
b = 0.5; % damping coefficient
g = 9.81; % acceleration due to gravity
l = 1.0; % length of the pendulum
m = 1.0; % mass of the pendulum
% Initial conditions [theta(0), theta_dot(0)]
theta0 = [0; 3]; % initial angle (rad) and angular velocity (rad/s)
% Time span
tspan = [0 20];
% Solve the ODE
[t, theta] = ode45(@(t, theta) ode_func(t, theta, b, g, l, m), tspan, theta0);
% Prepare the animation
figure;
hold on;
axis equal;
grid on;
xlim([-1.2 1.2]);
ylim([-1.2 1.2]);
xlabel('X Position (m)');
ylabel('Y Position (m)');
title('Pendulum Motion Animation');
% Plot the trajectory
plot(l*sin(theta(:,1)), -l*cos(theta(:,1)), 'r--'); % Plot the pendulum path
% Initialize pendulum line and mass
pendulumLine = line([0, l*sin(theta(1,1))], [0, -l*cos(theta(1,1))], 'LineWidth', 2);
pendulumMass = plot(l*sin(theta(1,1)), -l*cos(theta(1,1)), 'ro', 'MarkerSize', 12, 'MarkerFaceColor', 'r');
% Create the animation file
videoFile = VideoWriter('pendulum_motion.avi');
open(videoFile);
for i = 1:length(t)
% Update pendulum position
set(pendulumLine, 'XData', [0, l*sin(theta(i,1))], 'YData', [0, -l*cos(theta(i,1))]);
set(pendulumMass, 'XData', l*sin(theta(i,1)), 'YData', -l*cos(theta(i,1)));
% Capture the frame
frame = getframe(gcf);
writeVideo(videoFile, frame);
pause(0.01); % Pause for a short time to create the animation effect
end
% Close the video file
close(videoFile);
% Plot the angular displacement and velocity on the same plot
figure;
yyaxis left;
plot(t, theta(:, 1), 'b-', 'LineWidth', 1.5); % Plot theta (angle) over time
xlabel('Time (s)');
ylabel('Angle (rad)');
title('Pendulum Angle and Angular Velocity over Time');
grid on;
yyaxis right;
plot(t, theta(:, 2), 'r--', 'LineWidth', 1.5); % Plot theta_dot (angular velocity) over time
ylabel('Angular Velocity (rad/s)');
legend('Angle (rad)', 'Angular Velocity (rad/s)');
*****