Challenge

Week 3 - Solving second order ODEs

Functional Program:

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
Main Program:
clear all
close all
clc
l=1;
m=1;
b=0.05;
g=9.81;
%Solving second order differential equation
theta_0=[0;3];
%Initial displacement=0
%Initial velocity=5m/s
t_taken=linspace(0,20,200);
[t,v]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_taken,theta_0);
plot(t,v(:,1))
hold on
plot(t,v(:,2))
grid on
title('Graph')
xlabel('Time')
ylabel('Angular Displacement')
%To animate the pendulum
ANIME=v(:,1)
ab=1
for i=1:length(ANIME)
ANIME=v(i,1)
x0=0;
y0=0;
x1=1*sind(ANIME);
x2=1*cosd(ANIME);
figure(2);
%Plotting the length of the pendulum
plot([x0 x1],[y0 y1],'color','b','linewidth',3)
hold on
plot([0.2 -0.2],[0 0],'color','k','linewidth',4)
plot(x1,y1,'0','MarkerSize',22,'MarkerFaceColor','r')
axis([-1 1 -1.5 0.1]);
hold off
M(ab)=getframe(gcf);
ab=ab+1
end
%Creating a movie file
movie(M)
videofile=VideoWriter('Pendulum.avi','Uncompressed AVI');
open(videofile);
writevideo(videofile,M);
close(videofile)
The challenge was to write a program that solves the ode that relates to the motion of simple pendulum with damping.While writing the program after completing it I was successful in creating the plot but not the animation.I tried ways to figure out what the issue was but unlikely failed to create the animation.
Therefore attaching the image of the plot that i got.
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.