12.07.2015 Views

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

[x,y] = meshgrid(1:5,1:3)x =1 2 3 4 51 2 3 4 51 2 3 4 5y =1 1 1 1 12 2 2 2 23 3 3 3 3>> clf>> plt(x,y,’o’)>> axis([0.9 5 0.9 3])Such matrices can be used, for example, as variables in functions <strong>of</strong> x<strong>and</strong> y:[x,y] = meshgrid(linspace(0,5),linspace(-10,10));r = sqrt(x.^2 + y.^2);contour(x,y,r)axis equalaxis([-10 10 -10 10])(More detail on the axis comm<strong>and</strong> can be found on page 119.) Themeshgrid function can be used to produce three-dimensional grids,returning three-dimensional arrays that can be used in an analogousmanner. To go to more than three dimensions, you can use the functionndgrid. The following example <strong>of</strong> a three-dimensional volume visualisationis taken from the help entry for ndgrid:[x1,x2,x3] = ndgrid(-2:.2:2, -2:.25:2, -2:.16:2);z = x2 .* exp(-x1.^2 - x2.^2 - x3.^2);slice(x2,x1,x3,z,[-1.2 .8],[], -.2)view(-24,28)Exercise 12 What is the difference between the outputs <strong>of</strong>meshgrid <strong>and</strong> ndgrid when generating grid matrices <strong>of</strong> less thanfour dimensions? Why this difference? (Answer on page 189.)c○ 2000 by CRC Press LLC

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

Saved successfully!

Ooh no, something went wrong!