Project

Simulation of pendulum by solving second order ODE using MATLAB

Objective:

To write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping, and to animate it to have a swinging pendulum

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

Procedure:

Given,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

Also,

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2.

Now, Simulating the motion between 0-20 sec, for angular displacement=0, angular velocity=0, at the time t=0.

First of all, Breaking the Second order ODE into simple ODE,

taking,

`theta=y1`

then,

`dy1//dt=y2`

`d^2y1//dt^2=-[(b/m)y1+g//l*sintheta]`

Now, Coding the above equations and solving it using lsode,

The function to solve \'y\' values:

function [ydot]=ode_pendulum(t,y)

%Inputs
l=1;     %length in metres
m=1;    %mass in kg
b=0.05;  %Damping coefficient
g=9.81;  %acceleration due to gravity in m/s^2

ydot=zeros(2,1);

%Change of displacement
ydot(1)=y(2);

%Change of velocity
ydot(2)=-(b/m*y(2)+g/l*sin(y(1)));

end

The Main code to simulate the equation as pendulum:

    clear all
    close all   
    clc
    
    %Input variables
    t=[0 20];%Time period
    y=[0 3]; %Representation of Displacement
    
    [y]= lsode(\'ode_pendulum\',y,t);
    
    
    figure(1)
    plot(t,y(:,1),\'color\',\'r\',\'linewidth\',2)
    xlabel(\'Time\')
    hold on    
    plot(t,y(:,2),\'color\',\'b\',\'linewidth\',2)
    xlabel(\'Time\')
    legend(\'Angular displacement\',\'Angular velocity\')
   
    %Animation

    figure(2)
    ct=1
    for i=1:length(y(:,1))
     x0=0;
     y0=0;
     x1=1*sin(y(i,1));
     y1=-1*cos(y(i,1));
     
     plot([-0.5,0.5],[0,0],\'linewidth\',2,\'color\',\'y\');
     line([x0,x1],[y0,y1],\'linewidth\',2,\'color\',\'b\');
     hold on
     plot(x1,y1,\'O\',\'markers\',25,\'MarkerFacecolor\',\'g\',\'MarkerEdgecolor\',\'k\');
     axis([-1.5 1.5 -1.5 1.5]);
     pause(0.02);
     hold off
     M(ct)=getframe(gcf);
     ct=ct+1;
    end
    
    %Movie
    movie(M)
    videofile=VideoWriter(\'pendulum\',\'Uncompressed AVI\')
    open(videofile)
    writeVideo(videofile,M)
    close(videofile)

The Output:

%Output
y =

         0    3.0000
    0.0001    3.0000
    0.0001    3.0000
    0.0002    3.0000
    0.0002    3.0000
    0.0005    3.0000
    0.0007    3.0000
    0.0010    3.0000
    0.0012    2.9999
    0.0025    2.9999
    0.0037    2.9998
    0.0050    2.9997
    0.0062    2.9996
    0.0125    2.9991
    0.0188    2.9985
    0.0251    2.9977
    0.0313    2.9968
    0.0627    2.9904
    0.0939    2.9808
    0.1251    2.9681
    0.1560    2.9521
    0.2882    2.8471
    0.4143    2.6871
    0.5320    2.4775
    0.6391    2.2251
    0.7685    1.8122
    0.8698    1.3481
    0.9401    0.8502
    0.9780    0.3338
    0.9778   -0.3428
    0.9219   -1.0039
    0.8126   -1.6203
    0.6551   -2.1583
    0.4816   -2.5403
    0.2844   -2.8011
    0.0731   -2.9182
   -0.1414   -2.8822
   -0.3366   -2.7110
   -0.5156   -2.4198
   -0.6706   -2.0287
   -0.7958   -1.5632
   -0.8860   -1.0559
   -0.9402   -0.5190
   -0.9569    0.0305
   -0.9359    0.5769
   -0.8737    1.1353
   -0.7723    1.6536
   -0.6352    2.1081
   -0.4683    2.4737
   -0.2732    2.7311
   -0.0643    2.8445
    0.1473    2.8024
    0.3503    2.6103
    0.5082    2.3424
    0.6465    1.9946
    0.7604    1.5842
    0.8468    1.1293
    0.9091    0.5756
    0.9304    0.0049
    0.9099   -0.5638
    0.8486   -1.1119
    0.7604   -1.5725
    0.6435   -1.9830
    0.5017   -2.3249
    0.3403   -2.5803
    0.1424   -2.7453
   -0.0622   -2.7649
   -0.2628   -2.6360
   -0.4489   -2.3717
   -0.5858   -2.0657
   -0.7019   -1.6967
   -0.7937   -1.2803
   -0.8588   -0.8318
   -0.9001   -0.2589
   -0.8978    0.3213
   -0.8521    0.8879
   -0.7648    1.4191
   -0.6538    1.8479
   -0.5161    2.2087
   -0.3567    2.4816
   -0.1823    2.6501
    0.0130    2.7014
    0.2068    2.6140
    0.3893    2.3940
    0.5516    2.0604
    0.6709    1.6974
    0.7655    1.2832
    0.8326    0.8336
    0.8705    0.3634
    0.8760   -0.2266
    0.8356   -0.8057
    0.7509   -1.3495
    0.6260   -1.8317
    0.4839   -2.1903
    0.3199   -2.4531
    0.1412   -2.6017
   -0.0436   -2.6259
   -0.2251   -2.5248
   -0.3953   -2.3060
   -0.5462   -1.9844
   -0.6717   -1.5818
   -0.7625   -1.1491
   -0.8233   -0.6818
   -0.8524   -0.1959
   -0.8491    0.2938
   -0.8075    0.8288
   -0.7273    1.3308
   -0.6118    1.7768
   -0.4660    2.1432
   -0.3020    2.4007
   -0.1237    2.5415
    0.0602    2.5541
    0.2404    2.4385
    0.3943    2.2294
    0.5314    1.9344
    0.6467    1.5693
    0.7363    1.1523
    0.7999    0.6744
    0.8296    0.1756
    0.8243   -0.3271
    0.7843   -0.8172
    0.7155   -1.2579
    0.6187   -1.6560
    0.4975   -1.9944
    0.3562   -2.2568
    0.1865   -2.4389
    0.0081   -2.4992
   -0.1700   -2.4323
   -0.3391   -2.2446
   -0.4680   -2.0046
   -0.5804   -1.7014
   -0.6728   -1.3477
   -0.7427   -0.9572
   -0.7937   -0.4671
   -0.8091    0.0381
   -0.7884    0.5400
   -0.7324    1.0208
   -0.6544    1.4184
   -0.5523    1.7696
   -0.4297    2.0592
   -0.2909    2.2735
   -0.1223    2.4104
    0.0518    2.4270
    0.2226    2.3213
    0.3818    2.1025
    0.5021    1.8407
    0.6047    1.5213
    0.6863    1.1571
    0.7447    0.7616
    0.7822    0.2671
    0.7834   -0.2356
    0.7483   -0.7277
    0.6785   -1.1903
    0.5883   -1.5662
    0.4753   -1.8861
    0.3437   -2.1347
    0.1985   -2.2992
    0.0314   -2.3717
   -0.1365   -2.3270
   -0.2971   -2.1676
   -0.4426   -1.9060
   -0.5522   -1.6092
   -0.6416   -1.2615
   -0.7081   -0.8764
   -0.7498   -0.4672
   -0.7660    0.0341
   -0.7451    0.5324
   -0.6878    1.0075
   -0.5969    1.4388
   -0.4887    1.7750
   -0.3602    2.0406
   -0.2167    2.2208
   -0.0642    2.3054
    0.0981    2.2860
    0.2550    2.1570
    0.3991    1.9267
    0.5240    1.6111
    0.6163    1.2669
    0.6853    0.8822
    0.7287    0.4712
    0.7452    0.0474
    0.7298   -0.4546
    0.6770   -0.9354
    0.5889   -1.3727
    0.4700   -1.7439
    0.3401   -2.0057
    0.1952   -2.1783
    0.0417   -2.2504
   -0.1132   -2.2180
   -0.2622   -2.0841
   -0.3987   -1.8574
   -0.5165   -1.5509
   -0.6111   -1.1826
   -0.6770   -0.7853
   -0.7154   -0.3624
   -0.7251    0.0710
   -0.7059    0.5006
   -0.6575    0.9197
   -0.5818    1.3042
   -0.4814    1.6378
   -0.3606    1.9049
   -0.2205    2.0950
   -0.0706    2.1874
    0.0822    2.1752
    0.2305    2.0604
    0.3551    1.8752
    0.4657    1.6223
    0.5585    1.3134
    0.6304    0.9619
    0.6823    0.5487
    0.7052    0.1171
    0.6983   -0.3170
    0.6620   -0.7384
    0.6028   -1.1076
    0.5211   -1.4388
    0.4198   -1.7187
    0.3025   -1.9350
    0.1615   -2.0866
    0.0133   -2.1395
   -0.1350   -2.0898
   -0.2764   -1.9417
   -0.3876   -1.7433
   -0.4851   -1.4884
   -0.5658   -1.1873
   -0.6273   -0.8516
   -0.6721   -0.4374
   -0.6876   -0.0084
   -0.6733    0.4194
   -0.6298    0.8301
   -0.5675    1.1713
   -0.4852    1.4739
   -0.3856    1.7262
   -0.2721    1.9176
   -0.1331    2.0493
    0.0116    2.0840
    0.1553    2.0192
    0.2911    1.8598
    0.3963    1.6570
    0.4877    1.4017
    0.5626    1.1039
    0.6187    0.7744
    0.6585    0.3647
    0.6692   -0.0574
    0.6506   -0.4758
    0.6034   -0.8747
    0.5386   -1.2026
    0.4545   -1.4901
    0.3541   -1.7258
    0.2409   -1.8995
    0.1041   -2.0107
   -0.0370   -2.0269
   -0.1758   -1.9467
   -0.3060   -1.7761
   -0.4068   -1.5648
   -0.4932   -1.3033
   -0.5627   -1.0018
   -0.6131   -0.6711
   -0.6459   -0.2645
   -0.6500    0.1509
   -0.6250    0.5590
   -0.5723    0.9438
   -0.5027    1.2578
   -0.4148    1.5277
   -0.3115    1.7424
   -0.1966    1.8922
   -0.0616    1.9739
    0.0758    1.9625
    0.2091    1.8582
    0.3322    1.6685
    0.4273    1.4416
    0.5070    1.1679
    0.5688    0.8577
    0.6109    0.5222
    0.6336    0.1162
    0.6274   -0.2930
    0.5926   -0.6890
    0.5307   -1.0556
    0.4534   -1.3497
    0.3589   -1.5940
    0.2506   -1.7774
    0.1327   -1.8913
   -0.0006   -1.9301
   -0.1334   -1.8779
   -0.2594   -1.7378
   -0.3729   -1.5189
   -0.4597   -1.2678
   -0.5295   -0.9746
   -0.5800   -0.6506
   -0.6097   -0.3069
   -0.6173    0.0993
   -0.5958    0.5000
   -0.5462    0.8783
   -0.4708    1.2174
   -0.3823    1.4797
   -0.2785    1.6834
   -0.1635    1.8185
   -0.0422    1.8783
    0.0878    1.8565
    0.2133    1.7482
    0.3283    1.5596
    0.4278    1.3022
    0.5018    1.0182
    0.5566    0.6998
    0.5903    0.3587
    0.6019    0.0069
    0.5877   -0.3950
    0.5449   -0.7782
    0.4751   -1.1251
    0.3819   -1.4186
    0.2796   -1.6277
    0.1656   -1.7675
    0.0448   -1.8303
   -0.0776   -1.8131
   -0.1410   -1.7721
   -0.2026   -1.7097
   -0.2616   -1.6270
   -0.3173   -1.5252

The Plots:UI

The Simulation obtained as follows:

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.