User:Eml4507.s13.team7.wright

Section 12 M-files
Random integer matrix generator function a =randint(m,n,a,b) if nargin <3, a=0; b=9; end a=floor((b-a+1)*rand(m,n))+a;

EDU>> randint(4,5)

ans =

8    6     9     9     4     9     0     9     4     9     1     2     1     8     7     9     5     9     1     9

Shows multiple outputs for a function function [mean,stdev]=stat(x) [m n] = size(x) if m==1 m=n; end mean =sum(x)/m; stdev=sqrt(sum(x.^2)/m-mean.^2);

EDU>> stat(4)

m =    1 n =    1 ans =    4

Storing E into memory EDU>> M=magic(6); E=zeros(6,50); for j=1:50 E(:,j)=eig(M^i); end

This code returns a zero of a function of one variable via the bisection method function [b,steps]=bisectFEA(fun,x,tol) 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); 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 while abs(b-a) >2.0*tol*max(abs(b),1.0) c= a+0.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

Section 13 text strings, error messages, input
EDU>> s='this is a test'

s =

this is a test

EDU>> disp('this message is hereby displayed') error('sorry, the matrix must be symmetric') this message is hereby displayed ??? sorry, the matrix must be symmetric

EDU>> iter=input('enter the number of iterations: ') enter the number of iterations: 3

iter =

3

Section 14: Managing M-files
EDU>> !ed rotate.m 'ed' is not recognized as an internal or external command, operable program or batch file.

Section 15: Comparing efficiency of algorithms: flops, tic and toc
Tic and toc before and after a statement gives EDU>> tic, any statement, toc

ans =

1

Elapsed time is 0.000838 seconds.

Section 16: Output format
No example

Section 17 Hardcopy
EDU>> diary brandon EDU>> ksfh;hr;ghwlrih;gwoi;r ??? Undefined function or variable 'ksfh'. EDU>> yo ??? Undefined function or variable 'yo'. EDU>> y=12;x=45; EDU>> diary off

Below is what appeared in the file named “Brandon”

uiopen('C:\Users\Brandon\Documents\MATLAB\brandon',1) ksfh;hr;ghwlrih;gwoi;r {_??? Undefined function or variable 'ksfh'. }_ yo {_??? Undefined function or variable 'yo'. }_ uiopen('C:\Users\Brandon\Documents\MATLAB\brandon',1) y=12;x=45; uiopen('C:\Users\Brandon\Documents\MATLAB\brandon',1) diary off

Section 18. Graphics
Planar plots

EDU>> x=-1.5:.01:1.5; y=exp(-x.^2); plot(x,y)

3-D line plots EDU>> t=.01:.01:20*pi; x=cos(t); y=sin(t); z=t.^3; plot3(x,y,z)

3-D mesh and surface plots

EDU>> [x,y]=meshgrid(-1:.2:2, -2:.2:2); z=exp(-x.^2-y.^2); mesh(z)



function [x,y,z]=torus(r,n,a) if nargin <3, a=1; end if nargin <2, n=30; end if nargin <1, r=0.5; end theta=pi*(0:2:2*n)/n; phi=2*pi*(0:2:n)'/n; xx=(a+r*cos(phi))*cos(theta); yy=(a+r*cos(phi))*sin(theta); zz=r*sin(phi)*ones(size(theta)); if nargout == 0 surf(xx,yy,zz) ar=(a+r)/sqrt(2); axis([-ar,ar,-ar,ar,-ar,ar]) else x=xx; y=yy ; z=zz; end



Section 19: Sparse Matrix Computations
EDU>> F=floor(10*rand(6)); F=triu(tril(F,1),-1); EDU>> S=sparse(F)

S =

(1,1)       8   (2,1)        9   (1,2)        2   (2,2)        5   (3,2)        9   (2,3)        4   (3,3)        8   (4,3)        1   (3,4)        6   (5,4)        8   (4,5)        3   (5,5)        6   (6,5)        1   (6,6)        8

EDU>> F=full(S)

F =

8    2     0     0     0     0     9     5     4     0     0     0     0     9     8     6     0     0     0     0     1     0     3     0     0     0     0     8     6     0     0     0     0     0     1     8

EDU>> issparse(A) ??? Undefined function or variable 'A'. EDU>> m=6; n=6; e=ones(n,1); d=-2*e; EDU>> T=spdiags([e,d,e],[-10,1],m,n)

T =

(1,2)      -2   (2,3)       -2   (3,4)       -2   (4,5)       -2   (5,6)       -2

EDU>> i=[1 2 3 4 4 4]; j=[1 2 3 1 2 3]; s=[5 6 7 8 9 10]; EDU>> S=sparse(i,j,s,4,3), full(S)

S =

(1,1)       5   (4,1)        8   (2,2)        6   (4,2)        9   (3,3)        7   (4,3)       10

ans =

5    0     0     0     6     0     0     0     7     8     9    10

EDU>> n=6;e=floor(10*rand(n-1,1)); E=sparse(2:n,1:n-1,e,n,n)

E =

(2,1)       6   (3,2)        3   (4,3)        9   (6,5)        4