Challenge

Week 3 - Solving second order ODEs

Aim: Write a program in Matlab that will simulate the pendulum motion.

Governing equation:

Objective:

(1)Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.

(2)Get the position of the pendulum as a function of time and use this information to animate the motion of the pendulum.

Fundamental ODE:

Initial conditions given:

Angular displacement = 0, Angular velocity = 3 rad/s at time t = 0. Motion is to be simulated between 0 and 20 seconds.

Solution:

For solving the ordinary differential equation, function was called, named as ode_func.

Matlab program:

clear all
close all
clc

%Input parameters
b=0.05;
g=9.8;
l=1;
m=1;

%Initial condition
theta_0=[0;3];

%Time span
t_span=linspace(0,20,500);

%Solving ODE
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);

plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('Plot')
xlabel('Time')
%Animation of the pendulum

figure(2)
ct=1;
for i=1:length(results(:,1))
    x_0=0;
    y_0=0;
    x_1=l*sin(results(i,1));
    y_1=-l*cos(results(i,1));
    
    plot([-1.5 1.5 ],[0 0])
    hold on
    plot([x_0 x_1],[y_0 y_1],'linewidth',1,'color','b')
    axis([-1.5 1.5 -1.5 1.5])
    plot(x_1,y_1,'o','MarkerSize',20,'color','r')
    pause(0.0001)
    M(ct)=getframe(gcf)
    hold off
    ct=ct+1
end

%Making the video file
movie(M)
videofile=VideoWriter('simple_pendulum.avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)

Function:

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

Results:

Motion of pendulum:

https://drive.google.com/file/d/1RhV9PsewRNJqE0A_p7dOqbP-Fpa2xZKv/view?usp=sharing

Conclusion:

Matlab program is generated for the motion of a simple pendulum as per the given timeframe and video is generated.

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.