Challenge

Week 3 - Solving second order ODEs

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

clear all
close all
clc

% Inputs
b=0.05
g=9.81
l=1
m=1
x0=0
y0=0

% Initial condition
theta_0=[0;3]

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

% solve ode
[t1,results]=ode45(@(t,theta) ode_function(t,theta,b,g,l,m),t_span,theta_0);

figure(1)
% plot
plot (t1,results(:,1))
hold on
plot (t1,results(:,2))
ylabel('plot')
xlabel('time')
ct=1
for i = 1:length(t1)
   x1=l*sin(results(i,1));
   y1=-l*cos(results(i,1));
   figure(2)
   plot([x0,x1],[y0,y1])
   hold on
   plot(x1,y1,'marker','.','markersize',50)
   axis([-1 1 -2 0])
   M(ct)=getframe(gcf)
   ct=ct+1
   hold off
   
end
movie(M)
videofile = VideoWriter('pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

2. ode_function

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

3. Figure 1

4. explanation

i, line.no.5 to 11 - given inputs

ii, line.no.13-initial condition in theta 0 values - displacement and velocity (0,3)

iii, line.no. 16-17 - time points - using t_span for linspace command (0-20 sec with 100 values)

iv, line.no. 19-20 - to find the time value of (0-20sec), at initial to end (0-20sec) to find the displacement & velocity values for figure out the simple pendulum. while using ode45 method.

ode45 needs 2 value's - so we give time and theta (ref. line.no.14)

v, line.no.20 - ode function to derive the theta's 2 value for using for loop method.

1. dtheta1_dt=theta2

2. dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1)

vi, line no. 20-28 - to create a plot for time and displacement, where x = time, y = displacement

vii, line.no.29-42 - to create a animation for simple pendulum process, using for loop method and at the free end of thread will going to fix the bob while using marker command

viii, line.no.43-47 - to converts this process to AVI video file. refer the above attached link.

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.