Project

Solving 2nd order ODE(simple pendulum)

AIM : To solve simulate the transient behavior of a simple pendulum

OBJECTIVES :

  1. To solve the ODE of simple pedulum
  2. To animate the motion of simple pendulum

STEPS :

  1. Firstly write all the inputs of the code
  2. Then take a variable that has the start time and start velocity
  3. Another variable that has the time array
  4. Then solve the ODE by dividing it into multiple single order ODE
  5. Then plot the results
  6. Make a movie using the results and time values

CODE :

close all
clc

%Inputs
b=0.05;  %damping coefficient
g=9.81;  %acceleration due to gravity
l=1;     %length of the pendulum
m=1;   %mass

%Initial conditions
theta_0=[0;3];

%time array
t_span=linspace(0,20,500);

%solve ODE
[t,results]=ode45(@(t,theta) ode_f(t,theta,b,g,l,m),t_span,theta_0); %ode45 is a matlab operator.ode_f is the function in the sub program. 

%plotting
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel("time")
title("Simple pendulum")

theta2 = results(:,1);
ct=1;
for i=1:length(theta2)
    theta = theta2(i);
    x0=0;
    y0=0;
    x1=l*sin(theta);
    y1=-l*cos(theta);
    
    %plotting the pendulum
    figure(2)
    plot([-1 1],[0 0],"color","k","linewidth",3)
    hold on
    plot([x0 x1],[y0 y1],"color","m","linewidth",3)
    plot(x1,y1,'o','markers',10,"markerfacecolor","b")
    hold off
    grid on
    axis([-2 2 -2 2]);
    pause(0.03)
    A(ct)=getframe(gcf);
    ct=ct+1;
end

movie(A)
videofile=VideoWriter('Simple_pendulum.avi','UNCOMPRESSED AVI');
open(videofile)
writeVideo(videofile,A)
close(videofile)

SUB PROGRAM:

%This sub program is used to solve the ODE
function [dtheta_dt]=ode_f(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

ERROR :

I got a videowriter error

SOL : Only use single quote inside the Videowriter

OUTPUT :

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.