Challenge

Solving second order ODEs by Nishant Dutt Mishra

AIM:-

To solve the governing differential equation of a pendulum and executing its simulation.

GOVERNING EQUATION:-

The governing equation of a pendulum is a second-order differential equation, consisting of variables like mass of bob 'm', damping coefficient 'b', link length 'l', and gravity 'g'.

*All quantities are in SI units;

here is the governing equation:-

theta - angular velocity in radians;

t - time;

OBJECTIVES:-

  • To deduce the ODE to its state-space form.
  • To create a program to solve the differential equation.
  • To simulate the pendulum as per the constraints in the program.

In the first program, we have made use of a variable 'theta' which contains two variables theta1 and theta2. Then, the derivative of theta1 has been assigned to theta2 and the latter is used for the subsequent equations to get the state space form i.e. first-order derivative.

here is the code for the first program:-

%deducing the ODE to state space form
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

In the next program, we have used the variables like mass of bob 'm', damping coefficient 'b', link length 'l', acc. due to gravity 'g'.

The initial conditions and time span has been provided. Then 'ode45' solver has been used to solve the differential equation. A counter has also been used to store the individual frames. The value of the counter keeps incrementing by one at the end of each loop iteration.

Then the configuration of the pendulum has been provided and each configuration has been stored in an array. And then, that array has been transformed into a movie.

Here is the second program:-

clear all
close all
clc

%declaring the physical aspect variables
b = .05;
g = 9.81;
l = 1;
m =1;

%initial conditions
theta_0 = [0;1];

%time span
t_span = linspace(0, 20, 200);

%solving ODE
[t, results] = ode45(@(t, theta) ode_func(t, theta, b, g, l, m), t_span, theta_0);
figure(1)
plot(t, results(:,1))
hold on
plot(t, results(:,2))
ylabel('plot')
xlabel('time')

%configuration of pendulum
x0 = 0;
y0 = 0;
ct = 1;
for i = 1:length(results(:,1))
    THETA = results(i,1);
    
    x1 = l*sin(THETA);
    y1 = -l*cos(THETA);
    
    figure(2)
    plot([x0, x1],[y0, y1], 'linewidth' , 3)
    hold on
    plot(x1, y1, '-ko', 'MarkerSize',12)
    hold off
    axis([-1 1 -1.5 1.5])
    N(ct) = getframe(gcf);
    ct = ct+1;
    
end
%creating a movie from the array 'N' and then saving it in a video file
movie(N)
video2 = VideoWriter('pendulum.avi' , 'Uncompressed AVI');
open(video2)
writeVideo(video2,N)
close(video2)

Here is the error that took place:-

Here is the output plot of angular displacement in 'blue' and velocity in 'red':-

here is the link for the pendulum simulation:-

https://drive.google.com/file/d/1hrCxtO2h10ZdSTGgH4gPbTeBOoSRLppx/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.

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.