Unformatted text preview:

Quick Summary, Part IGeneral Commandsexit Exit Matlabwhos List all variables and infols List the directorydir List the directoryhelp command Type the help for commandhelpdesk Invoke the browser helplookfor keyword Search help for keyworddiary filename Save all commands in filenameedit Invoke the editorclc Clear command windowFile Input/Outputsave filename Save all as filename.matsave filename var1 Save var1 as filename.matload filename Load filename.matopen filename.fig Load a figureX=load(’file.dat’); Loads text data into XArithmetic and Functions+,-,*,/ Add, SubtractMultiply, DivideUsual Trig Functionsatan2(y,x) 4-quadrant inverse tangentexp(A) eA, elementwiselog(A) ln(A), elementwisemod(x,y) See helprandperm(n) Random permutationof integers 1-nArray Commandssize,lengthA’ ATfor A realA.*B Element multiplyA.^n Element exponentiateA./B Element divideA*B Matrix multiplyA+c Add scalar c to all A.A*c Multiply scalar c to all AA=rand(m,n) Random matrixA=randn(m,n) Random matrixA=zeros(m,n) Zero matrixA=ones(m,n) Matrix of 1’sA=eye(n) n × n identitya:c [a a+1 a+2 ... a+m]with a + m ≤ ca:b:c [a a+b a+2b ... a+mb]with a + mb ≤ clinspace(a,b,N) N points between a a ndb (inclusive)A(i,j) The (i, j)th element of AA(i,:) The entire ith rowA(:,j) The entire jth colum nA(:,2:5) The 2d to fifth columns, all rowsA(1:4,2:3) A 4 × 2 submatrixA(1,:)=[]; Delete the first row.A([1,3],:)=[]; Delete rows 1 and 3.A(:,3)=[]; Delete column 3.A(:,1:2:5)=[]; Delete the odd columns.Plot Example:x=linspace(-2,2);y1=sin(x);y2=x.^2;plot(x,y1,’g*-’,x,y2,’k-.’);title(’Example One’);legend(’The Sine Function’,’A Quadratic’);xlabel(’Dollars’);ylabel(’Sense’);Code Colory yellowm magentac cyanr redg greenb bluew whitek blackSymbol Creates. pointo (”oh”, not zero) circlex x-mark+ plus− solid∗ star: dotted−. dashdot−− dashedAlso see: plotyy semilogx semilogyCalculus and EquationsSee the help files.Equation Solver example:[x,y]=solve(’x^2+x*y+y=3’,’x^2-4*x+3=0’)Approx Derivative: diffSymbolic Derivative examp:x = sym(’x’); t = sym(’t’);diff(sin(x^2)) is 2*cos(x^2)*xdiff(t^6,6) is 720Approx Integral: quadSymbolic Integral: intx=sym(’x’); int(1/(1+x^2))= tan−1(x)Diff. Eqns: dsolveDiff. Eqns: ode23 or ode45Matrix Exponential: expm(A)Taylor Series: taylorLinear Algebra BasicsX=A\B Solution to AX=B(Exact or Least-squares)norm(x) kxk[U,S,V]=svd(A) The Singular ValueDecomposi tion of AR=rref(A) Produce the RREF of AQ=orth(A) Basis for Col(A)(columnwise, ortho norm al)N=null(A) Basis for Null(A)(columnwise, ortho norm al)[Q,R]=qr(A) QR factorizationA = QR, with Q orthog andR upper triangularBasic Statisticsmean(x) mean of vector xmean(X) mean taken columnwisestd(x) standard deviation of xstd(X) stand dev columnwisehist(a,n) Histogram of values invector x using n bins.hist(A,n) Histogram (columnwise)of matrix A using n binserrorbar(m,s) Error bar plot,m = meanss = standard deviations1Bits of useful codeChoose action k with prob p(k)P=cumsum([0,p]); %if p is a rowx=rand;n=histc(x,P);k=find(n==1);Sort and applications[vals,idx]=sort(b) Sorted (lo-hi) valuesin vals Indices in idxb(idx(1)) Smallest value of bb(idx(end)) Biggest value of bb(idx) Same as valsidx1=idx(end:-1:1) Reverse the indexMaxs/Minsa=max(b) Max of ba=max(B) Max of each columna=max(max(B)) Max of BSame for minUse of repmatA=repmat(x,a,b) replicates x a times down and b timesacross.1. Example: Mean subtract a matrix X (vectors arecolumns)[m,n]=size(X);x=mean(X’);X=X-repmat(x’,1,n);2. Example: Normalize a matrix X:[m,n]=size(X);d=sqrt(sum(X.*X));Xnorm=X./repmat(d,m,1);(Note the second line- it computes a vector whosevalues are the norms of each column of X).3. Example: Find the column in X closest to a vector a(assume X is m × n and a is a column)A=X-repmat(a,1,n);d=sqrt(sum(A.*A));[vals,idx]=sort(d);Then X(:,idx(1)) is the column closest to a.Sight and Soundimage(X) Display matrix X as an imageimagesc(X) Same, but scale the colorsAlso see colormap and colorbarMovie Example:for k=1:10plot(x,y);M(:,k)=getframe;endmovie(M,3) Play movie M 3 timesAlso see slideshowSounds: See help file for infowavreadsoundsoundscDebuggingEither use the tools with the editor, or usedbstop if errorTo turn thi s off: dbclear if


View Full Document

Whitman MATH 350 - Lecture Notes

Documents in this Course
Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?