Week 4.1 - Solving second order ODEs
clear all
close all
clc
b = 0.5;
m = 1;
l = 1;
g = 9.81;
% initial condition
theta_0 =[0;3];
%time point
t_span =linspace(0,20,500);
% solve ODEs
[t,results] = ode45(@(t,theta) ODE_func(t,theta,b,m,l,g),t_span,theta_0);
plot ( t,results(:,1));
hold on
plot (t,results(:,2));
ylabel('t_span')
xlabel('time')
title('simply_supported_beam')
%Animation
ct=1;
figure(2)
theta1= results(:,1)
for i = 1:length(theta1)
x0 = 0;
y0 = 0;
theta2 = theta1(i);
x1 = l*sin(theta2);
y1 = -l*cos(theta2);
plot([-2 2],[0 0])
hold on
plot([x0 x1],[y0 y1],'linewidth',3)
plot(x1,y1,'marker','o','color','g','Markersize',50,'Markerfacecolor','g');
axis([-2 2 -2 2])
pause(0.02)
M(ct)=getframe(gcf)
hold off
ct=ct+1
end
movie(M)
videofile = VideoWriter('simplysupported_beam.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Introduction:
To Simulate the Simple Pendulam motion using Newton's Second law of motion with the format of Second order ODE function.
Equation:
X0 = 0
Y0 = 0
X1 = l*sin(theta2)
Y1 = -l*cos(theta2)
Here,
The X0 and Y0 represents the starting point of the point origins (0,0) where the X1 and Y1 represents the motion of the pendulam which is the product of the l (Length of the pendulam) with the theta angle 2.
Objectives:
The main objective of this function is to simulate the Simple pendulam using the ODE order equation.
Explanation:
From the first three lines indicates the basic command of clear all , close all and clc.
The 5th,6th,7th and 8th line indicates the equation values of b,m,l,g.
Here the 10th line shows the initial value of theta which is the angular displacement and angular velocity that is 0 and 3.
The 13th line shows the time point value which is t_span which equals as linspace command from 0 to 20 of 500 times.
The 17th line starts with the header of Solving ODEs which gives the equation of ode45 which is a command in matlab program with t(time) and theta of ODE_func program(Another function which is made for deriving the main function and for deriving equations ) product time,theta,b,m,l,g,t_span and theta_0 value.
The 21st line gives the plot command to plot the time and result 1.
The 22ndth line gives the hold on command to hold the line 21.
The 23rd line gives the plot command of the plot time with result 2.
The 24th line gives the y label as t_span
The 25th line gives the x label as time and the 26th line gives the title for the command as Simply Supported Beam.
The 28th line gives a header as Animation and the below commands are going to give the animation of the pendulam.
The lines from 29th to 31st gives the counter value 1 and indicates the figure2 and it shows the theta1 is equal to the results of 1st column of rows.
In 32nd line the for loop is given for the pendulam oscillation, here i is equals to the 1: the length of theta 1.and the 33 and 34th line gives the X0 and Y0 value as 0,0
In line 35th, theta2 value is given as the product of theta 1 and i no. of times.
The line 36th shows the value of the X1 where it equals to the l(1) with the product of sine theta 2.
The line 37th shows the value of the Y1 where it equals to the l which is negative to the cos theta 2.
The 38th line gives the plot command for the x and y axis values from -2 and 2, 0 and 0.
Then the hold on command is given for holding the upcoming command plot with the values of x0 x1 and y1 y2 with the linwidth of 3.
The line 42nd shows the plot command for another input that are used for plotting the X1 Y1 and marker command is also used and the color green (g) is given for the ball's appearance then the marker size and marker face color is given as 50,green(g).
The 45th line gives the axis command as -2 ,2 ,-2 ,2 and below the pause command is used to pause the output in 0.02sec
The 47th line shows the Movie command with counter(ct) which equals the getframe(gcf).
The 48thline shows the hold off command and the 49th line shows the counter value as counter + 1 (ct+1).
The 50th line shows the end of the for loop command.
Then the 52nd line shows the movie command for showing the output in a video format.
The 53rd line shows the videofile file format as VideoWriter with Simplysupported_beam.avi and as Uncompressed AVI.
The line 54th gives the open command of the videofile and the line 55th gives the writeVideo command as videofile M.
The 56th line gives the close command of the videofile.
Conclusion:
Thus, the given ODE function is determined and the output of the Simply supported beam is obtained by derived code.