Challenge

Week 3 - Solving second order ODEs

AIM-write a program to demonstrate the damping of a simple pendulum.
MAIN CODE.
b=0.05;
l=1;
m=1;
g=9.81;
%intialcondition
theta_0=[0;5];
%timepoints
t_span=linspace(0,10,400);
%solveode
[t,results]=ode45(@(t,theta)ode_function(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
%animation
x0=0;
y0=0;
ct=1;
for i=1:length(t)
x1=l*sin(results(i,1));
y1=-l*cos(results(i,1));
figure(2)
plot([-1 1],[0 0],'linewidth',6,'Color','r');
axis([-2 2 -2 2])
line([x0 x1],[y0 y1],'linewidth',4,'color','r');
hold on
plot(x0,y0);
plot(x1,y1);
title('damping of a simple pendulum')
grid on
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M);
videofile = VideoWriter('simple_pendulum.AVI','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
FUNCTION CODE.
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];
end
PROBLEM FACED
I faced a problem in the animation of the simple pendulim but i rectified the mistake which was i typed hold on instead of hold off.
CONTENTS
the ode_2.m file contains the main code
the ode_function.m contains the function code
the ode_3.fig contains the frequency curve
the simple_pendulum.AVI.zip contains the animation of the damping of the pendulum
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.