Challenge

Week 3 - Solving second order ODEs

programming for animated simple pendulum:

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

%initial condition
theta_0 = [0;3];

%time points

t_span = linspace(0,20,100);

%solve ODE
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0)
%ploting
plot(t,results(:,1));
hold on
plot(t,results(:,2));
xlabel('time')
ylabel('plot')
grid on
title('angular displacement and angular velocity w.r.t time','color','r')


x = results(:,1);
ct = 1;
for i = 1:length(x)
    x1 = l*sin(x(i));
    y1 = -l*cos(x(i));
    x0 = 0;
    y0 = 0;

    figure(2)
    plot([-0.5 0.5],[0 0],'linewidth',5,'color','g')
    hold on
    plot([x0 x1],[y0 y1],'linewidth',5,'color','black')
    plot(x1,y1,'o','markersize',15,'markerFacecolor','y')
    axis([-2 2 -2 2])
    title('damping of simple pendulum')
    legend('support rod','length of pendulum','bob')
    hold off
    pause(0.02);
    M(ct) = getframe(gcf)
    ct = ct+1;
end

   
movie(M)
videofile=VideoWriter('simple pendulum.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)

function for ode45:

function [dtheta_dt] = ode_func(t,theta,b,g,l,m)

theta1 = theta(1)
theta2 = theta(2)
dtheta1_dt = theta(2);
dtheta2_dt = -(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt = [dtheta1_dt;dtheta2_dt];

end

explanation:

1.firstly 2nd order ODE is converted into first order ODE by using ode function

putting the input parameters as given in the question as input valves

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

2.after that take angular displacement and angular velocity also time between 0 and 20 in intervals of 100 usimg linspace command

theta_0 = [0;3]

t_span = linspace(0,20,100)

3.to solve the ODE function using the following inbuilt command

ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0)

4.for loop is used for getting automatic valves instead of manual valves

5.using plot command we have to drawn the supporting rod length of pendulum using different colors for indication

using markersize command i have drawn the bob with diameter and for color i have used markerFaceColor

6.hold off is used for not repeating the length of pendulum when it is oscillating and ct command is used for counting the lines one to another

7.for animation we have to use movie(M) command

movie(M)
videofile=VideoWriter('simple pendulum.avi','uncompressed AVI')- it is used for file name in avi format
open(videofile)- it opens the file
writeVideo(videofile,M)- it is used to writes the video file

close(videofile)-closes the videofile

8.legend command used for indicating the bob supporting rod and length of the pendulum in graph

9.title command used for giving the title in graph

errors:

i have used marker instead of markersize and after that the i have got the plot

syntax error

youtube link :

https://youtu.be/jM_9LxhaF_Q

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.