% this is the matlab diary from Friday's class, Sept 8, 2000 % start a diary > diary 9_8_00 % create a 3x3 matrix > A = [1,2,3;4,5,6; 7,8,9] A = 1 2 3 4 5 6 7 8 9 % one can multiply a square matrix by itself: A*A makes sense: > A^2 ans = 30 36 42 66 81 96 102 126 150 % if you want to square _each_ entry in A then you need to use .^ > A.^2 ans = 1 4 9 16 25 36 49 64 81 % create a vector that starts at 0, ends at 5, with increments of .1 > x = 0:.1:5 x = Columns 1 through 7 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 Columns 8 through 14 0.7000 0.8000 0.9000 1.0000 1.1000 1.2000 1.3000 Columns 15 through 21 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 Columns 22 through 28 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 Columns 29 through 35 2.8000 2.9000 3.0000 3.1000 3.2000 3.3000 3.4000 Columns 36 through 42 3.5000 3.6000 3.7000 3.8000 3.9000 4.0000 4.1000 Columns 43 through 49 4.2000 4.3000 4.4000 4.5000 4.6000 4.7000 4.8000 Columns 50 through 51 4.9000 5.0000 % if I don't want to see the result of creating the vector, use ";" > x = 0:.1:5 ; % I can now plot x versus sin(x) with a solid line. > plot(x,sin(x)); % I can now plot x versus sin(x) with a dashed line. > plot(x,sin(x),'--'); figure(1) % I can now plot x versus sin(x) with a red solid line. > plot(x,sin(x),'r-'); figure(1) % I can now plot x versus sin(x) with a solid that also marks the pts. > plot(x,sin(x),'.-'); figure(1) % want to plot f(x) = x^2... > plot(x,x^2) Error using ==> ^ Matrix must be square. % problem! can't square a non-square matrix! solution: > plot(x,x.^2), figure(1) % what is x? > whos x Name Size Bytes Class x 1x51 408 double array Grand total is 51 elements using 408 bytes % define y: > y = x.^2; % what is y? > whos y Name Size Bytes Class y 1x51 408 double array Grand total is 51 elements using 408 bytes % redefine x to be more refined. > x = 0:.01:5 ; % what is x? > whos x Name Size Bytes Class x 1x501 4008 double array Grand total is 501 elements using 4008 bytes % plot x versus y > plot(x,y); Error using ==> plot Vectors must be the same lengths. % problem! x is 501 long, y is 51 long, can't plot them against each % other. solution: redefine y. > y = x.^2; % plot x versus y and put labels on the plot to prepare it for printing > plot(x,y); figure(1); > title('this is the plot for problem 1'); > xlabel('time'); > ylabel('population of bunnies'); > figure(1) > gtext('look right here!'); % want to plot sin(x) and sin(2x) on same plot > plot(x,sin(x)); figure(1) > plot(x,sin(2*x)); > figure(1) % problem! sin(2x) erased sin(x). solution: use "hold on" > hold on % now plot a bunch of sines. > plot(x,sin(x),'r-'); > plot(x,sin(3*x),'b-'); > plot(x,sin(4*x),'g-'); > figure(1) % want to do a new plot. One solution: create a new window and plot there > figure(2) > plot(x,log(x)) Warning: Log of zero. > figure(2) % other solution: go to old window and use "hold off" to allow sines % to be written over. > figure(1) > hold off > plot(x,log(x),'r-'), figure(1) Warning: Log of zero. % save the diary! > diary off