Week 3 - Solving second order ODEs
Aim :
To programe a ode function for a given function.
Given Function :
d2θ/dt2 + b/m dθ/dt +g/l sinθ = 0;
Given Condiction :
initial displacement = 0
given angualr velosity = 3 rad/sec
time interval = 20 sec.
Given Inputs :
length of pendulam (l) = 1 meter;
mass (m) = 0.1 kg.
damping co-efficient = 0.05;
gravity = 9.81.
Main Program :
clear all
close all
clc
%input parameters
l = 1;
b = 0.05;
g = 9.81;
M = 0.1;
%given condiction
con_0 = [0,3];
%given time
t_span = linspace(0,20,500);
%ode function
[t,result] = ode45(@(t,con) formula_fun(t,con,g,l,b,M),t_span,con_0);
%ploting
figure(1)
plot(t,result(:,1),'color', 'r')
axis([0 15 -10 10])
hold on
plot(t,result(:,2), 'color', 'b')
xlabel('time')
ylabel('plot')
grid on
legend('displacement','velocisy')
%video ploting
ct = 1;
for j = 1:length(t_span);
x0 = 0;
y0 = 0;
x1 = l*sin(result(j,1));
y1 = -l*cos(result(j,1));
figure(2)
plot([x0 x1],[y0 y1],'LineWidth',2);
pause(0.025)
grid on
axis([-3 3 -3 3])
m(ct) = getframe(gcf);
ct = ct+1;
end
movie(m)
videofile = VideoWriter('pendulam_chalange.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,m)
close(videofile)
Function Program :
function [dtheta_dt] = formula_fun(t,con,g,l,b,m)
theta1 = con(1);
theta2 = con(2);
dtheta1_dt = theta2;
dtheta2_dt = -(b/m)*theta2-(g/l)*sin(theta1);
[dtheta_dt] = [dtheta1_dt;dtheta2_dt];
end
Explanaction :
- initialy the program clear all content in work space and command window and close all opened files respect to matlab.
- lnow the program reads the given input values and condiction which is length, mass, dampinf co-efficient, gravity.
- the condictions are set to arra which is :: con_0 = [0,3]. herse 0 is initial displacement,3 is angular velocity.
- the number of time is set to with in 0 to 20 sec of 500 values.
- now the ode45 solver is used to derive the equation. here the inputs and condictions are given in the function formula_fun with the con_0 and t_span.
- these values are saved in [t,results]. were t contian t_span value,result contioan output value of angular displacement and angular velocity.
- when function get called the input values are automativaly assigned to the respective variables.
- all these values are stored in [d2θ/dt2].
- here variable theta1 contain the initial displacement 0,theta2 contain the initial angular velosity 3 rad/sec.
- the given function is a secound order equation so it is splited in to simple first order equation manualy.
- ther fore dθ1/dt = theta2 :: which is initial angular velosity.
- dθ2/dt = -b/m * theta2 - g/l * sin(theta1). :: which is angular displacement respect to velosity.
- both values are saved in [d2θ/dt2] as initialy declared.
- now the result variable contain t_span times of displacement ,velosity value.
- now to plot:: plot(t,result(:,1) - gives the plot relaction between time to angular displacement.
- now to plot:: plot(t,result(:,2) - gives the plot relaction between time to angular velosity.
- legend commend helps to give the respective name for plots.
- to find the displacement for end of the pendulam point for loop created with respect to t_span time
- j = 1:length(t_span) these syntex helps to repet the loop until the time given limit ends.
- now initial cordinatie(first end of pendulam) give as x0 = 0,yo = 0.
- ne secound end is change its possition withrespect to angular displacement for each time sec.
- it is given by x1 = sin(result(j,1)); and y1 = -cos(result(j,1)); here j represent the t_span counte and the first value of displacement in result matrix.
- For each time loop runs displacemnt value change accourdingly.
- these value get to plot by(plot commend followed by([x0 y0],[x1 y1],'linewidth',2).
- for each loop seperate plots will be ploted.these seperate plots are called as frame.
- these frames are collected in m(ct) variable with getframe(gcf) commend.
- now the value for ct increased by 1 :: ct = ct+1;
- so the loop continues until the j - t_span of 20 sec eaches.
- these frames are converted to movie.
- videofile is a variable created to store video in the .avi formate.
- open commend helps to open the video file.
- VideoWriter commend helps to collect all frames and create a movie file.
- close commend helps to close the video file.
Note : my orginal video file got 534mb so i compressed the video on onloine. thanking you.....