Challenge

Week 3 - Solving second order ODEs

## SECOND ORDER ODE

% Program to solve second order ODEs

clear all
close all
clc

% Inputs

% Gravity [m/s^2]
g = 9.81;

%Damping Coeff.
b = 0.05;

%Length of pendulum
l = 1;

%mass of the ball [kg]
m = 1;

%Time Points
t_span = linspace(0,20,500);

%Initial conditions
theta_0 = [0;5];

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

ct=1;

for i = 1:length(t_span)

x0 = 0;
y0 = 0;

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

x3 = -0.8;
y3 = 0;

x4 = 0.8;
y4 = 0;


% Plotting the graph
plot([x0 x1],[y0 y1],'linewidth',2)
hold on
p = plot(x1,y1,'o','MarkerFaceColor','red','MarkerSize',15);
plot([x3 x4],[y3 y4],'linewidth',15)
xlabel('Time','color','b')
ylabel('Plot','color','r')
hold off
axis([-3 3 -3 3])
title('simple pendulum','color','r')
pause(0.3)
M(ct) = getframe(gcf);
ct = ct+1;
grid on
end

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

plot(t,results(:,1))
hold on
plot(t,results(:,2))
grid on

## ODE FUNCTION

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

DRIVE LINK FOR PENDULUM ASSIGNMENT:-

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

YOUTUBE LINK :-

https://youtu.be/gvKxj-yLlfQ

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.