02.03.2016 Views

MATLAB by rudra pratap

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

6. 7 Animation<br />

225<br />

Example 3: A bar pendulum swings in 2-D<br />

Here is a slightly more complicated example. It involves animation of the motion of<br />

a bar pendulum governed <strong>by</strong> the ODE B +sin B = 0. Now that you are comfortable<br />

with defining graphics objects and using their handles to change their position, etc.,<br />

the added complication of solving a differential equation should not be too hard.<br />

%----- script file to animate a bar pendulum --------------<br />

elf<br />

data = [0 0; -1 .5 0] ;<br />

phi = 0;<br />

R = [cos (phi) -sin (phi) ;<br />

data = R*data;<br />

axis([ - 2 2 -2 2] )<br />

axis('equal ')<br />

% clear figure and stuff<br />

% coordinates of endpoints of the bar<br />

% initial orientation<br />

sin (phi) cos(phi) ];<br />

% rotation matrix<br />

% set axis limits<br />

%-----define the obj ects called bar , hinge , and path .<br />

bar = line('xdata' ,data(1, :), 'ydata' ,data (2 , :), ...<br />

' linewidth' ,3, 'erase' , 'xor ') ;<br />

hinge = line ('xdata' ,O, 'ydata' ,O, 'marker ' , 'o' , 'markersize ' , [10] ) ;<br />

path = line('xdata' ,[] ,'ydat a' ,<br />

[] ,'marker' ,' .','erasemode' , 'none ');<br />

theta = pi -pi/ 1000;<br />

thetadot = 0·<br />

'<br />

t = 0;<br />

dt = .2;<br />

tfinal = 50 ;<br />

% initial angle<br />

% initial angular speed<br />

% initial time<br />

% time step<br />

% final time<br />

%------Euler 's method for numerical integration<br />

while (t

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!