< M A T L A B (R) > Copyright 1984-2013 The MathWorks, Inc. R2013a (8.1.0.604) 64-bit (glnxa64) February 15, 2013 To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com. >> diary >> date ans = 01-Jul-2014 >> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ARRAYS, MATRICES >> A=[1 2 3; 4, 5, 6] A = 1 2 3 4 5 6 >> [A A] ans = 1 2 3 1 2 3 4 5 6 4 5 6 >> [A; A] ans = 1 2 3 4 5 6 1 2 3 4 5 6 >> B=ones(2,3) B = 1 1 1 1 1 1 >> A+B ans = 2 3 4 5 6 7 >> A+i*B ans = 1.0000 + 1.0000i 2.0000 + 1.0000i 3.0000 + 1.0000i 4.0000 + 1.0000i 5.0000 + 1.0000i 6.0000 + 1.0000i >> ans' % transposition and complex conjugation ans = 1.0000 - 1.0000i 4.0000 - 1.0000i 2.0000 - 1.0000i 5.0000 - 1.0000i 3.0000 - 1.0000i 6.0000 - 1.0000i >> ans.' % transposition only ans = 1.0000 - 1.0000i 2.0000 - 1.0000i 3.0000 - 1.0000i 4.0000 - 1.0000i 5.0000 - 1.0000i 6.0000 - 1.0000i >> C=A*B' C = 6 6 15 15 >> det(C) ans = 0 >> C(1,1)=7 C = 7 6 15 15 >> D=inv(C) D = 1.0000 -0.4000 -1.0000 0.4667 >> C^(-1) ans = 1.0000 -0.4000 -1.0000 0.4667 >> clear >> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SOLVING LINEAR EQUATIONS >> A=[2 3; 3 1] A = 2 3 3 1 >> b=[4; 5] b = 4 5 >> X=A\b X = 1.5714 0.2857 >> A*X ans = 4 5 >> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DISPLAYED PRECISION >> format long >> pi ans = 3.141592653589793 >> format >> pi ans = 3.1416 >> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% EIGENVALUES, ETC. >> eig(A) ans = -1.5414 4.5414 >> help eig EIG Eigenvalues and eigenvectors. E = EIG(X) is a vector containing the eigenvalues of a square matrix X. [V,D] = EIG(X) produces a diagonal matrix D of eigenvalues and a full matrix V whose columns are the corresponding eigenvectors so that X*V = V*D. [V,D] = EIG(X,'nobalance') performs the computation with balancing disabled, which sometimes gives more accurate results for certain problems with unusual scaling. If X is symmetric, EIG(X,'nobalance') is ignored since X is already balanced. E = EIG(A,B) is a vector containing the generalized eigenvalues of square matrices A and B. [V,D] = EIG(A,B) produces a diagonal matrix D of generalized eigenvalues and a full matrix V whose columns are the corresponding eigenvectors so that A*V = B*V*D. EIG(A,B,'chol') is the same as EIG(A,B) for symmetric A and symmetric positive definite B. It computes the generalized eigenvalues of A and B using the Cholesky factorization of B. EIG(A,B,'qz') ignores the symmetry of A and B and uses the QZ algorithm. In general, the two algorithms return the same result, however using the QZ algorithm may be more stable for certain problems. The flag is ignored when A and B are not symmetric. See also condeig, eigs, ordeig. Overloaded functions or methods (ones with the same name in other directories) help sym/eig.m Reference page in Help browser doc eig >> doc eig Overloaded functions or methods (ones with the same name in other directories) doc symbolic/eig >> [VA,DA]=eig(A) VA = 0.6464 -0.7630 -0.7630 -0.6464 DA = -1.5414 0 0 4.5414 >> v1=VA(:,1) v1 = 0.6464 -0.7630 >> A*v1 ans = -0.9963 1.1761 >> DA(1,1)*v1 ans = -0.9963 1.1761 >> format long >> v2=VA(:,2) v2 = -0.763019982472726 -0.646374896130196 >> DA(2,2)*v2 ans = -3.465164653336038 -2.935434843548373 >> A*v2 ans = -3.465164653336039 -2.935434843548373 >> clear >> format >> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PLOTING IN ONE VARIABLE: plot, plot3 >> x=-2:2 x = -2 -1 0 1 2 >> plot(x.^2) >> plot(x.^2,'r*') >> plot(x.^2,'go-') >> >> cos(x) >> ans = -0.4161 0.5403 1.0000 0.5403 -0.4161 >> x .* cos(x) ans = 0.8323 -0.5403 0 0.5403 -0.8323 >> x=-2:.1:2; >> y=x .* cos(x); >> plot(x,y) >> axis equal >> hold on >> xlabel x >> ylabel('y') >> title('graph of y=x*cos(x)') >> %%%%%%%%%%%%%%% gtext adds text to a position one can select with the mouse >> %%%%%%%%%%%%%%% note that some TeX is understood by Matlab >> gtext('\gamma_1=2') >> grid >> plot(x, exp(-x.^2),'g') >> legend('x*cos(x)', 'exp(-x^2)') >> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "save as" PDF, EPS from the figure window >> figure(2) >> plot3(x,x.*cos(x), x.*sin(x)) >> grid >> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% rotate the figure; data coursor; see all >> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% the other choices in the figure window >> xlabel x >> ylabel('y') >> zlabel z >> axis equal >> axis ans = -2.0000 2.0000 -0.8323 0.83229 0 1.8186 >> axis([-2 2 -2 2 -2 2]) >> figure(1) >> grid >> >> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PLOTING IN TWO VARIABLES: surf >> clear >> doc surf >> hold off >> format >> [x,y]=meshgrid(linspace(-1,1), linspace(-2,2)); >> size(x) ans = 100 100 >> size y ans = 1 1 >> size(y) ans = 100 100 >> x(1:5,1:5) ans = -1.0000 -0.9798 -0.9596 -0.9394 -0.9192 -1.0000 -0.9798 -0.9596 -0.9394 -0.9192 -1.0000 -0.9798 -0.9596 -0.9394 -0.9192 -1.0000 -0.9798 -0.9596 -0.9394 -0.9192 -1.0000 -0.9798 -0.9596 -0.9394 -0.9192 >> y(1:5,1:5) ans = -2.0000 -2.0000 -2.0000 -2.0000 -2.0000 -1.9596 -1.9596 -1.9596 -1.9596 -1.9596 -1.9192 -1.9192 -1.9192 -1.9192 -1.9192 -1.8788 -1.8788 -1.8788 -1.8788 -1.8788 -1.8384 -1.8384 -1.8384 -1.8384 -1.8384 >> z=exp(x.^2-y.^2); >> clf >> surf(x,y,z) >> figure(2), surfc(x,y,z) >> figure; subplot(2,1,1) >> contour(x,y,z) >> subplot(2,1,2) >> contour3(x,y,z) >> mesh(x,y,z) >> demo >> % select 3-D visualisation/Klein bottle/run in the command window >> KLEIN BOTTLE % look at the file also etc. End of demo >> >> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SCRIPTS, FUNCTIONS >> clear >> %%%%%%%%%%%%%%% INLINE FUNCTIONS >> f=inline('x.^2-y.^2', 'x', 'y') f = Inline function: f(x,y) = x.^2-y.^2 >> f(2,3) ans = -5 >> f([2 3],[1,2]) ans = 3 5 >> %%%%%%%%%%%%%%% ANONYNOMOUS FUNCTIONS >> F=@(x,y) x.^2-y.^2 F = @(x,y) x.^2-y.^2 >> F(1,2) ans = -3 >> F([2 3],[1 2]) ans = 3 5 >> %%%%%%%%%%%%%%% FUNCTIONS IN M-FILES ======================== ASIDE ======================== A file ff.m was created in a directory where MatLab looks for files (this list always includes the current directory). A few related commands, at least for octave: check which "ff" is invoked with "which ff" check current directory with "pwd", abbreviation of "print working directory" do "lookfor path" for commands related to the load/search path Between the two lines below is the content of the ff.m file. The first comment block after "function" is returned by "help" and searched by "lookfor". ------------ function z=ff(x,y) % ff(x,y)=x^2-y^2 % works on arrays as well z=x.^2-y.^2; ------------ ==================== END ASIDE ==================== >> help ff ff(x,y)=x^2-y^2 works on arrays as well >> ff(1,2) ans = -3 >> ff([2 3], [1 2]) ans = 3 5 >> plot2('ff',-1,1,2, 3) ans = 2 >> plot2('ff',-21 , 21, -5, 5, 5) ans = 5 >> plot2('ff',-21 , 21, -5, 5) fig = 2 ans = 2 >> which peaks /usr/lib/matlab-2006b/toolbox/matlab/demos/peaks.m >> peaks z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2) >> plot2('peaks',-2 , 2, -5, 5) ans = 3 >> type peaks function [xz,y,z] = peaks(arg1,arg2) %PEAKS A sample function of two variables. % PEAKS is a function of two variables, obtained by translating and % scaling Gaussian distributions, which is useful for demonstrating % MESH, SURF, PCOLOR, CONTOUR, etc. % There are several variants of the calling sequence: % % Z = PEAKS; % Z = PEAKS(N); % Z = PEAKS(V); % Z = PEAKS(X,Y); % % PEAKS; % PEAKS(N); % PEAKS(V); % PEAKS(X,Y); % % [X,Y,Z] = PEAKS; % [X,Y,Z] = PEAKS(N); % [X,Y,Z] = PEAKS(V); % % The first variant produces a 49-by-49 matrix. % The second variant produces an N-by-N matrix. % The third variant produces an N-by-N matrix where N = length(V). % The fourth variant evaluates the function at the given X and Y, % which must be the same size. The resulting Z is also that size. % % The next four variants, with no output arguments, do a SURF % plot of the result. % % The last three variants also produce two matrices, X and Y, for % use in commands such as PCOLOR(X,Y,Z) or SURF(X,Y,Z,DEL2(Z)). % % If not given as input, the underlying matrices X and Y are % [X,Y] = MESHGRID(V,V) % where V is a given vector, or V is a vector of length N with % elements equally spaced from -3 to 3. If no input argument is % given, the default N is 49. % CBM, 2-1-92, 8-11-92, 4-30-94. % Copyright 1984-2006 The MathWorks, Inc. % $Revision: 5.10.4.3 $ $Date: 2006/06/27 23:02:56 $ if nargin == 0 dx = 1/8; [x,y] = meshgrid(-3:dx:3); elseif nargin == 1 if length(arg1) == 1 [x,y] = meshgrid(linspace(-3,3,arg1)); else [x,y] = meshgrid(arg1,arg1); end else x = arg1; y = arg2; end z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2); if nargout > 1 xz = x; elseif nargout == 1 xz = z; else % Self demonstration disp(' ') disp('z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... ') disp(' - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... ') disp(' - 1/3*exp(-(x+1).^2 - y.^2) ') disp(' ') surf(x,y,z) axis('tight') xlabel('x'), ylabel('y'), title('Peaks') end >> %%%%%%%%%%%%%%% changed plot2 to not echo the figure number if nargout=0 >> plot2('ff',-3,3, -3, 3,3) >> a=plot2('ff',-3,3, -3, 3,3) a = 3 >> figure(3) >> clf >> xx=peaks; >> size(xx) ans = 49 49 >> length(xx) ans = 49 >> xx(1:5,1:5) ans = 0.0001 0.0001 0.0002 0.0004 0.0007 0.0001 0.0002 0.0004 0.0006 0.0010 0.0002 0.0003 0.0005 0.0009 0.0016 0.0002 0.0004 0.0008 0.0014 0.0023 0.0003 0.0006 0.0011 0.0019 0.0032 >> plot2(@(x,y) x^2-y^3, -1 , 1, -2, 2)