Week 3 - Solving second order ODEs
1)function of ode
function [dtheta_dt]=ode_fun(t,theta,b,g,l,m_a)
theta1=theta(1)
theta2=theta(2)
dtheta1_dt=theta2
dtheta2_dt=-(b/m_a)*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt]
end
2)code for solving ode
close all
clear all
clc
%inputs
b=.05 % damping coefficient
l=1 %length of string
g=9.8 %accelration due to gravity
m_a=1 %mass of the body attached to the string
% initial condition (dependent variable) depends on time
theta_0=[0;3]
%time (indipendent variable)
t_span=linspace(0,20,100);
%ode equation
[t,results]=ode45(@(t,theta) ode_fun(t,theta,b,g,l,m_a),t_span,theta_0)
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time')
ylabel('plot')
figure(2)
x0=0;
y0=0;
ct=1;% here ct defines the frame
for i=1:length(t_span)
x=results(i,1)
x1=l*sin(x)
y1=-1*cos(x)
plot([-3,3],[0,0],'color','r')%plotting the fix point line where the string is fixed
hold on
line([x0 x1],[y0 y1],'color','y','lineWidth',3)%making the string
hold on
plot(x1,y1,'marker','O','markerSize',10,'markerFaceColor','b')%plotting the circle for hanging mass to the string ,where it is attached to point x1 and y1 final length of string
axis([-2 2 -2 2])
grid on
pause(0.01)
hold off
M(ct)=getframe(gcf)%making the arrays of the defined frame
ct=ct+1
end
movie(M)
videofile=VideoWriter('pendulum_challange_6.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
1) solving the ode for given equation .
so, here the given equation is
d_theta/d_t=-b/m d_theta/d_t-b/m sin(theta)
here we can say that theta is dependent variable which is dependent on time and time is indipendent variable
in case of a pendulum where the mass is attached to the string possensing th tension T amd mass of a ball is m,
length of the string is l , accelartion due to gravity is g and damping coefficient of the damper(string) is b
in case 1 we will be making the function
here the function name will be same as the name of file,
here we can say that the variable dtheta_dt is the function of
b,l,m,g,t and theta
where we will be splliting the second order diffrential equation into two parts
since
theta=theta1
^dtheta1/dt=theta 2
since
d^2 theta/dt^2=d^theta1/dt^2
since, dtheta1/dt=theta2
here theequation becomes d/dt(theta2)
since , dtheta/dt=theta 1
and dtheta1/dt=theta2
here there will be two row and single coloumn matrix
where we can say that dtheta1/dt= theta2
and dtheta2/dt=-b/m(theta2) -g/l(sin (theta1))
so here we will write these two equation and say that dtheta/dt is function of these two equation
we will be assigning the values to theta1 and theta2 since
initial conditon theta_0 in programme reffers to the initial displacement and velocity
here we can say that theta1 is displacement and theta2 is velocity
so basically we can say that displacement with respect to time can be reffered as velocity.
part 2 : coding
here we will be defining the variables l,g,b,m,theta and t
where t will be a array and theta is dependent on t since theta is dependent it will have as many as value as that of t
since theta is a row matrix defining two different values where the first value is assigned to displacement and second value is of velocity
in theta_0 we will be giving as initial condition so according to the equation when displacement will be 0 and vlocity is 3
since t is an coloumn array there will be same values for displacemt and velocity
after that we will be defining the ode function
here t and results will be our final variables where results contain 2 values in row arrray till time array and two coloumns .
here ode45 is internal matlab function for we are saying that ode45 is function of theta and time where ode_fun is function of dtheta_dt which is ode45 it is valid till the value of t_span which is array and initial conition of the state is theta_0
after that we will be plotting the graph for angular velocity and anguar displacement
for example for plottiong the graph for displacement against time
plot(t,results(:,1)) where we are saying to plot the graph of displacemt which is first row of the result against the time
and same goes for velocity. here results will be for 2nd row
now part 3 creating animation for pendulum
here we can say that let x0 and y0 be the starting point of the string which is 0 for both the axis and
x1=l x sin theta(1)
y1=-l x cos theta(1)
here theta(1) can be represnted as angle made by sting to the vertical axis since angle is made with respect to vertical axis we are giving y1=-l cos theta(1)
(angle will be made when displacemnt will be positive or negative ,independent to velocity so here every thing will be dependent on displacment wich is result of row 1).
here we will be creating for loop
since for i(which is loop variable ) will be defined as 1:length(result)
x=results(i,1) (result of row 1 till the length of result )
this each value will be a different array to create this as single picture we will be defining ct which can be assigned as a output of 1st frame
and then after plotting the values
we will be creating the array of each frame and incrimenting the value by one
for plotting the ball we are using maker command makerSize will define size og the ball makerColor will be defining hte color .
after that we will be creating the video by using the array we created using ct.
4)mistakes done
1) after defining the mass m i once again assigned the value of m to the array of ct to correct this i gave mass as m_a
2) insted of using -l cos theta i used positive value .
here you can refer scereen shots for perticular mistakes as given number
pendulum animation
https://drive.google.com/file/d/1SEmFNFUYVaMN0TKuG9A4G3PHvMkdZRjw/view?usp=sharing