Challenge

Week 4.1 - Solving second order ODEs

Aim

To write programe for the given equation for pendulam with damping

Before writen the program we have to understand newton second law

The acceleration of an object as produced by a net force is directly proportional to the magnitude of the net force, in the same direction as the net force, and inversely proportional to the mass of the object.

Program for ODE Function
inputs
function [dtheta_dt] = ode_func(t,theta,b,g,l,m)
b = 0.05;
g = 9.81;
m = 1;
l = 1
theta1 = theta(1)
theta2 = theta(2)
dtheta1_dt = theta(2);
dtheta2_dt = -(b/m)*theta2-(g/1)*sin(theta1);
dtheta_dt = [dtheta1_dt;dtheta2_dt];
end
These function has given as inputs for the pendulum
Program for pendulum
%inputs
b = 0.05; %Damping Coefficient
g = 9.81; %Gravity m/s^2
m = 1; %Mass of the Pendulum in kg
l = 1; %Length of the string attached to pendulum in meters
%initial conditions
theta_0 = [0;3];
t_span = linspace(0,20,100);
[t,result] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%plotting
figure(1)
subplot(2,1,1)
p1 = plot (t,result(:,1),'color','k')
xlabel('Time')
ylabel('Theta')
title('theta v/s Time')
hold on
subplot(2,1,2)
p2 = plot(t,result(:,2),'color','r')
xlabel('Time')
ylabel('Velocity')
title('Angular Velocity v/s Time')
legend([p1(1),p2(1)],'angular displacement','angular velocity')
theta = result(:,1)
ct=1;
for i=1:length(result)
THETA = result(i,1);
x0=0;
y0=0;
x1= l*sin(THETA);
y1= -l*cos(THETA);
figure(2)
hold on
plot([x0 x1],[y0 y1],'linewidth',2);
plot(x1,y1,'-o','markersize',30,'markeredgecolor','k','markerfacecolor',[0.4 0.4 0.4]);
plot(x0,y0,'-*','markersize',3,'markeredgecolor','c','markerfacecolor',[0.4 0.4 0.4]);
axis([-2 2 -2 0]);
pause(0.005);
M(ct)= getframe(gcf)
ct=ct+1;
clf()
end
movie(M);
videofile = VideoWriter('second_ode_pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
after the inputs of
b = 0.05; %Damping Coefficient
g = 9.81; %Gravity m/s^2
m = 1; %Mass of the Pendulum in kg
l = 1; %Length of the string attached to pendulum in meters
are given the results are derived with the ODE function to get the pendulum and after that we can create the movie file of the pendulum
outputs
Drive link
https://drive.google.com/file/d/1FlxHnJrRnKdLPtjc4S9_8nmYJthk9mgo/view?usp=drivesdk
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.