This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Engineering 101 Quote of the Day Freedom is not worth having if it does not include the freedom to make mistakes.
 Mahatma Gandhi 2D/3D Data Representation Plotting Data in MATLAB
To plot data use the function plot(x, y) x is a vector of x values y is a vector of y values x = 1:0.1:10; y = x .^ 2 10 .* x + 15; plot(x, y); Plotting Data in MATLAB
To add a title to a graph use the function title( ) To add x and y axis labels use the functions xlabel( ) xlabel( and ylabel( ) ylabel( To add a grid to the graph use the grid function title(`A Parabola'); xlabel(`x xlabel(`x value'); ylabel(`y ylabel(`y value'); grid on; Plotting Data in MATLAB
You can print your graph by using the menus or by typing print To output your graph to a TIFF file you can use the command print dtiff filename Plotting Data in MATLAB
Multiple data sets can be plotted on the same graph x = 0:0.1:10; y1 = x .^2 10 .* x + 15; y2 =  x.^2 + 10 .* x; plot(x, y1, x, y2); 1 Plotting Data in MATLAB
To distinguish between the two plots you can enter codes to set the line color, style, and marker style. color marker style line style
y = yellow m = magenta c = cyan r = red g = green b = blue w = white k = black . = point o = circle x = xmark + = plus xs = square d = diamond * = star p = pentagram h = hexagram v = triangle down ^ = triangle up > = triangle right < = triangle left  = solid : = dotted . = dashdot dash = dashed Plotting Data in MATLAB
Multiple data sets can be plotted on the same graph Here one will be red and dotdashed, and the other will dotbe blue and marked with circles A legend is created with the legend function x = 0:0.1:10; y1 = x .^2 10 .* x + 15; y2 =  x.^2 + 10 .* x; plot(x, y1, `r_.', x, y2, `bo'); legend(`parabola up', `parabola down'); Plotting Data in MATLAB
In addition to linear scales data can be plotted on logarithmic and semilogarithmic axes. semiplot( ) both x and y are linear semilogx( ) x is logarithmic, y is linear semilogy( ) x is linear, y is logarithmic loglog( ) both x and y are logarithmic Exercise
What will the Mfile do? M1 plot a parabola 3 plot two parabolas 2 plot a circle 4 plot a hyperbola plot(x, y1, x, y2) will print y1 and y2 on the same xaxis x More Plotting in MATLAB
So far we have learned about normal xy plots xYou can actually plot multiple plots on the same axes using an alternative method hold on will prevent the graph from being redrawn and all subsequent plots will be plotted on top of old graphs hold off will turn off this feature Other TwoDimensional Plots Twopolar(theta, r) bar(x, y) barh(x, y) pie(x) A polar plot A vertical bar chart A horizontal bar chart (x = labels, y = values) A pie chart 2 Three Dimensional Plots
Let's say you have some time dependent data For example the location of an object is described by the equations:
x(t) = exp(0.2*t) * cos(2*t) exp(y(t) = exp(0.2*t) * sin(2*t) exp(1 Exercise
Which MATLAB commands will plot the set of points visited by an object following the trajectory:
x(t) = exp(0.2*t) * cos(2*t) exp(y(t) = exp(0.2*t) * sin(2*t) exp(2 We can plot this in 2D easily
3 4 plot2d.m plot3d.m Three Dimensional Plots
t = 0:0.1:10; x = exp(0.2*t) .* cos(2*t); exp(y = exp(0.2*t) .* sin(2*t); exp(plot(x,y); This produces a 2d plot, but it tells us nothing about time. Three Dimensional Plots
t = 0:0.1:10; x = exp(0.2*t) .* cos(2*t); exp(y = exp(0.2*t) .* sin(2*t); exp(plot3(x, y, t); Now we can immediately see the time dependence. Three Dimensional Plots
3D plots are particularly useful when we have data or functions that vary in more than one spatial dimension. For example let's say we wanted to know what the function z = (x2 y2) exp((x2 + y2)) exp(looks like. Set Up a Mesh
Use the built in function:
[x,y]= meshgrid(xstart:xinc:xend, ystart:yinc:yend) Once the grid is set up, then computing our function is simple: z = (x.^2 y.^2) .* exp((x.^2 + y.^2)); exp( 3 more3d.m Three Dimensional Plots
The simplest way to plot is a pseudocolor plot pcolor(x, y, z); You can also plot contours contour(x, y, z); contourf(x, y, z); %filled contour Perspective views can be obtained with mesh(x, y, z); meshc(x, y, z); % with contour surf(x, y, z); surfc(x, y, z); % with contour surfl(x, y, z); % with lighting Plotting in 3D
[x, y] = meshgrid(4:0.1:4, 4:0.1:4); meshgrid(z = (x.^2y.^2).*exp((x.^2+y.^2)); (x.^2y.^2).*exp(pcolor(x,y,z); or contour(x,y,z); mesh(x,y,z); surf(x, y, z); Options with 3D Plots
In shaded plots (e.g. pcolor, surf) you can choose different shading options: shading faceted shading flat shading interp % can be slow You can also choose different colormaps colormap pink colormap copper See the online help for more options on Figure Window Tools
The tools in the figure window can also be used to manipulate your image. These include a tool to zoom in/out, and to in/out, rotate and change the camera viewpoint. Making Movies in MATLAB
You can use the getframe and movie commands to create and play a movie. We will make a motion picture feature starring the thrilling function: Making Movies in MATLAB
[x, y] = meshgrid(4:0.1:4,4:0.1:4); meshgrid(4:0.1:4,f1 = exp((x.^2 + y.^2)); exp(f2 = x.^2 y.^2; f3 = 2.*x.*y; cos 2 (t / 2) x 2  y 2 cos(t ) + 2 xy sin (t ) exp  x 2 + y 2 {( ) } [( )] 4 Making Movies in MATLAB
[x, y] = meshgrid(4:0.1:4,4:0.1:4); meshgrid(4:0.1:4,f1 = exp((x.^2 + y.^2)); exp(f2 = x.^2 y.^2; f3 = 2.*x.*y; Making Movies in MATLAB
[x, y] = meshgrid(4:0.1:4,4:0.1:4); meshgrid(4:0.1:4,f1 = exp((x.^2 + y.^2)); exp(f2 = x.^2 y.^2; f3 = 2.*x.*y; colormap copper; for t = 0:pi/20:4*pi z = (cos(0.5*t)^2).*(f2.*cos(t)+f3.*sin(t)).*f1; surfl(x,y,z); axis([axis([4 4 4 4 0.5 0.5]); % axes always same shading interp; Rotate Twice for t = 0:pi/20:4*pi z = (cos(0.5*t)^2).*(f2.*cos(t)+f3.*sin(t)).*f1; end end surf_movie.m Making Movies in MATLAB [x, y] = meshgrid(4:0.1:4,4:0.1:4); meshgrid(4:0.1:4,f1 = exp((x.^2 + y.^2)); exp(f2 = x.^2 y.^2; f3 = 2.*x.*y; j=1; colormap copper; for t = 0:pi/20:4*pi z = (cos(0.5*t)^2).*(f2.*cos(t)+f3.*sin(t)).*f1; surfl(x,y,z); axis([axis([4 4 4 4 0.5 0.5]); % axes always same shading interp; mov( j ) = getframe; j = j+1; end movie(mov); %play the movie Next Lecture
MATLAB Applications (Mandelbrot Set) 5 ...
View
Full
Document
This note was uploaded on 05/04/2010 for the course ENGIN 101 taught by Professor Jeffringenberg during the Spring '07 term at University of Michigan.
 Spring '07
 JeffRingenberg

Click to edit the document details