Challenge

Week 3 - Solving second order ODEs

https://youtu.be/VAj9JdQIUbw

Aim

         To write a program that solves an equation of motion of simple pendulum with damping.

Explanation

       The above equation is second order, To solve an ODE  we are breaking into two first order equations

 dtheta1_dt=theta2

 dtheta2_dt= -(b/m)*theta2-(g/l)*sin(theta1)

clear all
close all
clc
b=0.05; %damping coefficients
m=1; %mass of the bob
l=1; %length of the string
g=9.81; %acceleration due to gravity

%initial displacement,initial velocity
theta_0=[0;3]; %angular displacementand angular velocity
t_span=linspace(0,20,300); %generate an array of 100 values between 0 t0 20 seconds

%ODE syntax
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
ct=1;
for i=1:length(results)
x0=0; %coordinates for fixed point of pendulum
y0=0;
x1=l*sin(results(i,1)); %coordinates for oscillating point of pendulum
y1=-l*cos(results(i,1));
%plotting
plot([x0 x1],[y0 y1],'LineWidth',5,'color','r') %plot for string movement
hold on 
plot(x1,y1,'marker','o','markerSize',20,'MarkerEdgeColor','b') %plot for bob
axis([-1.5 1.5 -2 2]) %defining the axis for the graph
pause(0.03)
M(ct)=getframe(gcf); %gathering the frames of the pendulum movement
ct=ct+1; %incrementing the counter
hold off
end
movie(M) %calling the frames
videofile=VideoWriter('secondODE.m','Uncompressed AVI')
open(videofile) %open the video
writeVideo(Videofile,M) %writing the frames in a videofile
close(videofile) %close the file
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
​

Face an error while typing a videowrite command.  written 'Videowriter' instead of 'VideoWriter'
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.