14-440-127+Lecture+07

14-440-127+Lecture+07 - 14:440:127 Introduction to...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 14:440:127 Introduction to Computers for Engineers Notes for Lecture 07 Rutgers University, Spring 2010 Instructor- Blase E. Ur 1 Animation Taking the power of loops together with Matlabs plot function, we can create animations. The idea behind animated graphs is that well graph something, pause, graph something slightly different, pause, graph something slightly dif- ferent, and so on. In essence, youll write a loop that plots slightly different things in each iteration. There are two very useful functions youll use when creating animations. One is pause( ) , which pauses for a specified number of seconds i.e. pause(0.5 ) stops the program for 0.5 seconds before continuing. The other useful function is axis([xmin xmax ymin ymax]) , which specifies a particular axis for a graph/plot. This allows you to slowly add more data to your plot, but have a consistent axis. Using the axis command, which should almost always directly follow plot , the axes wont change, making it seem like only the graph is moving. One example of animation is to graph a single point over time. For t = 0 through 20, lets graph x ( t ) = t * cos ( t ) and y ( t ) = 5 * sin ( t ). To do this, well create a for loop for t running from 0 to 20, moving up in small increments so that it looks like the point is moving continuously. Well display it as an X rather than as a single point so that we can actually see it. Well define our axes based on the theoretical minima and maxima of the function within our specified domain. Heres one way of animating this function: % X floating through space for t=0:0.1:20 % choose a small interval x=t*cos(t); y=5*sin(t); pause(0.05) % can come anywhere in the loop plot(x,y,X) axis([-20 20 -5 5]) % must follow plot end It might be easier to precompute all of the points well display and store them as vectors, and then just loop through these vectors. Lets rewrite the above loop using this new method. Note that we can also improve upon our method of determining the axes since weve precomputed all points that will be displayed: % X floating through space t = 0:0.1:20; x=t.*cos(t); % dont forget dot operations y=5*sin(t); for j = 1:length(x) pause(0.05) % can come anywhere in the loop plot(x(j),y(j),X) axis([min(x) max(x) min(y) max(y)]) end Another use of animation is to show motion over time by leaving a trail of where the X has been. Lets re- tain our method of precomputing all of the points well want to show. Now, rather than just looping through and displaying a single point at a time, well keep displaying more points each time we go through the loop. Note that well use 1:j to signify the first j points in the vector. 1 % X floating through space, leaving a trail t = 0:0.1:20; x=t.*cos(t); % dont forget dot operations y=5*sin(t); for j = 1:length(x) pause(0.05) % can come anywhere in the loop plot(x(1:j),y(1:j),x(j),y(j),X) axis([min(x) max(x) min(y) max(y)]) end...
View Full Document

This note was uploaded on 03/20/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Spring '08 term at Rutgers.

Page1 / 10

14-440-127+Lecture+07 - 14:440:127 Introduction to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online