function ballbeam(theta,ballx,tstep) % % Function name: BALLBEAM % Function m-file: BALLBEAM.m % Function Description: BALLBEAM(THETA,BALLX,TSTEP) displays the % animated response of a ball and beam apparatus where THETA is % the angle of the beam in radians, BALLX is the distance of the % ball from the center of the beam in meters, and TSTEP is the % amount of time to wait between consecutive BALLX and THETA values. % The beam is assumed to be 80 centimeters long and the minimum % value of TSTEP is 0.01 seconds as described in pause function % help. % Written by K. Wedeward Fall 96 % Modified by J. Watkins Fall 96 % ballx=ballx*100; % convert from m to cm beam_length = 80; % cm bl2 = beam_length/2; radius = 2.0; % ball radius arcstep = 36; % angle increment (in degrees) % to sweep out arc lengths when % creating sides of ball ltheta = length(theta); % find length of theta vector bally = - ballx .* tan(theta) + radius; % find y positions of ball j = 0:arcstep:(360-arcstep); % create ball sides using arclengths arcx = radius * cos((j+arcstep) * pi/180); % x coordinates of ball sides arcy = radius * sin((j+arcstep) * pi/180); % y coordinates of ball sides beamx1 = -bl2 * cos(theta); % compute coordinates of beam beamx2 = bl2 * cos(theta); % ends: left end => (beamx1,beamy1) beamy1 = bl2 * sin(theta); % right end => (beamx2,beamy2) beamy2 = -bl2 * sin(theta); % construct initial beam using a wide line % LineWidth of 0.5 in normal L = plot([beamx1(1) beamx2(1)], [beamy1(1) beamy2(1)], 'y', 'EraseMode', 'xor','LineWidth', [2.5]); axis([-45 45 -45 45]); % constuct square axes patch([-2 0 2 -2], [-5 0 -5 -5], 'y'); % construct beam stand % construct initial ball H = patch(arcx+ballx(1), arcy+bally(1), 'b', 'EraseMode', 'xor'); 'Press a key to begin ball and beam animation' pause; for i = 2:ltheta, % loop over data values set(L, 'XData', [beamx1(i) beamx2(i)]); % change x coordinates of beam ends set(L, 'YData', [beamy1(i) beamy2(i)]); % change y coordinates of beam ends set(H, 'XData', arcx+ballx(i)); % change x coordinate of ball set(H, 'YData', arcy+bally(i)); % change y coordinate of ball drawnow; % pause(tstep); % wait tstep seconds end;