Challenge

Week 3 - Solving second order ODEs

clear all
close all
clc

%Inputs

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

%Initial Conditions

theta_0 = [0;3];

%Time points

t_span = linspace(0,20,200);

%Solve ODE

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

%Plotting displacement and Angular velocity variation

figure(1)
plot (t,results(:,1))
hold on
plot (t, results(:,2))
xlabel('Time')
ylabel('Plot')
legend('Dispalacement','Angular Velocity')

%Using for loop for making pendulum animation

angle = results(:,1);
ct = 1;
for i = 1:length(angle)
alpha = angle(i);
figure (2)
title('Motion of Pendulum')

%Coordinates for the pendulum - fixed end and moving end

x0 = 0;
y0= 0;

x1 = l*sin(alpha);
y1 = -(l*cos(alpha));

plot([x0 x1],[y0 y1],'linewidth',3)
hold on
plot (x1,y1,'o','markersize',20)
hold off
axis([-2 2 -1.2 0])
grid on
pause (0.1)
M(ct)= getframe(gcf);
ct = ct+1;

end

%Making animation movie

movie(M)
videofile = VideoWriter('Motion_of_Pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)


%Error faced

%1) Pause command was writen after the contour command
%2) forgot to put - sign in y1 value
%3) forgot to write hold on command

%ODE pendulum vedio link

https://youtu.be/h8-FiO3w3h4

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.