Page 37 - Basics of MATLAB and Beyond
P. 37
The (x, y) pairs can be split into two matrices:
1234 1111
1234 2222
x = ; y = .
1234 3333
1234 4444
The matrix x varies along its columns and y varies down its rows. We
define the surface z:
2
2
z = x + y ;
which is the distance of each (x, y) point from the origin (0, 0). To
calculate z in matlab for the x and y matrices given above, we begin
by using the meshgrid function, which generates the required x and y
matrices:
>> [x,y] = meshgrid(1:4)
x =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
y =
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
Now we simply convert our distance equation to matlab notation; z =
2 2
x + y becomes:
>> z = sqrt(x.^2 + y.^2)
z =
1.4142 2.2361 3.1623 4.1231
2.2361 2.8284 3.6056 4.4721
3.1623 3.6056 4.2426 5.0000
4.1231 4.4721 5.0000 5.6569
c 2000 by CRC Press LLC