6 August 2006. July 5, 2016 Eric Rasmusen, erasmuse@indiana.edu This file is now at http://www.rasmusen.org/a/matlab-rasmusen.txt These notes are latest Matlab tips and tricks that I have found useful or thought might be useful. I wrote these for my own use and have not tried to make them clear for others, but some other people will find them useful. ********************************************************** COMMENTS %Here is a comment %{ Here is how to do a multiline one %} PLOTTING SYMBOLIC EQUATION Do NOT use fplot--- I couldn't get it to work. Use syms x; f = x^3 - 6*x^2 + 11*x - 6; ezplot(f,[-1 10]) ax =gca; ax.XTick = [0,1,2,3,4,5]; % This put ticks at 0,1,2, etc., and makes the plot go from x=-1 to %x=10 MULTIPLE CURVES IN ONE PLOT: hold on; ezplot(z1) ezplot(z2,[0,15 ]) hold off; WORKING DIRECTORY cd ('J:\_G406_Regulation_Office\problem-sets') pwd %to check present working directory ********************************************************** MINIMIZATION To minimize function f1 by choice of x1 with a maximum of 10 iterations, use these commands: options = optimset('MaxIter', 10,'Display', 'iter') x1=100 [x1,fval,exitflag output] = fminunc('f1',x1, options) You need to set up a function file f1.m to define f1 first. x1 is the starting value, which you need to set. You need to include the options fval and exitflag AS WELL AS output, even if you only want output, which tells you number of iterations, algorithm, etc.. The Display=iter option shows you what is going on at each iteration. ********************************************************** DIVIDERS FOR MAKING PROGRAMS EASIER TO READ Use this divider-- a string of stars, with a blank line above and below: disp(' ') disp('********************************************************* ') disp(' ') ********************************************************** MATRIX DIMENSIONS To find the dimensions of matrix A, type size(A) OR ddisp(['Object A dimensions: ' num2str(size(A)) ]) ********************************************************** RUNNING BATCH: Create a text file named rasprog.m. Then just type in it. Then just issue rasprog as a command in matlab, if you in the right diretory. Note: Once you have issued rasprog and you are editing or debugging it, you need to use the matlab editor, not Textedit or other editor of your own. rasprog For logging, use delete mydiary.txt diary mydiary.txt To turn it off , diary off Note that it appends to mydiary.txt if that file already exists. To overwrite, preface it with: You may just want to save one or more matrices. To save the value of the variable "x" to a plain text file named "x.value" use save x.value x -ascii To save all variables in a file named "mysession.mat" in reloadable format, use save mysession To restore the session, use load mysession ********************************************************** SUPPRESSING VIEWING OF OUTPUT A semicolon at the end of the line suppresses viewing output. ********************************************************** ********************************************************** ______________________________________________________ K = kron(X,Y) returns the Kronecker tensor product of X and Y. The result is a large array formed by taking all possible products between the elements of X and those of Y. If X is m-by-n and Y is p-by-q, then kron(X,Y) is m*p-by-n*q. Examples If X is 2-by-3, then kron(X,Y) is * [ X(1,1)*Y X(1,2)*Y X(1,3)*Y X(2,1)*Y X(2,2)*Y X(2,3)*Y ] ______________________________________________________ Y = ones(m,n) or Y = ones([m n]) returns an m-by-n matrix of ones. ______________________________________________________ indices = find(X) returns the linear indices corresponding to the nonzero entries of the array X XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ********************************************************** MATLAB syms x y z That defines those symbolic variables. >> A= (x+y+z)^2 A = (x+y+z)^2 >> B = expand(A) B = x^2+2*x*y+2*x*z+y^2+2*y*z+z^2 >> C = latex(B) C = {x}^{2}+2\,xy+2\,xz+{y}^{2}+2\,yz+{z}^{2} >> whos Name Size Bytes Class A 1x1 142 sym object B 1x1 182 sym object C 1x41 82 char array ans 1x1 8 double array x 1x1 126 sym object y 1x1 126 sym object z 1x1 126 sym object Grand total is 88 elements using 792 bytes >> (x+y)^2 ans = (x+y)^2 >> D= latex (ans) D = \left( x+y \right) ^{2} *@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@*@* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ----------------------------------------------------------------------