Appendix B
Sample Matlab(TM) Exercises for Mechanical Motion, Actuation and Feedback
Control
The study of the force/motion problem can also be the basis for simple
experiments that can be performed in a dorm room. For example, students can
observe the differences in falling time for a dense object such as a rock and a
much less dense object such as a piece of crumpled paper. Use of the model
with these experiments can give students a way to study model validation in a
simple setting. For example, by dropping the crumpled paper from some specified
height, a parameter fit can be made to the "falling object" model. This fit can
be checked by dropping the object from other heights. Other important issues
come into these types of studies as well -- measurement precision, accuracy,
and reproducibility. The initial development of a mechanical motion model is
described below. The development is carried through the basic velocity-position
control problem. The work as shown is done using the Matlab(TM) software.
The important concept introduced in the formulation of this problem is that
calculus is the language for expressing the model of this physical system and
that the derivative is central in notating the energetic relationships of the
system. This viewpoint complements the initial view of derivatives in calculus
courses which concentrates on rates-of-change in data sets. Numerical analysis
is introduced immediately so that students are not limited in the scope of
problems they can tackle.
Mechanical Motion Model.
A mass m acted on by a force F. Express Newton's law in momentum
format (using `p' for momentum), dp/dt = F and
relate velocity to momentum through the relation, v=p/m.
This format is very useful if varying mass problems will be introduced later.
The more familiar F=ma format could be used also. Position can then be
related to velocity v=dx/dt.
This is all that is needed to begin the numerical work. First program Euler's
method for numerical solution of differential equations explicitly, then use
built-in solvers for better accuracy and stability. Euler's approximation to
this equation is set in algorithmic (pseudo-code) form. Although this is
actually a second-order differential equation (albeit one that is integrable)
neither the development of the equation itself nor the numerical solution
should be threatening to students as they build on high school physics
material. The pseudo-code algorithm is:

F = <some function of time>
v = p/m
p_next = p + delt_t * F
x_next = x + v * delt_t
p = p_next
v = p/m
x = x_next
The Matlab(TM) program to accomplish this is provided on the following page.
Running the program gives the graphs on the right.
Matlab(TM) Program for Mechanical Motion Example
% Motion of a mass, Euler's method
% File: eulersmp.m
m = 1.2; % Never use "even" numbers in testing, particularly never
1.0!
% Values of 1.0 can mask programming errors.
tfinal = 20; % How long to simulate
delt_t = 0.1; % Step size for Euler approximation
%Initial values
p = 0; % System is at rest
x = 0;
v = p/m;
F = 0.6; % Constant for this case
result = [0 x v F]; % Initial data values
for t = 0:delt_t:tfinal
v = p/m;
p_next = p + delt_t * F;
x_next = x + v * delt_t;
p = p_next;
v = p/m;
x = x_next;
result = [result;t x v F]; % Record results
end
% Plot results
subplot(2,1,1); % Divide plotting window
plot(result(:,1),result(:,2),'k');
ylabel('Position');
subplot(2,1,2);
plot(result(:,1),result(:,3),'k');
ylabel('Velocity');
xlabel('Time');

The next step is to use this simulation to experiment with arbitrary
functions of time for the force input. The graph to the right shows the result
for a force that alternates between 0.6 and -0.6 . One interesting questions
here is: Why does the velocity remain positive if the force is, on average,
zero? Even though no explicit differentiation was done in solving this problem,
a graph of this sort provides excellent opportunities to observe the
relationships of acceleration, velocity and position, and how they relate to
the mathematics.
Extension to Feedback Control
Control of the position of a mass can be accomplished with a feedback
controller. Feedback control is a subject that is not normally introduced until
upper division (junior/senior). However, an intuitive introduction can be
presented at this level through the use of numerical solutions.
In our example, a measurement of position can be used to compute the applied
force so as to make the system output move towards the desired position. The
simplest form of this is proportional control, where the force is proportional
to the error, i.e., the difference between the desired position and the
actual position. This takes just one extra line in the Matlab(TM) program,
F = kp * (xdesired - x);
where kp is a constant describing the controller gain. The
result, shown at the right is interesting - it doesn't work to control the
mass!

No amount of fooling with kp helps. This introduces students to an
important principle of feedback control in a very graphic way. While properly
designed feedback can improve system performance or isolate it from external
disturbances, feedback also has the potential to make the system behavior
worse!
Making the control depend on both position and velocity and introducing the
velocity gain control constant kv, takes care of things, as seen in the
graph to the left.
F = kp * (xdesired - x) - kv * v;
Although there is no "why" for this controller structure at this point, it can
be tied to math material that comes later that can show very clearly why adding
the velocity term to the controller makes such an important difference.

Changing the controller gain values (kp and kv) can affect how
well the control works quite dramatically. The response is much faster, but
higher forces are used. Given that actuators are physically limited to how much
force they can produce, a limit on the force value can be added to simulate
that situation more accurately,
F = min(Fmax,max(F,Fmin));
In this case, the response will be a bit slower than in the previous example
because the force is limited to a maximum of 0.6 force units.
Following this progression, the students have now solved a nonlinear
differential equation. The Matlab(TM) program for all of this is now up to only
43 lines (including the plotting). The simulation is now up to a point at which
some interesting engineering questions can be asked. Dimensional problems can
be posed in which disk drive or airbag performance specifications are given,
tracking problems can be examined (in which the desired position changes with
time), behavior of the system when there are external disturbances can be
examined, etc. All of this has been done with the Euler-based solver. It would
probably be best at this point to switch over to a built-in solver to take
advantage of variable step size and more efficient algorithms.
Table of Contents