Challenge

Week 3 - Solving second order ODEs

Second order differential equation on simple pendulam

d^2theta/dt^2 + b/m*(dtheta/dt) + g/l*sintheta = 0 ---eqn1

b = damping coefficient

m =mass in kg

g = gravity m/s^2

l = length in m

let theta = theta1

then dtheta/dt = dtheta1/dt =theta2

dtheta/dt = theta2 --eqn2

d^2theta/dt^2 = d^2theta1/dt^2 =d/dt(dtheta1/dt)

d^2theta/dt^2 =d(theta2 )/dt--eqn3

sub eqn 2&3 in 1

we get

d(theta2 )/dt-b/m*(theta2 ) + g/l*sinthetan1= 0

d(theta2 )/dt = -b/m*(theta2 ) - g/l*sintheta1

2nd order differential equation funtion program

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

Main program

clear 
close all
clc
%inputs
b = 0.05; %damping coefficient
g = 9.81; % accelleration due to gravity in m/s^2
l = 1; %length of string in m
m = 0.1;% mass of bob in kg

%initial condition
theta_0 = [0;3];%angular displacement  in rad and angular velocity in rad/sec
%timepoints
t_span = linspace(0,20,500); % motion between 0 to 20 sec

%solve ODE

[t,results] = ode45(@(t,theta)ode_function(t,theta,b,g,l,m),t_span,theta_0);
figure (1)
plot(t,results(:,1)) % plotting change in angular displacement
hold on
plot (t,results(:,2))% plotting change in angular velocity
xlabel('Time --sec')
ylabel('angular displacement and velocity')
legend('change in angular displacement','change in angular velocity')
 theta2 = results(:,1);
 ct = 1;

 for i = 1:length(theta2)
     THETA2 = theta2(i);

     x0 = 0;
     y0 = 0;

     x1 = l*sin(THETA2);
     y1 = -l*cos(THETA2);% cos value is -ve in 4th quadrent
     
     figure(2)
     plot([x0 x1],[y0 y1],'color','b','marker','o');
     grid on;
     axis([-1.5 1.5 -1.5 0.5])
     pause(0.003)
     M(ct) = getframe(gcf);
     ct = ct+1;

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

results

Errors

1)

It went off when I correct the plot command and by removing the hold off commaand

It gone when i corrected small letter 'm' to capital letter 'M'

YOUTUBE LINK

https://youtu.be/7j4lZyJOxn8

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.