function drawmagneticball(tstep, ypos)
%
% DrawMagneticBall animates the response of a magnetic-ball
% suspension system.
%
% DrawMagneticBall(tstep, ypos) displays the
% animated response of a magnetically suspended ball where:
%   tstep is the (scalar) time step taken between displays
%       in seconds, and
%   ypos is the (vector) of ball positions taken from
%       vertical (and down) in meters.
%


ypos = - ypos; % flip positive position to be down

radius  = 0.1; % radius of ball
mangles = -pi:pi/30:pi; % angles for drawing circle representing mass
mx      = radius*cos(mangles); % x-coordinates of perimeter of mass
my      = radius*sin(mangles); % y-coordinates of perimeter of mass

% open figure in which to put magnetic-ball animation
figure('name', 'Magnetic Ball Animation', 'color', 'white'); 

% draw rectangle to represent coil
patch([-0.2, 0.2, 0.2, -0.2], [0.0, 0.0, 0.5, 0.5], 'b');

% set axis size, make axis square, and hold intitial figure
axis([-1, 1, -1.5, 0.5]); axis square; 
hold on;

% draw circle to represent ball
hmass = patch(mx, ypos(1) + my, 'r');

% loop over and display ball for given positions
for it = 2:max(size(ypos)),
    pause(tstep); % wait tstep seconds
    % set new y-coordinates of ball/circle
    set(hmass, 'Ydata', ypos(it) + my);
    drawnow; % redraw shapes with new coordinates
end;
hold off;
