Project

ODE for a simple pendulum using ODE45 solver in MATLAB

close all
clear all
clc

%one degre equation of a simple pendulum
%inputs
b = 0.05; %damping coefficient
m = 1; %mass of the bob
g = 9.81; %Accleration due to gravity
l = 1; %length of the chord

%initial distplacement, initial velocity 
theta_0 = [0;3] %angular displacement and initial velocity of the bob
%timee points
t_span = linspace(0,20,100);%generate values from o to 20 for 100 values
%differential equation
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0); %ode solver for solving one degree equation
ct = 1; %set the counter to 1
for i = 1:length(results(:,1)); %executing for loop, from results of theta from linspace
  
    x0 = 0; % coordinates for the fixed point of the pendulum
    y0 = 0;
 %doing vector resolution   
    x1 = l*sin(results(i,1));%vertical line for the chord
    y1 = -l*cos(results(i,1));%horizontal line for the chord
    figure(2);
    plot([-1 1], [0 0], \'linewidth\' , 4, \'color\',\'b\');%plot for chord movement
    hold on
    line([x0 x1],[y0 y1],\'linewidth\',2,\'color\',\'r\');%chord moving
    hold on
    plot(x1,y1, \'v\',\'markers\',20,\'markerfacecolor\',\'r\');%for adding bob
    hold off;
    grid on;
    axis([-1.5 1.5 -1.5 1.5]);%defining the axis for the graph
    pause(0.5);
  A(ct) = getframe(gcf); %capturing the frames of the pendulum movement
     ct = ct+1; %incrementing the counter
end
movie(A) %calling the frames
pendulum = VideoWriter(\'ode_pendulum.avi\',\'uncompressed avi\') %writing the video
open(pendulum) %open the video
writeVideo(pendulum,A) %perform the frames to video
close(pendulum)%close the file


figure(3);
plot(t,results(:,1)); %plotting the damping and the oscillation values
hold on
plot(t , results(:,2));
​

Function code

%ode func
function [dtheta_dt] = ode_func(t,theta,b,g,l,m);
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta2;
dtheta2_dt = -(b/m)*theta2-(g/l)*sin(theta1)
dtheta_dt = [dtheta1_dt ; dtheta2_dt];
end

The simple pendulum equation is solved by using an ode45 solver. The simple pendulum in the graph occurs in the third and fourth quadrant. As the damping coefficient is increased the vibrations fade away much faster.

Errors:-

1. MATLAB movie creation function is a case sensitive command. Encountered an error of the video where the video didn\'t last for a second.

2.
Conclusion:-


When damping value is 1.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.