Project

Solving second order Ode

MAIN PROGRAM

%Pendulum Animation
clc
clear all
close all
%inputs

m=1;
g=9.81;
l=2;
x1=0;
y1=0;

theta_O=[0 30]

%time span (sec)
t_span=linspace(0,60,100)
%plotting
[t results]=ode45(@(t,theta) odefunc(theta,t,m,g,l),t_span,theta_O);
figure(1)
hold on
plot(t,results(:,1),\'.-.\')
plot(t,results(:,2))


xlabel(\'Time (m/s)\')
ylabel(\'Plot\')
%animation
figure(2)
ct=1;
for i=1:length(results(:,1))
 Theta=results(i,1);
    
x2=l*cosd(Theta);
y2=l*sind(Theta);

plot(x1,y1,\'o\',x2,y2,\'o\',[x1 x2],[y1 y2])
title(\'Pendulum Animation\')
axis([-2 4 -2 2])
pause(0.001)
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter(\'Pendulum animation.avi\',\'uncompressed AVI\')
open(videofile)
writeVideo(videofile,M)
close(videofile)

DEFINED FUNCTION

function [ dtheta_dt]=odefunc(theta,t,m,g,l)
 theta1=theta(1);
 theta2=theta(2);
 dtheta1_dt=theta2
 dtheta2_dt=-(g/(l*m))^2*sind(theta1)
 dtheta_dt=[dtheta1_dt; dtheta2_dt];
end

PLOT

ANIMATION LINK

https://youtu.be/jqVFztkoSno

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.