You are on page 1of 75

Engineering

Dynamics
JERRY GINSBERG
Georgia Institute of Technology

Solutions to Chapter 8
Chapter 8, Page 1
Chapter 8, Page 2
Chapter 8, Page 3
Chapter 8, Page 4
Chapter 8, Page 5
Chapter 8, Page 6
Chapter 8, Page 7
Chapter 8, Page 8
Chapter 8, Page 9
Chapter 8, Page 10
Chapter 8, Page 11
Chapter 8, Page 12
Chapter 8, Page 13
Chapter 8, Page 14
Chapter 8, Page 15
Chapter 8, Page 16
Chapter 8, Page 17
Chapter 8, Page 18
Chapter 8, Page 19
Chapter 8, Page 20
Chapter 8, Page 21
Chapter 8, Page 22
Chapter 8, Page 23
Chapter 8, Page 24
Chapter 8, Page 25
Chapter 8, Page 26
Chapter 8, Page 27
Chapter 8, Page 28
Chapter 8, Page 29
Chapter 8, Page 30

% Set up initial conditions.


x0 = [0; 0; pi/2; 0; 0; 0];

options=odeset('RelTol',1e-8,'AbsTol',1e-8);
%---------------------------------------------------------------------------
-----
% Set start and end times for calling o.d.e. solver
t0 = 0;
tf = 60;
x_prime = @(t, x) Boat_accel_new(t, x, mass, kappa, H);

% Call Runge-Kutta ODE solver, then plot result


[t,x] = ode45(x_prime,[t0 tf], x0, options);

X_g = x(:,1);
Y_g = x(:,2);
theta = x(:,3);
speed = sqrt(x(:,4).^2 + x(:,5).^2);
N = length(t);
disp(['Final position: t = ', num2str(t(N))]);
disp([' X_G = ', num2str(X_g(N)), ', Y_G', num2str(Y_g(N)), ...
', theta =', num2str(theta(N))]);
figure(1)
plot(t,X_g,'k-', t,Y_g,'r:')
figure(2)
plot(X_g,Y_g,'K-')
figure(3)
plot(t,speed,'k-')

%-------------------------------------------------------------
function dx_dt = Boat_accel_new(t, x, mass, kappa, H)
% Exercise 8.17. Power boat
% Evaluate right side of state-space differential equations
% q = [X_A Y_A theta]', x' = [q' dq_dt']

theta = x(3);
a = [sin(theta) -cos(theta) 0];
a_dot = [(x(6) * cos(x(3))) (x(6) * sin(x(3))) 0];

m_mat(1,1) = mass;
m_mat(2,2) = mass;
m_mat(3,3) = mass * kappa^2;

