11.07.2015 Views

MatlabNotes

MatlabNotes

MatlabNotes

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

-0.2220 0 00 -0.2220 0.05550 0.0555 -0.2220This is confirmed since the di↵erences are atround–o↵ error levels (less than 10 15 ). A matrixwith this property is called an orthogonalmatrix.21.3 max & minThese functions act in a similar way to sum. Ifx is a vector, then max(x) returns the largestelement in x>> x = [1.3 -2.4 0 2.3]x =1.3000 -2.4000 0 2.3000>> max(x), max(abs(x))ans =2.3000ans =2.4000>> [m, j] = max(x)m =2.3000j =4Repeating these commands will lead to di↵erentanswers. Example 22.2 gives an illustrationof the use of random numbers.21.5 find for vectorsThe function “find” returns a list of the positions(indices) of the elements of a vector satisfyinga given condition. For example,>> x = -1:.05:1;>> y = sin(3*pi*x).*exp(-x.^2);>> plot(x,y,’:’)>> k = find(y > 0.2)k =Columns 1 through 129 10 11 12 13 22 23 24 25 26 27 36Columns 13 through 1537 38 39>> hold on, plot(x(k),y(k),’o’)>> km = find( x>0.5 & y> plot(x(km),y(km),’-’)When we ask for two outputs, the first gives usthe maximum entry and the second the indexof the maximum element.For a matrix, A, max(A) returns a row vectorcontaining the maximum element from eachcolumn. Thus, to find the largest element in A,we use max(max(A)).21.4 Random NumbersThe function rand(m,n) produces an m⇥n matrixof random numbers, each of which is in therange 0 to 1. rand on its own produces a singlerandom number.>> y = rand, Y = rand(2,3)y =0.9191Y =0.6262 0.1575 0.25200.7446 0.7764 0.612121.6 find for matricesThe find–function operates in much the sameway for matrices:>> A = [ -2 3 4 4; 0 5 -1 6; 6 8 0 1]A =-2 3 4 40 5 -1 632

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

Saved successfully!

Ooh no, something went wrong!