Week 3 - Solving second order ODEs
% Solving second order ode and simulating and animating the motion of the
% pendulum
clear
close all
clc
% inputs
L = 1;
g = 9.81;
m = 1;
b = 0.05;
% g = gravity_in_m/s2;
% L = length_of_the_pendulum_in_m;
% m = mass_of_the_ball_in_kg;
% b = damping_coefficient;
% initial condition
theta_0 = [0,3];
% time span
t_span = linspace(0,20,100);
% solving ode
[t, results] = ode45(@(t,theta) ode_function(t,theta,b,L,g,m),t_span,theta_0);
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time')
ylabel('plot')
hold off
%simulating and animating the motion of the
% pendulum
a = 1;
for i = 1:100
temporary = results(:,1);
x1 = L*sin(temporary(a,1));
y1 = -L*cos(temporary(a,1));
figure(2)
% for wall
plot ([-1 1], [0 0],'linewidth',3,'color','r')
hold on
% for center of the wall
plot(0,0,'*','linewidth',3,'color','g')
% for string
plot([0 x1],[0 y1],'linewidth',3)
% for ball
plot(x1,y1,'o','linewidth',8)
axis([-2 2 -2 2]);
pause(0.02)
M(a) = getframe(gcf);
hold off
a = a+1;
end
% for creating a movie to upload in youtube
movie(M)
videofile = VideoWriter('pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
REPORT
% EXPLANATION:
- All the inputs are provided on the question
- we are giving the initial condition via linspace command (1st value indicates displacement 2nd value indicates velocity)
- we are using ode45, it is a inbulid command and the ode function next to it is the function we are created
- (t_span = t)(theta_0 = theta) the ode command will take this inupts in that order
- plotting (t, results) we need all the rows in t, and 1 st and 2nd column seperately while plotting
- labelling and completing the graph
- for simulating and animating
- giving the plot to all the solid parts such as
- wall, string, ball
- providing axis
- we need a counter to run the program in different values, in this case it is 'a', if we dont add this it will run the same value again and again
- using pause command to slow the process
using getframe option to create a animated video file
-
% for creating a movie to upload in youtubemovie(M)videofile = VideoWriter('pendulum.avi','Uncompressed AVI');open(videofile)writeVideo(videofile,M)close(videofile)
%ERRORS I GOT
And how i fixed it
- I typed line rather than plot, it didnt took that command correctly so i put put plot in it
- when i put line the animation is not going back and forth
- in the formula to find x1 and y1 there are no values in degree, so i took the degree off and then program ran correctly
- when i ran with degree on in L*cosd(thete) and L*sind(theta) the pendulum is not moving
YOUTUBE LINK - https://youtu.be/iOWk35XJwnU