User:Eml4500.f08.Lulz.Layton.Eric/Homework 1

Part 11: Submatrices and colon notation
This section of the primer discusses more advanced matrix manipulation techniques.

 >> 0.2:0.2:1.2
 * The first example shows the use of non-integral increments and boundaries in matrix declaration:

ans =

0.2000   0.4000    0.6000    0.8000    1.0000    1.2000 >> 5:-1:1

ans =

5    4     3     2     1  >> x = [0.0:0.1:2.0]';
 * The next example employs a table of  values to demonstrate matrix generation from a set of vectors:

>> y = sin(x);

>> [x y]

ans =

0        0

0.1000   0.0998

0.2000   0.1987

0.3000   0.2955

0.4000   0.3894

0.5000   0.4794

0.6000   0.5646

0.7000   0.6442

0.8000   0.7174

0.9000   0.7833

1.0000   0.8415

1.1000   0.8912

1.2000   0.9320

1.3000   0.9636

1.4000   0.9854

1.5000   0.9975

1.6000   0.9996

1.7000   0.9917

1.8000   0.9738

1.9000   0.9463

2.0000   0.9093  >> A = rand(5)
 * The following set of examples display the various uses of the colon operator in MATLAB:

A =

0.8147   0.0975    0.1576    0.1419    0.6557

0.9058   0.2785    0.9706    0.4218    0.0357

0.1270   0.5469    0.9572    0.9157    0.8491

0.9134   0.9575    0.4854    0.7922    0.9340

0.6324   0.9649    0.8003    0.9595    0.6787

>> B = rand(5)

B =

0.7577   0.7060    0.8235    0.4387    0.4898

0.7431   0.0318    0.6948    0.3816    0.4456

0.3922   0.2769    0.3171    0.7655    0.6463

0.6555   0.0462    0.9502    0.7952    0.7094

0.1712   0.0971    0.0344    0.1869    0.7547 >> A(1:4,3)

ans =

0.1576

0.9706

0.9572

0.4854 >> A(:,3)

ans =

0.1576

0.9706

0.9572

0.4854

0.8003 >> A(1:4,:)

ans =

0.8147   0.0975    0.1576    0.1419    0.6557

0.9058   0.2785    0.9706    0.4218    0.0357

0.1270   0.5469    0.9572    0.9157    0.8491

0.9134   0.9575    0.4854    0.7922    0.9340 >> A(:,[2 4])

ans =

0.0975   0.1419

0.2785   0.4218

0.5469   0.9157

0.9575   0.7922

0.9649   0.9595 >> A(:,[2 4 5]) = B(:,1:3)

A =

0.8147   0.7577    0.1576    0.7060    0.8235

0.9058   0.7431    0.9706    0.0318    0.6948

0.1270   0.3922    0.9572    0.2769    0.3171

0.9134   0.6555    0.4854    0.0462    0.9502

0.6324   0.1712    0.8003    0.0971    0.0344 >> A(:,[2,4]) = A(:,[2,4])*[1 2;3 4]

A =

0.8147   0.5232    0.1576    0.7626    0.6557

0.9058   1.5438    0.9706    2.2440    0.0357

0.1270   3.2941    0.9572    4.7567    0.8491

0.9134   3.3341    0.4854    5.0838    0.9340

0.6324   3.8434    0.8003    5.7677    0.6787  >> x = A(:,1)
 * The final set of examples for this section explores reordering of vectors using both colon notation and the  and   commands.

x =

0.8147

0.9058

0.1270

0.9134

0.6324 >> x = x(5:-1:1)

x =

0.6324

0.9134

0.1270

0.9058

0.8147 >> y = fliplr(x)

y =

0.6324

0.9134

0.1270

0.9058

