Getting Started

Animations

MATLAB provides two ways of generating moving, animated graphics:

• Erase Mode Method--Continually erase and then redraw the objects on the screen, making incremental changes with each redraw.
• Creating Movies--Save a number of different pictures and then play them back as a movie.

Erase Mode Method

Using the `EraseMode` property is appropriate for long sequences of simple plots where the change from frame to frame is minimal. Here is an example showing simulated Brownian motion. Specify a number of points, such as

• ```n = 20
```

and a temperature or velocity, such as

• ```s = .02
```

The best values for these two parameters depend upon the speed of your particular computer. Generate `n` random points with (x,y) coordinates between -1/2 and +1/2.

• ```x = rand(n,1)-0.5;
y = rand(n,1)-0.5;
```

Plot the points in a square with sides at -1 and +1. Save the handle for the vector of points and set its `EraseMode` to `xor`. This tells the MATLAB graphics system not to redraw the entire plot when the coordinates of one point are changed, but to restore the background color in the vicinity of the point using an "exclusive or" operation.

• ```h = plot(x,y,'.');
axis([-1 1 -1 1])
axis square
grid off
set(h,'EraseMode','xor','MarkerSize',18)
```

Now begin the animation. Here is an infinite `while` loop, which you can eventually exit by typing Ctrl+c. Each time through the loop, add a small amount of normally distributed random noise to the coordinates of the points. Then, instead of creating an entirely new plot, simply change the `XData` and `YData` properties of the original plot.

• ```while 1
drawnow
x = x + s*randn(n,1);
y = y + s*randn(n,1);
set(h,'XData',x,'YData',y)
end
```

How long does it take for one of the points to get outside the square? How long before all the points are outside the square?

 Graphics User Interfaces Creating Movies