Challenge

Week 3 - Solving second order ODEs

Hi, Its Deepak Sharma, Today we will see, the simulation of a pendulam.:

We know that the differential equation is :

where , b= damping co-efficent

m= is the mass of ball hanged with a string and other end of string is attached to a rigid surface.

g= is the gravity i.e. 9.81 m/s2

L = is the length of the string.

where, theta is the angle of string, when oscilated.

Let assume:

length of 1m, string is attched to a ball having 1KG mass is hanged with rigid surface, with damping co-efficent of 0.5. we drop from an angle of theta, Due to Gravity of earth it will oscilate left to right to left. and after some time it will stop.

let first see the function:

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

function [dtheta_dt]=ode_func(t,theta,b,g,l,m) we storing function name 'ode_func', which have four variable.

theta1=theta(1); it is intitial angle
theta2=theta(2); it is 2nd instant angle
dtheta1_dt=theta2; it is the first derivative of theta w.r.t time. that we call it, theta2
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1); if we again derivate it , it becomes dtheta2_dt equals to as given

dtheta_dt=[dtheta1_dt;dtheta2_dt];derivate it dtheta_dt: it have two value one becomes for angular displacement and other is for Velocity
end

Note: Function and main programm must be in same folder.

Main code:

clear all
close all
clc

b=0.05;
g=9.81;
l=1;
m=1;

%intital conditioon
theta_0=[0;5];

%time points
t_span=linspace(0,20,200);

%solve ode
[t, results]=ode45(@(t,theta) ode_func(t, theta, b, g, l, m),t_span,theta_0);

ct=1
for i=1:length(results(:,1))
    x0=0
    y0=0
    x1=-l*sin(results(i,1))
    y1=-l*cos(results(i,1))
   plot([-1.0,1.0],[0 0],'linewidth',3)
   line([x0 x1],[y0 y1],'linewidth',3)
 hold on
   
   plot(x1,y1,'o','markersize',20,'markerfacecolor','b')
   grid on
   axis([-1.5 1.5 -1.5 0.5])
   pause(0.01)
   hold off
   M(ct)=getframe(gcf)
   ct=ct+1
end
    
movie(M)
videofile=VideoWriter('Oscilation of a Pendulam','uncompressed avi')
open(videofile)
writeVideo(videofile, M)
close(videofile)

b=0.05; (discussed above)
g=9.81;(discussed above)
l=1;(discussed above)
m=1;(discussed above)

%intital conditioon
theta_0=[0;5];, (theta_0: it states the two row 1 column vector, that contains, inititial condition Displacement & Velocity)

%time points
t_span=linspace(0,20,200); (t_span: it says, the tiome is given for 0 to 20 seecond in whioch 200 are intervals)

%solve ode
[t, results]=ode45(@(t,theta) ode_func(t, theta, b, g, l, m),t_span,theta_0);

(here [t, results]= is the answers, ode45(@(t, theta) is the inbuilt function to solve differential equation.

ode_func is our function name we have defined earlier , t_spane is starting and ending time, Theta_0 is intitial value of angle.

ct=1
for i=1:length(results(:,1))
x0=0
y0=0
x1=-l*sin(results(i,1)) for XY coredinate
y1=-l*cos(results(i,1)) for XY coredinate

plot([-1.0,1.0],[0 0],'linewidth',3) ploting line that is rigid to which string is linked
line([x0 x1],[y0 y1],'linewidth',3) ploting a line with x0 y0 and x1 y1
hold on holde it for every fram

plot(x1,y1,'o','markersize',20,'markerfacecolor','b') it creating the ball "marker"
grid on
axis([-1.5 1.5 -1.5 0.5]) loacking the axes.
pause(0.01) pause for each frame 0.01 sec
hold off
M(ct)=getframe(gcf) inserting each frame in "M'
ct=ct+1 every frame restart with increment of 1.
end

movie(M)
videofile=VideoWriter('Oscilation of a Pendulam','uncompressed avi')
open(videofile)
writeVideo(videofile, M)
close(videofile)

above are the Video generating command. 'Oscilation of a Pendulam' is the name of file/video.

https://www.youtube.com/watch?v=2h-Exd0WBm0

: i got some error but nobody from your side help me .

neither i get any session or any slot for my doubt clearing.

i am doing it by my self, from the date of joining, and to till now , nobody asked how i am doing, do you get any problems?.

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.