Challenge

Week 3 - Solving second order ODEs

AIM;

To solve the 2nd order ode equation, with the help of matlab program. To find out the a simple pendulam motion.

OBJECTS;

ODE45, which is inbuilt matlab solver. A simple pendulam motion move forward and reversee.

It contains,

L = length of spring, m

G = gravity,

M = mass, kg

B = damping coefficient,

d^2θ/dt^2 + b/m*dθ/dt +g/lsinθ=0

θ=θ1.

transfering to, dθ/dt = dθ1/dt = θ2. -------------------- 1

d^2θ/dt^2 = d^2θ1/dt^2 = d/dt(dθ1/dt) = θ2--2

Now we go to orginal equation,

d^2θ/dt^2 + b/m*dθ/dt +g/l*sinθ=0

d^2θ1/dt^2 + b/m*dθ1/dt +g/l*sinθ1=0

dθ2/dt + b/m*dθ2 +g/l*sinθ=0

dθ2/dt = + b/m*θ2 + g/l*sinθ

Then, we create 2 ode

1------------- dθ1/dt = θ2

2---------------dθ2/dt = - b/m*θ2 - g/l*sinθ

we will brak the equation for matric formation,

d/dt(θ1 θ2), = (θ2 - b/m*θ2 - g/l*sinθ)

we will go to program,

clear all
close all
clc

% inputs  
b=0.05
g=9.81 m/s^2
l=1 m
m=0.1 kg

% intial time for pendulam position 
theta = [0;5] angular displacement, velocity

% time 
t_span = linspace(0,20,500) time for intial condition 0 to 20 sec for 500 value.
 
we enter the ode equ 
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta)
ode45 inbuilt solver.
 we find the ode_func

function code 

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)

then we make the plot for 2 outputs
figure(1)
plot(t,results(:,1),'linewidth',3,'color','r')
hold on
plot(t,results(:,2),'linewidth',3,'color','b')
grid on
xlabel('time')
ylabel('plot')
titile('angular displacement vs velocity')
lengend('displacement', 'velocity')

we got output for this function.

now, we define the pendulam movement,

x=results(:,2)

ct=1

use for loop for find the theta value.

for i=1:length(x)

theta1=results(i)

% coordinates intial stage of pendulam x0=0, y0=0

x1=lsind(theta1)

y1=-lcosd(theta1) we use nagative value this direction,

this error theta1=results(x) wroung inputs enter,

theta1=results(i) correct value

we make the plot and animation for pendulam

plot([x0,x1],[y0,y1],'linewidth',10,'color','b','marker','o',markersize',25,'markerfacecolor','m')

we correct the axis value axis([-.12 .12 -1 1[)

pause(0.025)

ct=ct+1

M(ct)=getframe(gcf)

end

% create animation and save the movie

movie(M)

videofile=writevideo('ode_equ', 'uncompressed.avi')

open(videofile)

writevideo(videofile,M)

close(videofile)

https://drive.google.com/file/d/122KQmpZl8eSRRmdkP-JUl5D7XGA9zAKA/view?usp=sharing

error line no 51 and 52 interchaged.

this is correct.

animation video

https://drive.google.com/file/d/1UNWx5aixkqyi4A8DHLo234FkJM0Oufl_/view?usp=sharing
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.