Challenge

Week 3 - Solving second order ODEs

errors

i have got few errors like

multiple pendulum coming in the frame as shown in screenshot
for that i have added the holdon command in between the line command and marker command

and one for the graph is not like as per in the video shown
link for the simple pendulum video in google drive: https://drive.google.com/file/d/1AmP1Ihsq6qP3eiQEScTTYTLzRH7tZA4N/view?usp=sharing
Program for the ode_func

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
Today here we are creating the animation for the simple pendulum with the second order ODE’s, by using the matlab programming

Here, we have the equation of the motion for simple pendulum with the damping

d2Ø/dt + b/m + g/l sinØ = 0

In the above equation we have

Gravity(g) = 9.81 m/s2
Length of the pendulum(l) = 1 m
Damping coefficient(b) = 0.05
Mass of the ball(m) = 1 kg
Motion between at 0-20 sec
Angular displacement = 0
Angular velocity = 3 rad/sec
Time t = 0


Here we have all the inputs and we will start the writing the program

As usual starting with the 
Close all
Clear all and
Clc

After submitting all inputs b,g,l,m

Creating the theta_0 values that angular displacement at 0 and angular velocity at 3 m/s

That will be like
Theta_0 = [0,3]

Creating the time arrayas t_span from 0 to 20 seconds with 500 values for that we are using linspace command

t_span = linspace(0,20,500)

here we have the formula of simple pendulum with the damping

d2Ø/dt + b/m + g/l sinØ = 0

for expanding the above formula, here we are using the function command for that we are creating the one ode_func program with the expanding the above formula


in ode_func program


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

like the above we expanded the this formula and we will assign the inputs in the main program

now coming to the main program

we are using the ode45, ode45 is the solver function in the matlab

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

We adding the above line in the program where the time and results  

Creating the plot for the both angular displacement and angular velocity

When we run this we can see clearly two curves will be formed, blue coloured curve is displacement curve and the red one is the velocity curve which I had shown in the graph


now, by using this above program we are creating the animation of the simple pendulum with angular displacement and velocity results

for that we are using the for loop, Movie commands for video purpose

now the Co-ordinates are X0 = 0, Y0 = 0
adding the counter value ct = 1

opening the for loop adding the the i = 1:length(t) for the 500 values, because the ‘t’ has 500 values, and

adding the co-ordinates X1,Y1 for the pendulum ball

X1 = l*sin(results(i,1))
Y1 = -l*cos(results(i,1))

Now using the plotting command creating the straight beam for hanging the pendulum

plot([-1 1],[-0,0],'LineWidth',6,'Color','g');
in x direction from -1 to 1 and in y direction at 0,0

creating the length line with with co-ordinates x0,x1,y0,y1 with linewidth 3
line([X0,X1],[Y0,Y1],'linewidth',3,'color','b');

here, we are using the marker command, which will create the circle shape with red colour at markersize 20 mm
plot(X1,Y1,'o','markersize',20,'markerfacecolor','r');

adding title to the frame with tittle comman
title('simple pendulum');

using the get frame command
M(ct) = getframe(gcf);
And ct = ct=1

Closing the for loop

Now using the movie command for creating the movie

movie(M);
videofile  = VideoWriter('Simple Pendulum','Uncompressed AVI');

videofile command is for creating the video file name simple pendulum in a uncompressed AVI file

open(videofile);
writeVideo(videofile,M);
close(videofile);

Opening the videofile the creating the video into that file and closing the videofile

Then the video file will create the video in the same location
Program for the simple pendulum and creating the animation video


close all
clear all
clc

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

%intial condition
theta_0 = [0,;5];

%time points
t_span = linspace(0,20,150)

% solve ODE 

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

plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')


X0 = 0
Y0 = 0
ct=1

for i = 1:length(t)
X1 = l*sin(results(i,1))
Y1 = -l*cos(results(i,1))

    figure(2)
    
    
    plot([-1 1],[-0,0],'LineWidth',6,'Color','g');
    axis([-2 2 -2 2]);
    line([X0,X1],[Y0,Y1],'linewidth',3,'color','b');
    hold on;  
    plot(X1,Y1,'o','markersize',20,'markerfacecolor','r');    
    title('simple pendulum');

    hold off;

    M(ct) = getframe(gcf);
    ct = ct+1;

end

movie(M);
videofile  = VideoWriter('Simple Pendulum','Uncompressed AVI');

open(videofile);
writeVideo(videofile,M);
close(videofile);
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.