Project

ODE For Pendulum

clear all
close all
clc

%inputs
b=0.1;  %damping co-efficient
g=9.81; %acceleration due to gravity
l=1;    %length of pendulum
m=0.5;  %mass of ball

%intial condition
theta_0 = [0;5];

%time points
t_span = linspace(0,10,500);  %time 0 to 10 sec with 500 intervals

[t,results]=ode45(@(t,theta) odefunc(t, theta, b,g,l,m),t_span,theta_0);%ode45 to solve differential equation

%hold on
%plot(t,results(:,2))
%xlabel(\'PLOT\')
%xlabel(\'TIME\')

c=1; %intialation of counter
for i=1:length(results(:,1))  %calling points form array result
    
    
 x=0;
 y=0;
 x1=l*sin(results(i,1));  %x co-ordinates of ball postion
 y1=-l*cos(results(i,1)); %y co-ordinates of ball postion
 
 
 plot([x,x1],[y,y1],\'linewidth\',3,\'color\',\'b\') %plot of pendulum link
 hold on;
 
 plot(x,y,\'o\',\'markers\',5,\'markerfacecolor\',\'g\'); %plot of fix point
 hold on;
 
 plot(x1,y1,\'o\',\'markers\',20,\'markerfacecolor\',\'g\'); %plot of pendulum ball
 hold on;
 
 
 axis([-2 2 -1.5 1.5]); % scale for graph for ploting
 pause(0.001);          % pause between result ploting
 hold off;
 
 
  M(c)=getframe(gcf);   %geting graphics frames in array M
  c=c+1;                %counter increment
 
 
end

movie(M) %create movie of array M
videofile=VideoWriter(\'pendulm.avi\',\'Uncompressed AVI\') %file name and format 
open(videofile)
writeVideo(videofile,M)
close(videofile)
    
    
    
    
    



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

ODE DAMPING
Step 1
Define inputs for pendulum.i.e
damping co-efficient \'b\'.
acceleration due to gravity \'g\'=9.81.
length of a pendulum.
mass of the ball.
The initial condition of theta_0 is a column array of [0;5].

Step2
for getting results at a different time between a time span a row array is taken ranging
from 0 to 10 with 500 intervals.
so in a row array [time, result] is stored.
ode45 function is used to solve differential equation by calling odefunc made to solve differential equation -(b/m)*theta2-(g/l)*sin(theta1).

we get an array of time and results.

Step3
Now set a counter to get an array of time and result at a different time from time range span.
for i=1
to get coordinates for pendulum ball results from time result array is used.
these result is substituted in
l*sin(results(i,1)) and -l*cos(results(i,1))

to get x1 and y1 coordinates respectively.

plot([x,x1],[y,y1]) to get pendulum link.
plot(x,y) for fixpoint and plot(x1,y1) with marker for pendulum ball.

axis for graph ranges from -2 to 2 on the x-axis and -1.5 to 1.5 on the y-axis.
in array m get graphics.
increment the counter.

step 4
movie function is used to create a movie from array M.

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.