Challenge

Week 3 - Solving second order ODEs

First error faced in the code was 

Image uploaded Pendulum_program_error_1

Q(ct) = getframe(gcf);

a command to get the frames stitch together and create a motio picture.

the error was because the code couldn't get all the frames together since the command was disrupted in between. once all the frames were made to run, it created the movie. Error was rectified.

Second error:

Image uploaded Pendulum_program_error_2

The error wrong calculation for finding the cordinates of the rope/string.

 x1 = x0 + l*cos(results(i,1));
 y1 = y0 - l*sin(results(i,1));

rectified by changing the calculatio to 

 x1 = x0 + l*sin(results(i,1));
 y1 = y0 - l*cos(results(i,1));
Youtube link of the Animated Video

link: https://youtu.be/NX2RWzDHJV8
clear all
close all
clc

%Program to simulate the equ of motion of a simple pendulam

%inputs (b-damping co-efficient, g-accl due to gravity, l-length of the string/
%pendulam and m-mass)

b = 0.05;
g = 9.81;
l = 1;
m = 1;

%initial conditions of the pendulam in terms of displacement and velocity

theta_0 = [0;3];

%initial time points

t_span = linspace(0,20,500);

%command to solve the ODE

[t, results] = ode23(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);

%frame count value
ct = 1;

%looping in the individual values of reults against variable i:
for i = 1:length(results(:,1))
             
    %calculation to the find the co-ordinates of the string/rope are:
    %origin or starting point of the rope:

    x0 = 0;
    y0 = 0;

    %ending point of the rope:
    
    x1 = x0 + l*sin(results(i,1));
    y1 = y0 - l*cos(results(i,1));
    
    %plotting all the coordinates of the rope
   
    plot([x0 x1],[y0 y1],'linewidth',3,'color','b');
    
    hold on
    
    %plotting the coordinates of the rope base
    plot([-1.5 1.5],[0 0],'linewidth',5,'color','r');
    
    plot(x1,y1,'-o','markers',20,'markerfacecolor','r');
    
    text (0,10, ['time = ', num2str(t_span(i))]);
    
    %setting the graph axis:
    axis([-1.5 1.5 -1.5 0.5])
    
    %pausing the plot-command/instances in secs:
    pause(0.1)
    
    hold off
    
    %creating a variable for Movie from these set of plots:
    Q(ct) = getframe(gcf);
    
    %Frame count increments:
    ct = ct+1;
end

%generating the movie:
movie(Q) 

videofile = VideoWriter('Pendulum_Motion_Animation.avi','uncompressed AVI')

open(videofile)

writeVideo(videofile,Q)

close(videofile)

%End of the Program
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.