Challenge

Week 3 - Solving second order ODEs

Main Script
clear all
close all
clc
%To develop a matlab script to solve the ODE of a motion of a simple
%pendulum with damping
%ODE is given by
%d/dt*(theta2) + b/m*(theta2) + g/l*(sin(theta1))
%Defining the input parameters
b=0.05;
g=9.81;
l=1;
m=1;
w = 3;
%Angular displacement at inital position theta = 0;
%initial condition
theta_0=[0;3];
%timepoints
t_span=linspace(0,20,500);
%solve ode
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
% Plot of angular displacement and velocity versus time
figure(1)
plot(t,results(:,1));
hold on
plot(t,results(:,2));
ylabel('plot of angular displacment and velocity')
xlabel('time')
legend({'Angular displacement','Angular velocity'})
%When the pendulum is at rest
xi=0;
yi=0;
plot(xi,yi,'.','MarkerSize',10)
% Isolating the displacement form the results.
d = results(:,1);
%Animation for the SHM of a simple pendulum
ct=1;
for i=1:length(t_span)
xd = l*sin(d(i));
yd = -l*cos(d(i));
figure(2)
plot([xi xd],[yi yd],'lineWidth',3,'color','b')
hold on
plot(xi,yi,'.','MarkerSize',20)
plot(xd,yd,'.','color','r','MarkerSize',50)
hold off
title('Simulation of motion of a pendulum with damping','FontSize',10,'color','g')
axis([-1 1 -2 0])
grid on
pause(0.10);
xlabel('Time span')
ylabel('Angular displacement and velocity')
M(ct)= getframe(gcf);
ct=ct+1;
end
%Movie(M) creation to file
movie(M);
videofile = VideoWriter('Simplependulum.avi','Uncompressed AVI');
videofile.FrameRate = 5;
open(videofile);
writeVideo(videofile,M)
close(videofile)

Function script

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


Report:As the report couldn't be attached here due to format issues I am uploading it as a word document.
Youtube link for Simple pendulum animation:https://youtu.be/pRpFzUwQ6Eo
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.