Challenge

Week 3 - Solving second order ODEs

Aim:write a program that will simulate the pendulam motion

Program:

ode fuction call

function [dtheta_dt] = ode_function(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];

ode main program

clear all
close all
clc

%inputs
%damping coefficient
b = 0.05;
%accelaration due to gravity - m/s^2
g = 9.81;
%mass of the bob - kg
m = 1;
%length of the string - m
l = 1;
%initial condition
dis_angvelo = [0;3];

%timepoints
t_span = linspace(0,20,500);

%solving ode
[t,results] = ode45(@(t,theta) ode_function(t,theta,b,g,l,m),t_span,dis_angvelo);

%plotting
figure(1)
plot(t,results(:,1),'linewidth',1)
hold on
plot(t,results(:,2),'linewidth',1)
axis([0 25 -3 3])
xlabel('time - sec')
ylabel('angular velocity & displacement')
legend('change in displacement','change in angular velocity')
hold off

figure(2)
plot(results(:,1),results(:,2))
xlabel('position - red')
ylabel('angular velocity - rad/sec')

theta2 = results(:,1);
ct = 1;
for i = 1:length(theta2);
    theta3 = theta2(i);
    
    x0 = 0;
    y0 = 0;
    
    x1 = 1*sin(theta3);
    y1 = -1*cos(theta3);
    
    figure(3)
    plot([-1.1 1.1],[0 0],'linewidth',2);
    hold on
    plot([x0 x1],[y0 y1],'color','r','linewidth',2)
    hold on
    plot(x1,y1,'o','markers',20,'markerfacecolor','b')
    hold off
    grid on
    axis([-1.5 1.5 -1.5 0.5])
    pause(0.003)
    M(ct) = getframe(gcf);
    ct = ct+1;
end
movie(M)
videofile = videowriter('pendulam.avi','uncompressed avi');
open(videofile)
writevideo(videofile,M)
close(videofile)

*Damoing coefficient,gravity of earth,length of rop and mass of ball taken as the input

*plot command is used to get the finel plot

*xlable() and ylable() command used to define the lables in the plot .title command is used to define the plot title

*the video file of '.avi' format is made to save the movie

*the movie is the written into the videofile and the final animation is thus created.

Errors occurs during programing

here by mistake, i give the wrong argument to the function.

RESULT

THE GRAP SHOWS TIME VS DISPLACEMENT(BLUE) AND TIME VS ANGULAR VELOCITY(RED).AS SHOWN IN GRAPH DISPLACEMENT IN CONTINUOSLY DECREASING DUE TO DAMPING COEFFICIENT ALSO ANGULAR VELOCITY IS IN DECREASING ORDER

ANIMATION:

https://youtu.be/C1xmVTEvjvA

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.