Project

Pendulum Simulation

Given the motion of simple pendulum we convert the second order ODE to First order ODE as follows. The equations then can be solved in matlab.

x(1) = `\\theta`

x(2) = `\\dot{\\theta}`

`\\frac{d\\theta}{dt}` = x(2)

`\\frac{d(x(2))}{dt} = \\frac{d^2\\theta}{dt^2} = -k*x(2)-r*sin(\\theta)`

Matlab Code

clc
clear all
close all

%% The system parameters
m = 1;
l = 1;
b = 0.05;
g = 9.81;

%% for simplification%
r = g/l;
k = b/m*l;

%% The pendulum ode
f =@(t,x)[x(2);-k*x(2)-r*sin(x(1))];

%% initial condition
%init = [0; 0];
t = [0 20];
x0 = [0 3];

%% Solve the ode 

[t, x] = ode45(f, t, x0);

%plot the result
%plot(t, x(:,1));
%grid on; hold on;
%plot(t, x(:,2), \'k\');
%xlabel(\'Time (s)\');
%ylabel(\'Amplitude\');
%legend({\'Angle (rad)\',\'Angular speed (rad/s)\'});

%% Animation
O = [0, 0];
axis(gca, \'equal\');
axis([-2 2 -2 2]);
grid on;

% Loop for animation

for i=1:length(t)
    %Mass point
    P = l*[sin(x(i, 1)) -cos(x(i, 1))];
    
    % Circle in origin
    O_circle = viscircles(O, 0.01);
    
    %pendulum
    pend = line([O(1) P(1)], [O(2) P(2)]);
    %ball
    ball = viscircles(P, 0.05);
    
    pause(0.001);
    if i<length(t)
        delete(ball);
        delete(O_circle);
        delete(pend);
    end
end

Result:

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.