Challenge

Week 3 - Solving second order ODEs

clear all- used for clear all the data stored in previous workspace

close all- used for colse the all the figures and plots of before program

clc-use for clear the command window

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2.

theta- is the angle made by the pendullam moves from staring position to extream position , when external force is applied

t_span- used for time take to complete the oscillations from 0 second to 20 seconds

[t, results]=ode45(@(t,theta) ode_func(t,theta,d,l,g,m),t_span, theta_0)
this is the second order defferencial equation of (theta) with respect to time (t) with the variables of d, l, g, m.
plot(t,results(:,1),'linewidth',5,'color','r') % this code is for how time varies in Oscillation of simle pediudulam
plot(t,results(:,2),'linewidth',5,'Color','g'); % how angular dispalcement varies in Oscillation of simle pediudulam
Co ordinates of simlpe pedulam
x0=0;
y0=0;
x1=-l.*sin(results(i,1));
y1=-l.*cos(results(i,1));
plot([-1 1],[0 0],'LineWidth',3,'color','y') % code for fix the base line
plot(x0,y0,'-b','MarkerSize',2,'Color','k'); % code for fix a point on base line for string of bob
plot(x1,y1,'o','MarkerSize',25,'MarkerFaceColor','r'); % code for plot the bob of pendulam
plot([x0 x1],[y0 y1],'linewidth',5,'color','b'); % code for the joining the fixed point on the base line and bob like string
M(ct)=getframe(gcf) %gives a name(M) for all the counter variable to collect the exact frame of the video

for creating a movie of the result a counter variable is itroduced before the first for loop as "ct"

M % for capture the current frame for every time untill the for loop ends

ct+1 % used for increment of frame addend to the variable "ct"

movie(M) %used for cteating a movie of M

VideoWriter %command is used for write the videofile int formate of .avi

open(videofile) %is used of open that videofile

Writevideo %used for write the video file with plot frames

close(videofile) %used for clos the video file

for running the program we can us F5

CODE:-
clear all
close all
clc

%inputs
d=0.05;
g=9.81;
l=1;
m=0.1;
%inital condition
theta_0=[0;5];

%time points
t_span=linspace(0,20,100);
%solve ODE
[t, results]=ode45(@(t,theta) ode_func(t,theta,d,l,g,m),t_span, theta_0);
figure(1)
hold on
plot(t,results(:,1),'linewidth',5,'color','r');  % plotiing of time
plot(t,results(:,2),'linewidth',5,'Color','g');  % plotting of angular displacement 
xlabel('time');
ylabel('displacement');
legend('time','angular displacement');
grid on
ct=1;                         % counter value staring from 1
for i=1:length(results)
x0=0;                         % i
y0=0;
x1=-l.*sin(results(i,1));
y1=-l.*cos(results(i,1));

figure(2)

plot([-1 1],[0 0],'LineWidth',3,'color','y');
hold on
plot([x0 x1],[y0 y1],'linewidth',5,'color','b');
plot(x0,y0,'-b','MarkerSize',2,'Color','k');
plot(x1,y1,'o','MarkerSize',25,'MarkerFaceColor','r');
legend('Oscillations of simple pendulam')
hold off
axis([-2 2 -2 2])
pause(0.03)
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter('simple_pendullam.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)  
close(videofile)
Simplification of Second Order Differential Equation to single Order Differential Equation
function[dtheta_dt]=ode_func(t,theta, d,l,g,m)
theta1=theta(1);
theta2=theta(2);
dtheta1_dt=theta2;
dtheta2_dt=-(d/m)*(theta2)-(g/l)*(sin(theta1));
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end​
Workspace
Errors
I am getting oscilations like this. To over come the error, I'm introduce the hold on command after plotting the base line. Which is in line number 34
Results
https://youtu.be/iMD2dmOPbXU
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.