Week 3 - Solving second order ODEs
https://drive.google.com/file/d/1ey2SB_6VRLYEZGT7nnEfxBd5o0xhahfQ/view?usp=sharing
clear all
close all
clc
%inputs
damp_coeff=0.05;
gravt_force=9.81;
l=1.1;
mass=0.1;
%initial conditions
disp_velocity= [0;5];
%time points
t_span = linspace(0,10,100);
%solving ode
[t,out] = ode45(@(t,theta) ode_func(t,theta ,damp_coeff,gravt_force,l,mass),t_span,disp_velocity);
figure(1)
plot(t,out(:,1))
hold on
plot(t,out(:,2))
xlabel(t_span)
ylabel(disp_velocity)
%animation plottiing for pendulumm
ct=1
for i=1:length(out(:,1))
x0=0;
y0=0;
x1=l*sin(out(i,1));
y1=-(l*cos(out(i,1)));
figure(2)
plot([-0.9 0.9],[0 0],'color',[0 0 1],'linewidth',5)
axis([-1.5 1.5 -2 2])
hold on
plot([x0 x1],[y0 y1],'linewidth',3,'color','r')
plot(x1,y1,'--mo','markersize',15,'markerfacecolor',[rand rand rand],'color','r')
pause(0.03)
hold off
M(ct)= getframe(gcf)
ct=ct+1;
end
movie(M)
videofile=VideoWriter('ode_solving.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
STEP :1 clear all , close all , clc commands are inputed to clear the command window and the work space
STEP :2 then the inputs such as damping co efficient , length of the pendulum , mass of the spherical ball , gravitational force are fed
STEP :3 then the initial conditions such as displacement and the veloocity is inputted
STEP :4 then the time of the ossilation of the pendulum is given in an array of 500 values from 1 to 10 sec with linspace command
STEP :5 then i create a function which helps to solve the ode , which has the input of time , theta values damping co efficient , length of the pendulum , mass of the spherical ball , gravitational force
STEP :6 next the dtheta_1 and dtheta_2 variables are assigned to their respected equatios which are derived from the pendulum equation given
STEP :7 the theta 1 and theta 2 are clubed in a single value of theta in a column matrix then dtheta_1 and dtheta_2 are clubbed in a single column matrix
STEP :8 then the clubbed dtheta values are fed to the function output
STEP :9 next the ode solving function is made by using ode45 solver which is the inbuilt operation of matlab , where the function which we created is fed as an input to this ode45 which gives up two outputs , one is the time array and another is an array which is a column matrix who has the set of values of angular velocity and angular displacement
STEP :10 then by the set of values we created , now were going to plot them to create an animation of pendulum ossiltion
STEP :11 for that we have to create a for loop which contains the values of ouput (column matrix , all rows 1 column)
STEP :12 then we have to point the fixed and moving ends of the pendulum , this costs the creation of end points of the pendulum , this is made by this way , x1=l*sin(out(i,1)) , y1=-(l*cos(out(i,1))); x1 and y1 are the moving end points of the pendulum where the fixed end is in the orgin
STEP :13 then by using plot command we create the base line where the pendulum is hung
STEP :14 next the x0 x1 y0 y1 points are plotted by plot command , here we add line width , color off the pendulum
STEP :15 then spherical ball's color , size is made by using a markersizecolor , markersize command , which is added in the ploting command of the spherical ball
STEP :16 next the animated video is made by using movie command
STEP : 17 then the videoWriter is used to make the uncompressed avi video file ,
STEP : 18 open cOmmand is used to open the movie we crreated
STEP : 19then writeVideo is used to write the video file
STEP : 20 finally video is closed by using close command
errors faced while coding
while makig the pendulum plot , i have placed x and y co ordinates , instead of plotting first x co ordinates and next y co ordinates . by making this error , the animation of my plot gets worse ,
while defining the y co ordinate point , i missed the negative sign value , which results in the inverse plotting of my pendulum animation , then this was rectified by gathering knowledge from my mentor ,
then i had made a couple of errors which are made by coding the program carelessly , i am trying my best to overcome this kind of problems 