lhs = [[m_mat, -a'];[-a, 0]];

% Evaluate speed of center of mass and drag force


v_G= sqrt(x(4)^2 + x(5)^2);
if (v_G <= 8)
f_d = 8000*(v_G/5);
else
f_d = 8000;
end

% Evaluate thrust force:


if (t <= 30)
beta = 1 * pi/180;
else
Chapter 8, Page 31

beta = -4 * pi/180;
end

u = x(4) * cos(x(3) + beta) + x(5) * sin(x(3) + beta)...


- H * x(6) * sin(beta);
if u <=10
F = 16000;
else
F = 160000/u;
end

% Evaluate basic matrices


rhs = [(F * cos(theta + beta) - f_d * cos(theta)); ...
(F * sin(theta + beta) - f_d * sin(theta)); ...
- F * H * sin(beta); ...
a_dot * x(4:6)];
% [t cond(lhs)];
sol = lhs\rhs;
dx_dt = [x(4:6); sol(1:3)];

Final position: t = 60 X_G = -111.1829, Y_G-69.9575, theta =0.9633

150

XG
100
YG

50
C oordinate (meters )

50

100

150

200
0 10 20 30 40 50 60
T ime (s econds )
Chapter 8, Page 32

80

60

40

20
Y G (meters )

0
S tart
20

40

60

80

100
200 150 100 50 0 50 100 150
X G (meters )

20

18

16

14

12
vG (m/s )

10

0
0 10 20 30 40 50 60
T ime (s econds )
Chapter 8, Page 33
Chapter 8, Page 34

% Exercise 8.18
% Main script file
clear all
g_R = 1.6^2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part (a) Unconstrained generalized coordinates
% Set up initial conditions.
theta_0 = pi/2; x_0 = [theta_0 0]';
t_max = 16;
dt = 0.01;
i_max = ceil(t_max/dt);

% Set tolerance for ODE45 routine


options = odeset('RelTol', 1.0e-8,'AbsTol',1.0e-8);

accel = @(t, x) acc_8_18_un(t, x, g_R);


%----------------------------------------------------------------
% Integration loop
for i = 1:i_max
% Set start and end times for calling o.d.e. solver
t0 = (i - 1) * dt;
tf = i * dt;
% Save response variables
t_val(i,1) = t0;
x_val_un(i,1:2) = x_0';
if x_val_un(i,1) <= 0
break, break
end
Chapter 8, Page 35

% Call Runge-Kutta ODE solver, then plot result


[t,x] = ode45(accel, [t0 tf], x_0, options);

% Save current t and {q}


n_steps = size(x, 1);
x_0 = x(n_steps,1:2)';

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part (b) Constrained generalized coordinates
% Set up initial conditions.
x_0 = [theta_0 0 0 0]';

% Set tolerance for ODE45 routine


options = odeset('RelTol', 1.0e-8,'AbsTol',1.0e-8);

accel_aug = @(t, x) acc_8_18_con(t, x, g_R);


%------------------------------------------------------------
% Integration loop
for i = 1:i_max

% Set start and end times for calling o.d.e. solver


t0 = (i - 1) * dt;
tf = i * dt;

% Save response variables


t_val_con(i,1) = t0;
x_val_con(i,1:4) = x_0';
if x_val_con(i,1) <= 0
break, break
end

% Call Runge-Kutta ODE solver, then plot result


[t,x] = ode45(accel_aug, [t0 tf], x_0, options);

% Save current t and {q}


n_steps = size(x, 1);
x_0 = x(n_steps,1:4)';

end
figure(1)
plot(t_val, x_val_un(:,1)*180/pi, '--r', t_val_con, x_val_con(:,1)*180/pi,
'b:')
xlabel('t (sec))')
ylabel('\theta (deg)')

legend('Constrained formulation', 'Unconstrained formulation')

%------------------------------------------------------------------
function rhs = acc_8_18_un(t, x, g_R)
% Evaluate d^2/dt^2(theta) for Problem 8.18 when theta is the unconstrained
% generalized coordinate
F = 16/3 + 2 * cos(x(1))^2/sin(x(1))^4 - 4 * cos(x(1))/sin(x(1));
dF = - 4 * cos(x(1))/sin(x(1))^3 - 8 * cos(x(1))^3/sin(x(1))^5 +
4/sin(x(1))^2;
d2_theta = -(0.5 * dF * x(2)^2 + 2 * g_R * (sin(x(1)) -
Chapter 8, Page 36

cos(x(1))/sin(x(1))^2))/F;
rhs = [x(2) d2_theta]';

%------------------------------------------------------------------
function dx = acc_8_18_con(t, x, g_R)
% Evaluate d^2/dt^2(q) for Problem 8.18 when q = [theta y] are
unconstrained
% generalized coordinates
a = [((x(2) + 1) * cos(x(1))) sin(x(1))];
R = [[16/3 (2 * sin(x(1))) -a(1,1)]; [(2 * sin(x(1))) 2 -a(1,2)]; [-a
0]];
S = [(-2 * g_R * sin(x(1))); (-2 * x(3)^2 * cos(x(1)) - 2 * g_R); ...
(2 * x(3) * x(4) * cos(x(1)) -(x(2) + 1) * x(3)^2 * sin(x(1)))];
sol = R\S;
dx = [x(3:4); sol(1:2)];

Constrained formulation
Unconstrained formulation
90

80

70
θ (deg)

60

50

40

30
0 2 4 6 8 10 12 14 16
t (sec))
Chapter 8, Page 37
Chapter 8, Page 38

% Exercise 8.19 Tundra pulls a sled


% Main script file
clear all
mass = 500; kappa = 0.6; H = 1.5; L = 10; Cable_F = 300;

% Set up initial conditions.


q_0 = [-(H+L) 0 0]'; v_0 = [0 0 0]'; z_0 = [q_0; v_0];

t_max = 10;
dt = 0.01;
i_max = ceil(t_max/dt);

% Set tolerance for ODE45 routine


options = odeset('RelTol', 1.0e-8,'AbsTol',1.0e-8);

accel = @(t, z) sled_accel(t, z, mass, kappa, H, L, Cable_F);


%---------------------------------------------------------------------------
--------------
% Integration loop
for i = 1:i_max

% Set start and end times for calling o.d.e. solver


t0 = (i - 1) * dt;
tf = i * dt;

% Save response variables


t_val(i,1) = t0;
z_val(i,1:6) = z_0';
X_G = z_val(i,1); Y_G = z_val(i,2); theta = z_val(i,3);
beta = - theta + acos(-X_G/L - (H/L) * cos(theta));
tundra_coords(i, 1) = X_G + H * cos(theta) + L * cos(beta + theta);
tundra_coords(i, 2) = Y_G + H * sin(theta) + L * sin(beta + theta);

% disp([t0 z_val(i, 1:3)])

% Call Runge-Kutta ODE solver, then plot result


[t,z] = ode45(accel, [t0 tf], z_0, options);

% Save current t and {q}


n_steps = size(z, 1);
z_0 = z(n_steps,1:6)';

end
% Plot paths of center of mass and of Tundra
figure(1)
plot(tundra_coords(:,1), tundra_coords(:,2), z_val(:,1),z_val(:,2))
xlabel('X (meters)')
ylabel('Y (meters)')
legend('Tundra''s path', 'Path of center of the sled')
figure(2)
plot(t_val(:,1), z_val(:,1), t_val(:,1), z_val(:,2))
xlabel('Time (seconds)')
ylabel('Coordinate (meters)')
legend('x_G)', 'y_G')
figure(3)
plot(t_val(:,1), z_val(:,4), t_val(:,1), z_val(:,5))
xlabel('Time (seconds)')
ylabel('Speed (m/s)')
legend('dx_G/dt', 'dy_G/dt)')
Chapter 8, Page 39

figure(4)
[AX,H1,H2] = plotyy(t_val(:,1), z_val(:,3)*180/pi, t_val(:,1), z_val(:,6));
set(get(AX(1),'Ylabel'),'String','Degrees')
set(get(AX(2),'Ylabel'),'String','Rad/s')
set(H1,'LineStyle','-')
set(H2,'LineStyle','--')
xlabel('Time (Seconds)')
% Find t for theta > 89 deg
j = 1;
while z_val(j,3) * 180/pi < 89
j = j + 1;
end
disp(['Theta at 89 deg when t = ', num2str(t_val(j))])

%-------------------------------------------------------------
function rhs = sled_accel(t, z, mass, kappa, H, L, Cable_F)
% Exercise 8.19 - Tundra pulls a sled
% Evaluate right side of state-space differential equations
% Implementation of augmentation method
% {z} is current state-space vector:
% number of generalized coordinates is 3, so size of {z} is 6

X_G = z(1); Y_G = z(2);


theta = z(3); theta_dot = z(6);

beta = - theta + acos(-X_G/L - (H/L) * cos(theta));

% Evaluate basic matrices


a = [sin(theta) -cos(theta) 0];
a_dot = [theta_dot*cos(theta) theta_dot*sin(theta) 0];

m_matrix = diag([mass mass mass*kappa^2]);

f = Cable_F * [cos(beta + theta) sin(beta + theta) H * sin(beta)].';

% Assemble system matrices


lhs = [[m_matrix, -a'];[-a, 0]];
rhs = [f; a_dot*z(4:6)];
sol = lhs\rhs;
rhs = [z(4:6); sol(1:3)];
Chapter 8, Page 40

35

30
Tundra's path
Path of center of the sled
25
Y (meters)

20

15

10

0
-25 -20 -15 -10 -5 0 5 10 15
X (meters)

30

25

20 xG)
yG
15
Coordinate (meters)

10

-5

-10

-15
0 1 2 3 4 5 6 7 8 9 10
Time (seconds)
Chapter 8, Page 41

5
dxG/dt
4
dyG/dt)
Speed (m/s)

3
2
1
0
-1
-2
0 2 4 6 8 10
Time (seconds)

150 1

100 dq/dt
0.5
Degrees

50 0 Rad/s

0 -0.5
0 1 2 3 4 5 6 7 8 9 10
Time (Seconds)
Chapter 8, Page 42
Chapter 8, Page 43

% Exercise 8.20
% Main script file
clear all
F_0 = 50; omega = 4; m = 0.8; R = 0.2; kappa = 0.15; g = 9.807;

% Constrained generalized coordinates


% Set up initial conditions.
phi_0 = pi/9; theta_0 = asin(2.5 * sin(phi_0));
z_0 = [theta_0 phi_0 0 0]';

% Set tolerance for ODE45 routine


options = odeset('RelTol', 1.0e-8,'AbsTol',1.0e-8);

accel_aug = @(t, z) acc_8_20(t, z, F_0, omega, m, R, kappa, g);

t_max = 5; dt = 0.01; i_max = ceil(t_max/dt);


t_val = zeros(i_max, 1); z_val = zeros(i_max, 4);
%------------------------------------------------------------
% Integration loop
for i = 1:i_max

% Set start and end times for calling o.d.e. solver


t0 = (i - 1) * dt;
tf = i * dt;

% dz = acc_8_20(t0, z_0, F_0, omega, m, R, kappa, g);


% [f_q f_v f_dv] = constraint_8_20(z_0, dz);
% disp(' ')
% disp([t0 z_0(1:2)'])
% disp([t0 f_q f_v f_dv])

% Save response variables


t_val(i,1) = t0;
z_val(i,:) = z_0';

% Call Runge-Kutta ODE solver, then plot result


[t,z] = ode45(accel_aug, [t0 tf], z_0, options);

% Save current t and {q}


n_steps = size(z, 1);
z_0 = z(n_steps,1:4)';

end

f_config = sin(z_val(:,1)) - 2.5 * sin(z_val(:,2));


Chapter 8, Page 44
figure(1)
subplot(2,1,1)
plotyy(t_val, z_val(:,1)*180/pi, t_val, z_val(:,2)*180/pi)
title(['Max configuration constraint error = ', ...
num2str(max(abs(f_config)))])

v_center = R * z_val(:,3);
subplot (2,1,2)
plot(t_val, v_center)
xlabel('t (sec))')
ylabel('Velocity (m/s)')
title('v_c_e_n_t_e_r = R d\theta\\dt')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function dz = acc_8_20(t, z, F_0, omega, m, R, kappa, g)
% Evaluate dz/dt for Problem 8.20 where
% z = [theta phi d_theta d_phi]'
a = [cos(z(1)) (-2.5 * cos(z(2)))];
M(1,1) = 6 + 2* kappa^2 + 4 * sin(z(1));
M(1,2) = 3.75 * (sin(z(2)) - cos(z(1) + z(2))); M(2, 1) = M(1,2);
M(2,2) = 25/3;
Force = F_0 * sin(omega * t);
F(1,1) = (Force/(m * R)) * (1 + sin(z(1))) - 2 * z(3)^2 * cos(z(1)) ...
- 3.75 * z(4)^2 * (cos(z(2)) + sin(z(1) + z(2)));
F(2,1) = 2.5 * (Force/(m * R)) * sin(z(2)) ...
- 3.75 * z(3)^2 * sin(z(1) + z(2)) - 1.25 *(g/R) * cos(z(2));
Lhs = [M -a'; -a 0];
Rhs = [F; (-z(3)^2 * sin(z(1)) + 2.5 * z(4)^2 * sin(z(2)))];
sol = Lhs\Rhs;
dz = [z(3:4); sol(1:2)];

Max configuration cons traint error = 6.9662e008


10000 50
q (deg)

f (deg)
f
5000 q 0

0 50
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

15
V elocity (m/s )

10 vc e nte r = R dq/dt

0
0 1 2 3 4 5
t (s ec)
Chapter 8, Page 45
Chapter 8, Page 46

% Exercise 8.21 - Nonholonomic linkage


% {q} = [theta1 theta2]'
m = 4; L = 0.5; Gamma = 60;

% Set up initial conditions.


z_0 = [pi/2 pi 0 0]';

% Set tolerance for ODE45 routine


options = odeset('RelTol', 1.0e-8,'AbsTol',1.0e-8);

accel_aug = @(t, z) acc_8_21(t, z, Gamma, m, L);

t_max = 5; dt = 0.01; i_max = ceil(t_max/dt);


t_val = zeros(i_max, 1); z_val = zeros(i_max, 4);
%------------------------------------------------------------
Chapter 8, Page 47

% Integration loop
for i = 1:i_max

% Set start and end times for calling o.d.e. solver


t0 = (i - 1) * dt;
tf = i * dt;

% Save response variables


t_val(i,1) = t0;
z_val(i,:) = z_0';

% Call Runge-Kutta ODE solver, then plot result


[t,z] = ode45(accel_aug, [t0 tf], z_0, options);

% Save current t and {q}


n_steps = size(z, 1);
z_0 = z(n_steps,1:4)';

end

theta1 = z_val(:,1);
theta2 = z_val(:,2);
r_C = L * [(sin(theta1) + sin(theta2)) (cos(theta1) + cos(theta2))];

figure(1)
% subplot(2,1,1)
plot(t_val, theta1 * 180/pi, ':k', t_val, theta2 * 180/pi, '--k')
xlabel('Time (seconds)')
ylabel('Angle (deg)')
legend('\theta_1', '\theta_2')
figure(2)
plot(r_C(:,1), r_C(:,2))
set(gca,'YDir','reverse')
axis equal
title('Path of end C')
xlabel('X_C')
ylabel('Y_C')

%-----------------------------------------------------------
function dz = acc_8_20(t, z, F_0, omega, m, R, kappa, g)
% Evaluate dz/dt for Problem 8.20 where
% z = [theta phi d_theta d_phi]'
a = [cos(z(1)) (-2.5 * cos(z(2)))];
M(1,1) = 6 + 2* kappa^2 + 4 * sin(z(1));
M(1,2) = 3.75 * (sin(z(2)) - cos(z(1) + z(2))); M(2, 1) = M(1,2);
M(2,2) = 25/3;
Force = F_0 * sin(omega * t);
F(1,1) = (Force/(m * R)) * (1 + sin(z(1))) - 2 * z(3)^2 * cos(z(1)) ...
- 3.75 * z(4)^2 * (cos(z(2)) + sin(z(1) + z(2)));
F(2,1) = 2.5 * (Force/(m * R)) * sin(z(2)) ...
- 3.75 * z(3)^2 * sin(z(1) + z(2)) - 1.25 *(g/R) * cos(z(2));
Lhs = [M -a'; -a 0];
Rhs = [F; (-z(3)^2 * sin(z(1)) + 2.5 * z(4)^2 * sin(z(2)))];
sol = Lhs\Rhs;
dz = [z(3:4); sol(1:2)];

% [f_q f_v f_dv] = constraint_8_20(z, dz);


% disp([t f_q f_v f_dv])
Chapter 8, Page 48

200

Angle (deg)
0 q1
q2
200
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
T ime (s econds )

0.4
P ath of end C
0.2
YC

0
0.2
0.5 0 0.5 1
XC
Chapter 8, Page 49

% Exercise 8.22 Linkage with a steerable wheel


% Main script file
clear all
m_AB = 2; m_A = 0.5; L = 0.8; Cable_F = 50;

% Set up initial conditions.


q_0 = [0 0]'; v_0 = [0 0]'; z_0 = [q_0; v_0];

t_max = 2;
dt = 0.005;
i_max = ceil(t_max/dt);

% Set tolerance for ODE45 routine


options = odeset('RelTol', 1.0e-8,'AbsTol',1.0e-8);

% Define anonymous function to hand off to ODE 45


accel = @(t, z) Steerable_linkage(t, z, m_AB, m_A, L, Cable_F);
%--------------------------------------------------------------------
Chapter 8, Page 50

% Integration loop
for i = 1:i_max

% Set start and end times for calling o.d.e. solver


t0 = (i - 1) * dt;
tf = i * dt;

% Save response variables


t_val(i) = t0;
x_A(i) = z_0(1); y_A(i) = 0;
theta(i) = z_0(2);
x_B(i) = x_A(i) + L * cos(theta(i)); y_B(i) = L * sin(theta(i));

% disp([t0 x_A(i) theta(i)])

% Call Runge-Kutta ODE solver, then plot result


[t,z] = ode45(accel, [t0 tf], z_0, options);

% Save current t and {q}


n_steps = size(z, 1);
z_0 = z(n_steps,1:4)';

end
%Plot paths
figure(1)
plot(x_A, y_A, x_B, y_B)
legend('Collar A', 'Wheel B')
figure(2)
plot(t_val, theta * 180/pi, '-r', t_val, x_A, ':b')
legend('theta (deg)', 'x_A')

[max_theta, index] = max(theta);


disp(['Max theta = ', num2str(max_theta * 180/pi), ' when t = ',
num2str(t_val(index))])

% -----------------------------------------------------------------------
function rhs = steerable_linkage(t, z, m_AB, m_A, L, Cable_F)
% Exercise 8.22 - linkage with a steerable wheel
% Evaluate right side of state-space differential equations
% Implementation of augmentation method
% {z} is current state-space vector:
% number of generalized coordinates is 3, so size of {z} is 6

x_A = z(1); theta = z(2); x_A_dot = z(3); theta_dot = z(4);

beta = 0.5 * pi * ( 2* exp(-2 * t) - 1);


beta_dot = -2 * pi * exp(-2 * t);

% Evaluate basic matrices


a = [sin(beta + theta) L * cos(beta)];
a_dot = [theta_dot*cos(theta) theta_dot*L * sin(theta)];
m_matrix = [(m_A + m_AB) (-0.5 * m_AB * L); (-0.5 * m_AB * L) (m_AB *
L^2)/3];
F = [(Cable_F + 0.5 * m_AB * L * theta_dot^2 * cos(theta)) 0].';

% Assemble system matrices


lhs = [[m_matrix, -a'];[-a, 0]];
Chapter 8, Page 51

rhs = [F; a_dot*z(3:4)];


sol = lhs\rhs;
rhs = [z(3:4); sol(1:2)];

0.8

0.6
Y (meters )

0.4 P ath of collar A


P ath of wheel B
0.2

0. 2

0. 4
0 10 20 30 40 50 60 70 80 90
X (meters )

100

q (deg)
x A (meter)
50

50
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
T ime (s ec)
Chapter 8, Page 52
Chapter 8, Page 53
Chapter 8, Page 54

% Exercise 8.23 - Sphere on a turntable


% {q} = [r(nondim) psi theta phi]'
clear all

% Set up initial conditions.


z_0 = [3 0 0 0 0 1 0 0]';

% Set tolerance for ODE45 routine


options = odeset('RelTol', 1.0e-8,'AbsTol',1.0e-8);

accel_aug = @(t, z) acc_8_23(t, z);

t_max = pi; dt = 0.01; i_max = ceil(t_max/dt);


t_val = zeros(i_max, 1); z_val = zeros(i_max, 8);
%------------------------------------------------------------
% Integration loop
for i = 1:i_max

% Set start and end times for calling o.d.e. solver


t0 = (i - 1) * dt;
tf = i * dt;

% Save response variables


t_val(i,1) = t0;
z_val(i,:) = z_0';

% Call Runge-Kutta ODE solver, then plot result


[t,z] = ode45(accel_aug, [t0 tf], z_0, options);

% Save current t and {q}


n_steps = size(z, 1);
z_0 = z(n_steps,1:8)';

end

r = z_val(:,1);
psi = z_val(:,2);
% Absolute coordinates
r_C = [(r .* cos(psi)) (r .* sin(psi))];
% Coordinates relative to xyz attached to the turntable
for n = 1:length(t_val)
angle = t_val(n);
Rot = [[cos(angle) sin(angle)]; [-sin(angle) cos(angle)]];
r_C_rel(n, :) = r_C(n, :) * Rot';
end

figure(1)
subplot(2,1,1)
plot(t_val, r, ':k')
ylabel('Radial distance (nondim)')
subplot(2,1,2)
plot(t_val, psi * 180/pi, '--k')
xlabel('Time (seconds)')
ylabel('Angle (deg)')
figure(2)
plot(r_C(:,1), r_C(:,2), '-k', r_C_rel(:,1), r_C_rel(:,2), ':k')
axis equal
title('Path of the center of the sphere')
xlabel('x_C')
Chapter 8, Page 55

ylabel('y_C')
legend('stationary XYZ', 'xyz attached to the turntable')

%--------------------------------------------------------
function dz = acc_8_23(t, z)
% Evaluate dz/dt for Problem 8.23
x = z(1:4); dx = z(5:8);
a = [[1 0 -1 0]; [0 1 0 -sin(x(3))]];
M = [[1 0 0 0]; [0 (x(1)^2 + 0.4) 0 (0.4 * cos(x(3)))]; ...
[0 0 0.4 0]; [0 (0.4 * cos(x(3))) 0 0.4]];
F(1,1) = x(1) * dx(2)^2;
F(2,1) = -2 * x(1) * dx(1) * dx(2) + 0.4 * dx(3) * dx(4) * sin(x(3));
F(3,1) = -0.4 * dx(3) * dx(4) * sin(x(3));
F(4,1) = 0.4 * dx(2) * dx(3) * sin(x(3));
F(5,1) = 0;
F(6,1) = dx(3) * dx(4) * cos(x(3));
Lhs = [M -a'; -a zeros(2)];
sol = Lhs\F;
dz = [z(5:8); sol(1:4)];

12
Radial distance (nondim)

10

2
0 0.5 1 1.5 2 2.5 3 3.5

100

80
Angle (deg)

60

40

20

0
0 0.5 1 1.5 2 2.5 3 3.5
Time (seconds)
Chapter 8, Page 56

Path of the center of the sphere


10

2
yC

-2
stationary XYZ
xyz attached to the turntable
-4

-6

-8

-10
-10 -5 0 5 10 15
xC
Chapter 8, Page 57
Chapter 8, Page 58
Chapter 8, Page 59
Chapter 8, Page 60
Chapter 8, Page 61
Chapter 8, Page 62
Chapter 8, Page 63
Chapter 8, Page 64

% Exercise 8.29 - Falling bar


% {q} = [x y theta]'
% where x = X_C/L and y = Y_C/L
clear all
mu_vals = [0.1 0];

for n_case = 1:length(mu_vals)


mu = mu_vals(n_case);
t_val = []; z_val = [];
% Set up initial conditions.
z_0 = [0 0.5 75*pi/180 0 0 0]';

% Set tolerance for ODE45 routine


options = odeset('RelTol', 1.0e-8,'AbsTol',1.0e-8);

accel_aug = @(t, z) acc_8_29(t, z, mu);

t_max = pi; dt = 0.001; i_max = ceil(t_max/dt);

%------------------------------------------------------------
% Integration loop
for i = 1:i_max

% Set start and end times for calling o.d.e. solver


t0 = (i - 1) * dt;
tf = i * dt;

% Save response variables


t_val(i,1) = t0;
z_val(i,:) = z_0';

% Call Runge-Kutta ODE solver, then plot result


[t,z] = ode45(accel_aug, [t0 tf], z_0, options);
Chapter 8, Page 65

% Save current t and {q}


n_steps = size(z, 1);
z_0 = z(n_steps,1:6)';
theta = z_0(3);
% disp([t0 theta])
if theta < 0.0001
break, break
end
end
disp(['mu = ', num2str(mu), ', Max t = ', num2str(tf)])
figure(2*n_case - 1)
subplot(2,1,1)
plot(t_val, z_val(:,1), ':k', t_val, z_val(:,2), '--k')
ylabel('Coordinates (nondim)')
legend('X_G', 'Y_G')
title(['\mu = ', num2str(mu)])
subplot(2,1,2)
plot(t_val, z_val(:,3) * 180/pi, '-k')
xlabel('Time (seconds)')
ylabel('Angle (deg)')
figure(2*n_case)
plot(z_val(:,1), z_val(:,2), ':k')
axis equal
title(['Path of the center of the bar, \mu = ', num2str(mu)])
xlabel('x_C')
ylabel('y_C')
end

%----------------------------------------------------------------
function dz = acc_8_29(t, z, mu)
% Evaluate dz/dt for Problem 8.29
x = z(1); y = z(2); theta = z(3); dx = z(4); dy = z(5); dtheta = z(6);
n_slip = 1; % Set a flag to control program flow according to slipping
% Check whether slipping has occurred:
v_c_x = dx + 0.5 * dtheta * sin(theta);
if (abs(v_c_x)) < 10^-8
n_slip = 0;
Lhs = [[1 0 0 0 -1]; ...
[0 1 0 -1 0]; ...
[0 0 1/12 0.5*cos(theta) -0.5*sin(theta)]; ...
[0 -1 0.5*cos(theta) 0 0];
[-1 0 -0.5*sin(theta) 0 0]];
F = [0 -1 0 0.5*dtheta^2*sin(theta) 0.5*dtheta^2*cos(theta)]';
sol = Lhs\F;
N = sol(4); f = sol(5); q_2dot = sol(1:3);
% Check if friction force is too large
if N < 0
disp('Negative normal force')
elseif abs(f)/N > mu % If true, reset flag to indicate slipping
n_slip = 1;
end
end
if n_slip == 1
Lhs_slip = [[1 0 0 mu*sign(v_c_x)]; ...
[0 1 0 -1]; ...
[0 0 1/12 0.5*cos(theta)+mu*sign(v_c_x)*sin(theta)]; ...
[0 -1 0.5*cos(theta) 0]];
F_slip = [0 -1 0 0.5*dtheta^2*sin(theta)]';
Chapter 8, Page 66

sol = Lhs_slip\F_slip;
q_2dot = sol(1:3);
end
dz = [z(4:6); q_2dot];

m=0.1
0.5
Coordinates (nondim)

XG
0.4
YG
0.3

0.2

0.1

0
0 0.5 1 1.5 2

80

60
q (deg)

40

20

0
0 0.5 1 1.5 2
Time (seconds)

Path of the center of the bar, m = 0.1


0.5

0.4

0.3
yC

0.2

0.1

0. 2 0. 1 0 0.1 0.2 0.3


xC
Chapter 8, Page 67
Chapter 8, Page 68

% Exercise 8.30 Bar on a cylinder with friction


clear all

g_R = 1.6^2;
mu = 0.1;

theta_0 = 20 * pi/180;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Constrained generalized coordinates
% Set up initial conditions.
x_0 = [theta_0 (1/sin(theta_0) - 1) 0 0]';

t_max = 16
;
dt = 0.01;
i_max = ceil(t_max/dt);

% Set tolerance for ODE45 routine


options = odeset('RelTol', 1.0e-8,'AbsTol',1.0e-8);

accel_aug = @(t, x) acc_8_30(t, x, g_R, mu);


%------------------------------------------------------------
% Integration loop
for i = 1:i_max

% Set start and end times for calling o.d.e. solver


t0 = (i - 1) * dt;
tf = i * dt;

% Save response variables


t_val(i,1) = t0;
x_val(i,1:4) = x_0';
if x_val(i,1) <= 0
break, break
end

% Call Runge-Kutta ODE solver, then plot result


[t,x] = ode45(accel_aug, [t0 tf], x_0, options);

% Save current t and {q}


n_steps = size(x, 1);
x_0 = x(n_steps,1:4)';

end
figure(1)
subplot(2,1,1)
plot(t_val, x_val(:,1)*180/pi, '-k')
ylabel('\theta (deg)')
subplot(2,1,2)
plot(t_val, x_val(:,3)*180/pi, '-k')
xlabel('t (sec))')
ylabel('d\theta/dt (rad/s)')

%---------------------------------------------------
function dx = acc_8_30(t, x, g_R, mu)
% Evaluate d^2/dt^2(q) for Problem 8.18 when q = [theta y] are
unconstrained
Chapter 8, Page 69

% generalized coordinates
a = [((x(2) + 1) * cos(x(1))) sin(x(1))];
R = [[16/3 (2 * sin(x(1))) -cot(x(1))]; [(2 * sin(x(1))) 2
-(mu*sign(x(3)) + sin(x(1)))]; [-a 0]];
S = [(-2 * g_R * sin(x(1))); (-2 * x(3)^2 * cos(x(1)) - 2 * g_R); ...
(2 * x(3) * x(4) * cos(x(1)) -(x(2) + 1) * x(3)^2 * sin(x(1)))];
sol = R\S;
dx = [x(3:4); sol(1:2)];

120

100

80
θ (deg)

60

40

20
0 2 4 6 8 10 12 14 16

100

50
dθ/dt (rad/s)

-50

-100
0 2 4 6 8 10 12 14 16
t (sec))
Chapter 8, Page 70
Chapter 8, Page 71
Chapter 8, Page 72

% Exercise 8.31 Bar sliding down a wall


% Main script file
clear all
L = 0.6; H = 0.25; phi_min = asin(H/L); phi_0 = 60 * pi/180;
mu = 0.1;

% Set up initial conditions.


q_0 = [phi_0 0 H*cot(phi_0)]'; v_0 = [0 0 0]'; z_0 = [q_0; v_0];

t_max = 2;
dt = 0.0005;
i_max = ceil(t_max/dt);

% Set tolerance for ODE45 routine


options = odeset('RelTol', 1.0e-8,'AbsTol',1.0e-8);

% Define anonymous function to hand off to ODE 45


accel = @(t, z) bar_wall(t, z, L, H, mu);
%---------------------------------------------------------------------------
% Integration loop
for i = 1:i_max

% Set start and end times for calling o.d.e. solver


t0 = (i - 1) * dt;
tf = i * dt;

% Save response variables


t_val(i) = t0;
phi(i) = z_0(1); X_B(i) = z_0(2); Y_B(i) = z_0(3);
% disp(['t=',num2str(t0),' z=', num2str(z_0')])

% Evauate contact forces


[z_dot, NA, NB] = bar_wall_eqs(t0, z_0, L, H, mu);
N_A(i) = NA; N_B(i) = NB;

if (NA < 0) + (NB < 0) + (phi < asin(H/L)) > 0


disp(['t = ', num2str(t0), ', phi = ', num2str(phi(i)), ', N_A =
', num2str(NA), ', N_B = ', num2str(NB)])
break, break
end

% Call Runge-Kutta ODE solver, then plot result


[t,z] = ode45(accel, [t0 tf], z_0, options);

% Save current t and {q}


n_steps = size(z, 1);
z_0 = z(n_steps,:)';

end
figure(1)
plot(t_val, phi * 180/pi, '-r', t_val, N_A, ':b', t_val, N_B, '.g')
legend('phi (deg)', 'N_A/m', 'N_B/m')
xlabel('t (sec)')

%--------------------------------
function rhs = bar_wall(t, z, L, H, mu)
Chapter 8, Page 73

% Evaluate z_dot for Exercise 8.31 solution using a separate function,


% which can be called independently of the ODE solver in order to examine
% the wall forces

[gen_accel, N_A, N_B] = bar_wall_eqs(t, z, L, H, mu);


rhs = [z(4:6); gen_accel];

%----------------------------------
function rhs = bar_wall(t, z, L, H, mu)
% Evaluate z_dot for Exercise 8.31 solution using a separate function,
% which can be called independently of the ODE solver in order to examine
% the wall forces

[gen_accel, N_A, N_B] = bar_wall_eqs(t, z, L, H, mu);


rhs = [z(4:6); gen_accel];

t = 0.237, phi = 0.79487, N_A = 3.5322, N_B = -0.0037631

60

50

40
phi (deg)
NA/m
30 NB/m

20

10

-10
0 0.05 0.1 0.15 0.2 0.25
t (sec)

You might also like