0.8147 > y = flipud(x')

y =

0.6324   0.9134    0.1270    0.9058    0.8147

Part 12: M-files: script files, function files
This section of the primer discusses MATLAB's M-file system for executing command sequences. MATLAB handles two types of M-files: scripts and functions.

 First, the MATLAB code saved as : A = [
 * The first example shows the use of a simple script for creating a matrix:

1234

5678

];

And then the execution of the script to create the matrix:

>> data

>> A

A =

1234

5678

 First, the MATLAB code saved as the function : function a = randint(m,n,a,b)
 * The next example employs a function to generate a n-by-m matrix of random numbers from 0 to 9:

%RANDINT Randomly generated integral matrix.

%   randint(m,n) returns and m-by-n such matrix with entries

%   between 0 and 9.

%   rand(m,n,a,b) return entries between integers a and b.

if nargin < 3, a = 0; b = 9; end

a = floor((b-a+1)*rand(m,n)) + a;

And the calling of the function to generate a 4-by-5 matrix: >> z = randint(4,5)

z =

2    1     5     5     5

6    4     2     6     1

6    9     7     8     1

1    3     2     9     2

 First, the MATLAB code saved as : function [mean, stdev] = stat(x)
 * The next function highlights the capability to have multiple outputs:

% STAT Mean and standard deviation

%   For a vector x, stat(x) returns the mean of x;

%   [mean, stdev] = stat(x) both the mean and standard deviation.

%   For a matrix x, stat(x) acts columnwise.

[m n] = size(x);

if m == 1

m = n; % handle case of a row vector

end

mean = sum(x)/m;

stdev = sqrt(sum(x.^2)/m - mean.^2); Next, two function calls are made to. The first displays the aforementioned multiple outputs: >> [xm, xd] = stat(x)

xm =

0.6786

xd =

0.2938 And the second displays a single output (no square bracket required): >> xm = stat(x)

xm =

0.6786 Lastly, the  command is used to display the header for  : >> help stat

STAT Mean and standard deviation

For a vector x, stat(x) returns the mean of x;

[mean, stdev] = stat(x) both the mean and standard deviation.

For a matrix x, stat(x) acts columnwise. 
 * The last function examples highlight the use of the  command to pass functions to other functions:

First, the MATLAB code for the function, which finds the zero of a passed function via bisection method: function [b, steps] = bisect(fun, x, tol)

%BISECT Zero of a function of one variable via the bisection method

%   bisect(fun,x) returns a zero of the function. fun is a string

%   containing the name of a real-valued MATLAB function of a

%   single real variable; ordinarily functions are defined in

%   M-files. x is a starting guess. The value returned is near

%   a point where fun changes sign. For example,

%   bisect('sin',3) is pi. Note the quotes around sin.

%

%   An optimal third input argument sets a tolerance for the

%   relative accuracy of the result. The default is eps.

%   An optional section output argument gives a matrix containing a

%   trace of the steps; the rots are of form [c f(c)].

% Initialization

if nargin < 3, tol = eps; end

trace = (nargout == 2);

if x ~= 0, dx = x/20; else, dx = 1/20; end

a = x - dx; fa = feval(fun,a);

b = x + dx; fb = feval(fun,b);

% Find change of sign.

while (fa > 0) == (fb > 0)

dx = 2.0*dx;

a = x - dx; fa = feval(fun,a);

if (fa > 0) ~= (fb > 0), break, end

b = x + dx; fb = feval(fun,b);

end

if trace, steps = [a fa; b fb]; end

%Main loop

while abs(b - a) > 2.0*tol*max(abs(b),1.0)

c = a + .5*(b-a); fc = feval(fun,c);

if trace, steps = [steps; [c fc]]; end

if (fb > 0) == (fc > 0)

b = c; fb = fc;

else

a = c; fa = fc;

end

end

And the function call to find the zero of the sin function near x = 2: >> bisect('sin',2)

ans =

3.1416  >> type eig
 * Finally, the primer notes the usefulness of the  command to identify the nature of functions:

'eig' is a built-in function. >> type vander

function A = vander(v)

%VANDER Vandermonde matrix.

%  A = VANDER(V) returns the Vandermonde matrix whose columns

%  are powers of the vector V, that is A(i,j) = v(i)^(n-j).

%

%  Class support for input V:

%     float: double, single

%  Copyright 1984-2004 The MathWorks, Inc.

%  $Revision: 5.9.4.1 $  $Date: 2004/07/05 17:01:29 $

n = length(v);

v = v;

A = ones(n,class(v));

for j = n-1:-1:1

A(:,j) = v.*A(:,j+1);

end >> type rank

function r = rank(A,tol)

%RANK  Matrix rank.

%  RANK(A) provides an estimate of the number of linearly

%  independent rows or columns of a matrix A.

%  RANK(A,tol) is the number of singular values of A

%  that are larger than tol.

%  RANK(A) uses the default tol = max(size(A)) * eps(norm(A)).

%

%  Class support for input A:

%     float: double, single

%  Copyright 1984-2004 The MathWorks, Inc.

%  $Revision: 5.11.4.3 $  $Date: 2004/08/20 19:50:33 $

s = svd(A);

if nargin==1

tol = max(size(A)') * eps(max(s));

end

r = sum(s > tol);

Part 13: Text strings, error messages, input
This section of the primer discusses the various methods of handling text in MATLAB.

 >> s = 'This is a test'
 * The first example shows the necessity of single quotes for defining a simple text string:

s =

This is a test  >> disp('this message is hereby displayed')
 * The next example features use of the  and   commands:

this message is hereby displayed >> error('Sorry, the matrix must be symmetric')

??? Sorry, the matrix must be symmetric Note that when the  command is used, the resulting output is displayed in red.  >> iter = input('Enter the number of iterations:  ')
 * The final example features text-based input:

Enter the number of iterations:  12

iter =

12