Project

SolvinG 2nd order ODE and simultinG damped oscillations for a simple pendulum

The followinG code simulates an animation for a simple pendulum with damped oscillations . The purpose of the code is to solve an ordinary differential equation which defines position of bob of the pendulum . It involves the followinG differential equation

\"ode\"

The code for the solver as shown below

clear all 
close all
clc
%animation for pendulum
l= 1;
m=1;
g=9.81;
b=0.5;
x0 =0;
y0= 0;

%intial condtion for theta and anGular vel;
theta_0 = [0;3];

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

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

ct=1;
for i=1:length(t_span)
    q=results(i) - pi/2
    x = -l*cos(q);
    y= l*sin(q);
    
    
    plot([x0 x] ,[y0 y] ,\'-O\' , \'linewidth\',3,\'color\',\'r\');
    axis([-1 1 ,-1 0]);
    M(ct) =getframe(gcf);
    
    ct=ct+1;
end
movie(M);
videofile = VideoWriter(\'pendulum_animation.avi\' , \'Uncompressed AVI\');
open(videofile);
writeVideo(videofile,M);
close(videofile);

The followinG is the function defininG my ode ode_func which is solved throuGh ode45 of MATLAB

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 code results in an animation of pedulum oscillatinG with damped oscillations.

The followinG is the youtube link of the resultinG animations:-

https://youtu.be/pFaSDRQGPH8

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.