6 August 2006 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. ********************************************************** 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 file named rasprog.m. Then just type in 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 RUNNING MATLAB AT IU Put in the IU CD that allows windows. It will start up automatically. Then type ssh erasmuse@libra.uits.iu.edu Then type matlab If that doesn't work, type matlab -display hostname:0 With that, I can cut and paste to WINDOWS. I can't go from Windows to matlab, though. And I can't display plots. For Matlab 6: OR: ssh erasmuse@steel.ucs.indiana.edu I can cut and paste from Windwos to Matalb in this, and Matlab 7. But STEEL is immensely slower than LIBRA. ********************************************************** MATLAB Matlab documentation I can't do plotting, but I can do equation solving. matlab is on Aries09 only, not the other Aries such as aries01. Stata is on aries05. 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 ----------------------------------------------------------------------