Week 3 - Solving second order ODEs
clear all
close all
clc
g= 9.81;// gravity
l= 1;// length of string
b= 0.05;// damping coeficient value
m= 1;// mass of the ball of the pendulum
x0=0;// initial point in x-axis of the pendulum
y0=0;// initaial point in y-axis of the pendulum
ct=1;// counter variable
theta_0 = [0;3];// defining initial values of velocity and displacement
t_span = linspace(0,20,100);// array to store values of time
[t, results] = ode45(@(t,theta) ode_func(t, theta, b, g, l, m),t_span, theta_0); //
solving the differential equation using solver and function , ode45 is a inbuilt solver, basically represened as the function of the input variables, function is defined and a function calling statement , which solves the differential equatioins and its inputs are defined, t_span which holds the value of time defines how far the integration process in the equation should be executed and the theta_0 in which initial values of velocity and displacement are stored , all of the values derived after solving is stored to the arrays , array t into which the values of time after integration is stored and array results will have 2 different sets of solved values of displacement and velocity
for i=1:length(results(:,1))// for loop is used to equate or loop the whole equation again and again untilll it satisfies its criteria
x1=-(l*sin(results(i,1))); // equation to calculate the position of the pendulum
y1=-(l*cos(results(i,1)));//equation to calculate the position of the pendulum
plot([-1 1],[0 0],'linewidth',4,'color','k'); // command along with the cordinatesto plot the horizontal line in the graph that reprsents the fixed line that hold the pendulums fixed end
hold on // command that holds retains plots in the current axes so that new plots added to the axes of the same graph do not affect existing ones. the above command should be active throughout the program so hold on command is used
line([x0 x1],[y0 y1],'linewidth',2,'color','b');// command along withh the cordinates to plot and represent the line as string of the pendulum connecting the ball
hold on // command that holds retains plots in the current axes so that new plots added to the axes of the same graph do not affect existing ones. the above command should be active throughout the program the line is visible throughout
plot(x1,y1,'-o','markers',20,'markerfacecolor','r')// command and cordinate that rerpresent the ball of the pendulum, marker command creates a marker at the designated cordinate, markerfacecolor is to give the the marker desired color
axis([-1.5 2 -1.5 2]) // determing the axis and its limits
grid on // command to display the grid on the graph
pause(0.1) // command to pause the frame before it changes and the time period mentioned inside
hold off// resets axes properties to their defaults before drawing new plots.
c(ct)=getframe(gcf); // each individual frame is stored inside an array c, and ct is the counter variable used to change the value to capture different frames as it changes which is executed using function getframe command that captures and saves the current figure window in to the array c
ct=ct+1; // counter variable and command to increment its value by 1after every looping operation
end
movie(c) // to make a movie with the elements present in the array c
videofile = VideoWriter('damping.avi','uncompressed AVI') // videofile is created using by the command videowriter, filename and the profile are mentioned in the braces.
open(videofile) // videofile opening command
writeVideo(videofile,c) //command to merge the captured frames and make a movie from it
close(videofile) // command to close the videofile
plot(t,results(:,1)) // command to plot the row variables from the array
hold on// command to hold on the points of the row varibles on the graph
plot(t,results(:,2))// command to plot the coloumn variables from the array
xlabel('plot') // labelling command in x axes
ylabel('time')// labelling command in y axes
function [dtheta_dt]= ode_func(t,theta,b,g,l,m) // function definition
theta1 = theta(1) // defining statement
theta2 = theta(2) // defining statement
dtheta1_dt = theta2; definind the first ode
dtheta2_dt = -(b/m)*theta2 - (g/l)*sin(theta1);// defining the second ode
dtheta_dt = [dtheta1_dt; dtheta2_dt];// combing the first and second ode into an single array
end
video link :
https://drive.google.com/file/d/1Gx-JXidOW69-F6zYZEQlE4zqMoPNAFdt/view?usp=sharing
clear all
close all
clc
g= 9.81;
l= 1;
b= 0.05;
m= 1;
x0=0;
y0=0;
ct=1;
theta_0 = [0;3];
t_span = linspace(0,20,100);
[t, results] = ode45(@(t,theta) ode_func(t, theta, b, g, l, m),t_span, theta_0);
for i=1:length(results(:,1))
x0=0;
yo=0;
x1=-(l*sin(results(i,1)));
y1=-(l*cos(results(i,1)));
plot([-1 1],[0 0],'linewidth',4,'color','k');
hold on
line([x0 x1],[y0 y1],'linewidth',2,'color','b');
hold on
plot(x1,y1,'-o','markers',20,'markerfacecolor','r')
axis([-1.5 2 -1.5 2])
grid on
pause(0.1)
hold off
c(ct)=getframe(gcf);
ct=ct+1;
end
movie(c)
videofile = VideoWriter('damping.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,c)
close(videofile)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('plot')
ylabel('time')
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
errors faced :
1.improper animation , hold off command wasn't included, when included animation turned out to be fine
2. syntax errors, missing characters