1 % Scaling counterexample
5 % This basic simulation is designed to show, by counterexample,
6 % why use of nondimensional numbers is necessary for NTRT.
7 % Equivalently, this would show why the commutative property of
8 % multiplication does not apply to scaling of units.
10 clear all; clc; close all;
12 % This disproves the following posulate:
13 % 10 * (length / time ^2) == (10 * length) / time^2
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16 % A spring-mass-damper attached horizontally to a rod, hinged at the
18 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21 p.x_r = 1; % meters; NOTE THIS IS NOT USED FOR THE ANGULAR CASE.
25 p.c = 10; % N*s/m, or kg/sec
26 p.theta_0 = pi/4; % radians
27 p.theta_r = pi/3; % radians. This is
for the rotational spring model.
30 % Forward-integrate (Euler style) for a handful of seconds
34 num_timesteps = size(t,2);
35 y = zeros(2, num_timesteps); % because we have 2 state variables.
37 % initialize. rod starts at angle theta_0, with zero velocity.
38 y(:,1) = [ p.theta_0; 0];
44 y(:, i) = ( rod_hinge_dynamics(theta, theta_dot, p)) * dt + y(:,i-1);
49 %plot(t, y(2,:),
'r');
50 axis([0 t_final 0.5 1.5]);
51 title(
'Original, unscaled simulation');
53 ylabel(
'Radial position of rod, radians');
55 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56 % Now,
for comparison, scale gravity. This should show different results!
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 g_scaled = scaling_factor * p.g;
62 % We can use the same timestepping, but need to collect
new data points:
63 y_new = zeros(2, num_timesteps);
64 y_new(:,1) = [ p.theta_0; 0];
69 theta_dot = y_new(2,i-1);
70 y_new(:, i) = ( rod_hinge_dynamics(theta, theta_dot, p)) * dt + y_new(:,i-1);
76 %plot(t, y_new(2,:),
'r');
77 axis([0 t_final 0.5 1.5]);
78 title(
'With only gravity scaled');
80 ylabel(
'Angular position of rod, radians');
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 % Here
's the fun part: do a third simulation, and scale length,
84 % as we'd normally
do in NTRT. Our length constants are the length of the
85 % rod, and the rest length of the spring.
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 l_new = (1/scaling_factor) * p.l;
89 %l_new = scaling_factor * p.l;
90 %l_new = sqrt(scaling_factor) * p.l;
91 %x_r_new = scaling_factor * p.x_r;
92 %c_new = sqrt(scaling_factor) * p.c;
93 %c_new = (1/scaling_factor) * p.c;
98 %k_new = scaling_factor * p.k;
99 %m_new = (1/sqrt(scaling_factor)) * p.m;
100 %m_new = (1/scaling_factor) * p.m;
105 y_new_length_scaled = zeros(2, num_timesteps);
106 y_new_length_scaled(:,1) = [p.theta_0; 0];
108 for i=2:num_timesteps
110 theta = y_new_length_scaled(1,i-1);
111 theta_dot = y_new_length_scaled(2,i-1);
112 y_new_length_scaled(:, i) = ( rod_hinge_dynamics(theta, theta_dot, p)) * dt + y_new_length_scaled(:,i-1);
118 plot(t, y_new_length_scaled(1,:));
119 %plot(t, y_new_length_scaled(2,:),
'r');
121 %Adjust the limits of the plot to match our
new scaling
122 %t_final_new = floor(t_final * (1/sqrt(scaling_factor)));
123 %axis([0 t_final 0.5 1.5]);
124 title(
'Gravity and length scale changed');
126 ylabel(
'Angular position of rod, radians');
127 axis([0 t_final 0.5 1.5]);
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 % Finally, scale according to our pi terms: both length and angular damping chang along with gravity.
131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134 %c_new = sqrt(scaling_factor) * p.c;
135 c_new = (1/scaling_factor) * p.c;
138 %k_new = scaling_factor * p.k;
139 %m_new = (1/sqrt(scaling_factor)) * p.m;
140 %m_new = (1/scaling_factor) * p.m;
145 y_new_length_spring_scaled = zeros(2, num_timesteps);
146 y_new_length_spring_scaled(:,1) = [p.theta_0; 0];
148 for i=2:num_timesteps
150 theta = y_new_length_spring_scaled(1,i-1);
151 theta_dot = y_new_length_spring_scaled(2,i-1);
152 y_new_length_spring_scaled(:, i) = ( rod_hinge_dynamics(theta, theta_dot, p)) * dt + y_new_length_spring_scaled(:,i-1);
158 plot(t, y_new_length_spring_scaled(1,:));
159 title(
'Gravity, length, and angular damping constant scale changed');
161 ylabel(
'Angular position of rod, radians');
162 axis([0 t_final 0.5 1.5]);
164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165 % But wait! We forgot to
do the time term.
166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 y_new_length_spring_time_scaled = zeros(2, num_timesteps);
171 y_new_length_spring_time_scaled(:,1) = [p.theta_0; 0];
173 for i=2:num_timesteps
175 theta = y_new_length_spring_time_scaled(1,i-1);
176 theta_dot = y_new_length_spring_time_scaled(2,i-1);
177 y_new_length_spring_time_scaled(:, i) = ( rod_hinge_dynamics(theta, theta_dot, p)) * dt + y_new_length_spring_time_scaled(:,i-1);
180 % Adjust time
for the g, c, k angular scaling
case
181 %t_new = t.* (1/scaling_factor);
182 t_new = t.* (scaling_factor);
184 % Adjust the time scale, from our pi term analysis:
185 %t = t * scaling_factor;
189 plot(t_new, y_new_length_spring_time_scaled(1,:));
190 title(
'Gravity, length, angular damping constant, and time scale changed');
192 ylabel(
'Angular position of rod, radians');
193 axis([0 t_final 0.5 1.5]);
void simulate(tgSimulation *simulation)