# function pendulum;

% Nonlinear dynamics of a double pendulum representing a child on a swing
tic
A.M
A.m
A.L
A.l
A.g

=
=
=
=
=

30; % kg, child's mass
3; % kg, swinging shin mass
3; % m, swing length
0.3; % m, shin length
9.8; % m s-2, g

METHODs = [1 1 1 1];
aaa = [0.1, 1.5]; %rad% shin swinging magnitude
iA = 1; % computation counter -reset
iF = 1; % figure counter -reset
A(iA).WW = sort(unique([0.1:0.05:5, ...
0.45:0.01:0.55, ...
0.9:0.01:1.1, ...
1.9:0.01:2.1])); % reduced (w/w0) swinging frequency range
A(iA).tt0 = [0:0.1:200]; % reduced (t/T) time array
if iA>1, A(iA) = A; end
for ia = 1:length(aaa), ia,
w0 = sqrt(A(iA).g/A(iA).L); % swing's small oscillation frequency
A(iA).a = aaa(ia); % rad, forcing magnitude
T0 = 2*pi/w0;
A(iA).c = 5; % 0.01 * (2*w0*(A(1).M+A(1).m)*A(1).L^2) %damping term
if METHODs(1),
YY = []; % swing's state storage array -reset
TT = []; % integration time array -reset
% LINEAR COMPUTATION ( y'(t) = odefun1(t,y) )
A(iA).title = 'L_{12} = m l L \theta'' \vartheta''';
for w = A(iA).WW*w0;
%disp(num2str(w));
A(iA).w = w;
T = 2*pi/w;
[T,Y] = ode45(@odefun1, A(iA).tt0*T, [0; 0.01], [], A(iA));
YY = [YY Y(:,1)];
TT = [TT T*w]; % normalized time array
end;
% results storage
A(iA).TT = TT;
A(iA).YY = YY;
% results plot
iF = res_plot(A(iA),iF,iA);
toc
iA = iA+1; A(iA) = A(iA-1);
end;
1

if METHODs(2),
YY = [];
TT = [];
% NONLINEAR COMPUTATION ( y'(t) = odefun2(t,y) )
A(iA).title = 'L_{12} = m l L \theta'' \vartheta'' cos(\theta-\vartheta )';
for w = A(iA).WW*w0;
%disp(num2str(w));
A(iA).w = w;
T = 2*pi/w;
[T,Y] = ode45(@odefun2, A(iA).tt0*T, [0; 0.01], [], A(iA));
YY = [YY Y(:,1)];
TT = [TT T*w];
end;
% results storage
A(iA).TT = TT;
A(iA).YY = YY;
% results plot
iF = res_plot(A(iA),iF,iA);
toc
iA = iA+1; A(iA) = A(iA-1);
end;
if METHODs(3);
YY = [];
TT = [];
% PARAMETRIC EXCITATION WITHOUT DIRECT KINEMATIC EXCITATION
% ( y'(t) = odefun3(t,y) )
A(iA).title = 'linear pendulum, variable length';
for w = A(iA).WW*w0;
%disp(num2str(w));
A(iA).w = w;
T = 2*pi/w;
[T,Y] = ode45(@odefun3, A(iA).tt0*T, [0; 0.01], [], A(iA));
YY = [YY Y(:,1)];
TT = [TT T*w];
end;
% results storage
A(iA).TT = TT;
A(iA).YY = YY;
% results plot
iF = res_plot(A(iA),iF,iA);
toc
iA = iA+1; A(iA) = A(iA-1);
end;
if METHODs(4);
YY = [];
TT = [];

2

% PARAMETRIC EXCITATION OF NONLINEAR SYSTEM WITHOUT DIRECT KINEMATIC EXCITATION
% ( y'(t) = odefun4(t,y) )
A(iA).title = 'nonlinear pendulum, variable length';
for w = A(iA).WW*w0;
%disp(num2str(w));
A(iA).w = w;
T = 2*pi/w;
[T,Y] = ode45(@odefun4, A(iA).tt0*T, [0; 0.01], [], A(iA));
YY = [YY Y(:,1)];
TT = [TT T*w];
end;
% results storage
A(iA).TT = TT;
A(iA).YY = YY;
% results plot
iF = res_plot(A(iA),iF,iA);
toc
iA = iA+1; A(iA) = A(iA-1);
end;
save(mfilename);
end;
function ff = odefun1(t,y,A);
% linear double penulum
% y = [x; x'], A y' + B y + f

= 0

M = A.M; m = A.m; L = A.L; l = A.l; g = A.g;
c = A.c*(1+exp(-t/(A.tt0(end)*2*pi/A.w)*10));
% swinging waveform and derivatives
a = A.a; w = A.w;
th0 = a*sin(w*t); % \vartheta
th1 = a*w*cos(w*t); % \dot{\vartheta}
th2 = -a*w^2*sin(w*t); % \ddot{\vartheta}
AA = (M+m)*L^2*[1, 0; 0 1];
BB = [0, -(M+m)*L^2; (M+m)*L*g, c];
f = [0; m*l*L*th2];
ff = AA\(-BB*y - f);
function ff = odefun2(t,y,A);
% nonlinear double pendulum
% y = [x; x'], A y' + B y + f

= 0

M = A.M; m = A.m; L = A.L; l = A.l; g = A.g;
c = A.c*(1+exp(-t/(A.tt0(end)*2*pi/A.w)*10));
% swinging waveform and derivatives
a = A.a; w = A.w;
th0 = a*sin(w*t); % \vartheta
th1 = a*w*cos(w*t); % \dot{\vartheta}
th2 = -a*w^2*sin(w*t); % \ddot{\vartheta}
AA = (M+m)*L^2*[1, 0; 0 1];
BB = [0, -(M+m)*L^2; 0, c];
f = [0; m*l*L*(th2*cos(y(1)-th0) + th1^2*sin(y(1)-th0)) ...
+ g*((M+m)*L*sin(y(1)))];

3

ff = AA\(-BB*y - f);
function ff = odefun3(t,y,A);
% linear pendulum, variable length
% y = [x; x'], A y' + B y + f = 0
M = A.M; m = A.m; L = A.L; l = A.l; g = A.g;
c = A.c*(1+exp(-t/(A.tt0(end)*2*pi/A.w)*10));
% excitation amplitude and frequency
a = A.a; w = A.w;
AA = (M+m)*L^2*[1, 0; 0 1];
BB = [0, -(M+m)*L^2; g*(M+m)*(L + l*a*sin(w*t)), c];
ff = AA\(-BB*y);
function ff = odefun4(t,y,A);
% nonlinear pendulum, variable length
% y = [x; x'], A y' + B y + f = 0
M = A.M; m = A.m; L = A.L; l = A.l; g = A.g;
c = A.c*(1+exp(-t/(A.tt0(end)*2*pi/A.w)*10));
% excitation amplitude and frequency
a = A.a; w = A.w;
AA = (M+m)*L^2*[1, 0; 0 1];
BB = [0, -(M+m)*L^2; 0, c];
f = [0; g*((M+m)*(L + l*a*sin(w*t))*sin(y(1)))];
ff = AA\(-BB*y - f);
function ff = res_plot(A,iF,iA);
% results plot
WW = A.WW;
TT = A.TT; T = TT(:,end);
YY = A.YY;
it = round(0.8*length(A.tt0)):length(A.tt0);
figure(iF);
plot(WW,max(YY(it,:))); xlabel('\omega/\omega_0'); ylabel('\theta_{max}');
title(['a = ' num2str(A.a) ', ' A.title]);
saveas(gcf,[mfilename '_yw_' num2str(iA)],'png');iF = iF+1;
figure(iF);
mesh(WW,T(it),YY(it,:));view([1,0,0])
title(['a = ' num2str(A.a) ', ' A.title]);
xlabel('\omega/\omega_0'),ylabel('\omega t'); zlabel('\theta');axis tight
saveas(gcf,[mfilename '_tyw_' num2str(iA)],'png');iF = iF+1;
figure(iF);
df = 2*pi/((TT(it(end),1)-TT(it(1),1))); ff = (1:length(it))*df; %?????
FFYY = 2*fft(YY(it,:))/length(it); iii = 1:(0.4*length(it));
contourf(WW,ff(iii),log(abs(FFYY(iii,:)))); colorbar;
title(['FFT: ln(\Theta), a = ' num2str(A.a) ', ' A.title]);
xlabel('\omega/\omega_0'),ylabel('2\pi f/\omega'); zlabel('\Theta');
saveas(gcf,[mfilename '_fft_ln_' num2str(iA)],'png');iF = iF+1;
figure(iF);
contourf(WW,ff(iii),(abs(FFYY(iii,:)))); colorbar;
title(['FFT: \Theta, a = ' num2str(A.a) ', ' A.title]);
xlabel('\omega/\omega_0'),ylabel('2\pi f/\omega'); zlabel('\Theta');
4

saveas(gcf,[mfilename '_fft_' num2str(iA)],'png');iF = iF+1;
ff = iF;

ia =
1
Elapsed
Elapsed
Elapsed
Elapsed

time
time
time
time

is
is
is
is

88.959240 seconds.
187.263104 seconds.
254.932947 seconds.
322.810583 seconds.

time
time
time
time

is
is
is
is

434.394929
575.254006
651.284290
729.750086

ia =
2
Elapsed
Elapsed
Elapsed
Elapsed

seconds.
seconds.
seconds.
seconds.

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Published with MATLAB® 7.9

21