Challenge

Week 3 - Solving second order ODEs

Aim,
  1. To create program for Ordinary Differential Equation for a simple pendulum.
  2. And create animation for simple pendulum motion.
Procedure,
The Ordinary Differential Equation is mentioned below,

In the above equation,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

We could use this equation to create a program for pendulum,

in this we assume the values

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2.

Step1,

  1. Here we see the 1st program.
  2. Here first we need to write the inputs.
  3. Then theta was angular displacement (is 0)and angular velocity (is 3) so we take the values of row matrix so we type in bracket with semi coloumn.
  4. Pendulum motion time is 0-20 sec so this is span time.And here hundreds values of ODE's need to be calculated so we use the linspace command for time array. t_span = linspace(0,20,500).
  5. And the ODE solving through the time with results that equation is [t,results] = ode45(@(t,theta) ode_func(t,theta,b,l,g,m),t_span by the coloumn of 500 values, theta_0 here, ode 45 is inbuilt command in matlab for ODE, and ode_func whose inputs are(t,theta,b,g,l,m) and span time and starting (angular displacement,angular velocity)called theta_0.
  6. Finally plotting was time and results.
Program1,
%inputs
b =0.05;
g= 9.81;
l = 1;
m= 1;
%Intialcondition
theta_0 = [0;3];
%Time points
t_span = linspace(0,20,500);
%Solve ODE
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
ct=1
plot(t,results(:,1),'Color','r')
hold on
plot(t,results(:,2),'Color','b')
xlabel('time')
ylabel('plot')

Step2,

  1. Now see the second program is a function program.
  2. 1st need to give name was a function.
  3. Here store the function[dtheta_dt] variables are t,theta,b,l,g,m.
  4. Storing the values of angular displacement as theta1 and angula velocity as theta2.
  5. Here dtheta1 is angular velocity so that is theta2.
  6. And split the ode equation to here and need to find the dtheta2/dt and the equation is dtheta2_dt= -(b/m)* theta2-(g/l)*sin(theta1).
  7. And finally we need the dtheta_dt so we just add dtheta1 and dtheta2 by coloumn matrix so that equation is dtheta_dt = [dtheta1_dt; dtheta2_dt].
  8. Then add this function into 1st program we get the results.
Program2,
function [dtheta_dt] = ode_func(t,theta,b,l,g,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
Output,
Step-2,
Animate the simple pendulum
Procedure,
  1. Here program was same as to plotting.
  2. Then we split the figure 1 & 2
  3. Figure 2 is going to animation of pendulum.
  4. 1st we give the counter value as 1 then use the for loop command here.To make a motion of pendulum.
  5. And give length of rod.
  6. Then we give the coordinates x0,y0 values are 0.
  7. And x1 = l*cos(results(i,1)) & y1 = -l*cos(results(i,1)) for each repetition of for loop.
  8. Now plotting the co ordinattes of [-2 2],[0 0] with color of red and width of 2. and add hold on command to not erase the previous plot.
  9. Then plot for coordinates of x0,y0 & x1,y1 & and here also use Hold on command to not erase the previous plots.
  10. And give the axis-2to2 for X axis & Y axis.
  11. Then pause the loop to 0.001.
  12. And creating an array Mand store all the frames of the plot.
  13. Now increase counter value plus 1 for each reptition.
  14. End for loop
  15. And finally need to save the animation as video file.
  16. so create a movie and then it's a video file so write the video writter command and give name of a file(second_ode_func.avi) and it's need to be uncompressed.
  17. Then opening video file and create a video of each frames in the array M
  18. Last close the video file.

Program,

close all
clear all
clc
%inputs
b =0.05;
g= 9.81;
l = 1;
m= 1;
%Intialcondition
theta_0 = [0;3];
%Time points
t_span = linspace(0,20,500);
%Solve ODE
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,l,g,m),t_span,theta_0);
figure(1)
plot(t,results(:,1),'Color','r')
hold on
plot(t,results(:,2),'Color','b')
xlabel('time')
ylabel('plot')
ct=1;
for i=1:length(t)
%pendulum motion
x0 = 0;
y0 = 0;
x1 = l*sin(results(i,1));
y1 = -l*cos(results(i,1));
figure(2)
plot([-2 2],[0 0],'Color','r','linewidth',2);
hold on
plot([x0,x1],[y0,y1]);
hold on
plot(x1,y1,'marker','.','markersize',50)
axis([-2 2 -2 2]);
pause(0.001);
M(ct)=getframe(gcf)
ct= ct+1;
hold off
end
movie(M)
Videofile = VideoWriter('second_ode_func.avi','Uncompressed AVI');
open(Videofile)
writeVideo(Videofile,M)
close(Videofile)
Output,
Here is the animation of the simple pendulum.
and programming screen shot.
Errors I faced,
1st error- I didn't complete the program before itself I run it.
2nd error- In the xlabel i didn't add upper gamma.
3rd Error- In this I made function program mistake so this one comes an error.
4th error - In this function program this no need to run that because all inputs are we give in main program.
5th error - Here linewidth no need space.
6th error - In this one pendulum upside is facing so we need to change x1 = -l*cos(results(i,1) them pendulum motion comes downside facing & another one is getframe means need to give video file then only this error not comes.
7th error - Here I was written the line 50 was write vido so correct this.
Conclusion,
1st was sucessfukky created a program for ordinary differential equation program.and animation was created for simple pendulum also created sucessfully using MATLAB.
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.