Challenge

Week 3 - Solving second order ODEs

Pendulum Code

clear all
close
clc

%constant values
b=0.05;
g=9.81;
l=1;
m=0.1;
%initial conditions
theta_0 = [0;3];

%time points
t_span = linspace(0,20,500);

%solving ODE
[t,result] =  ode45(@(t,theta) ode_fun(t,theta,b,g,l,m),t_span,theta_0);

ct=1;
angle = [result(:,2)]
for i=1:length(angle)
    ANGLE = angle(i);
x1=5;
y1=5;

x2= x1+l*sin(ANGLE);
y2= y1-l*cos(ANGLE);

%plotting pendulum
figure(1)
plot([x1 x2],[y1 y2])
hold on
plot(x2,y2,'Marker','o','MarkerSize',10)
axis ([3.5 6.5  3.5 6.5])
hold off
M(ct) = getframe(gcf)
ct=ct+1;
end   

Videofile = VideoWriter('Pendulum.avi','Uncompressed AVI');
open(Videofile)
writeVideo(Videofile,M)
close(Videofile)

%plotting graph
figure(2)
plot(t,result(:,1))
hold on 
plot(t,result(:,2))
xlabel('time')
ylabel('plot')

Function Code

function [dtheta_dt] = ode_fun(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

Pendulum Animation

https://drive.google.com/file/d/1UV1Ux3rZ3oZn6Z_VNqhNJFAVZ4dH4mi-/view?usp=sharing

Output Graph

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.