Challenge

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:

  1. All the inputs are provided on the question
  2. we are giving the initial condition via linspace command (1st value indicates displacement 2nd value indicates velocity)
  3. we are using ode45, it is a inbulid command and the ode function next to it is the function we are created
  4. (t_span = t)(theta_0 = theta) the ode command will take this inupts in that order
  5. plotting (t, results) we need all the rows in t, and 1 st and 2nd column seperately while plotting
  6. labelling and completing the graph
  1. for simulating and animating
  2. giving the plot to all the solid parts such as
  3. wall, string, ball
  4. providing axis
  5. 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
  6. using pause command to slow the process

using getframe option to create a animated video file

  1. % for creating a movie to upload in youtube
    movie(M)
    videofile = VideoWriter('pendulum.avi','Uncompressed AVI');
    open(videofile)
    writeVideo(videofile,M)
    close(videofile)

%ERRORS I GOT

And how i fixed it

  1. I typed line rather than plot, it didnt took that command correctly so i put put plot in it
  2. when i put line the animation is not going back and forth
  3. 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
  4. 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

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.