%Matlab primer 1 - mprim1.m %In this document, I want to illustrate simple Matlab functions, including: %data i/o, plotting, etc. %Some helpful hints: %1) Matlab stands for ``Matrix Laboratory'' - this means that Matlab is %designed to work well with matrices. As such, it is much faster to use %intrinsic matrix routines to operate on matrices rather than writing %elementary code (a la FORTRAN). For example, if we want to multiply two %conformable matrics A, B together to form C, we can simply write C = A*B %in Matlab. A = [1 2; 3 4] B = eye(2); C = A*B pause %The above statement is true for other algebraic operations as well. Note %that a semicolon suppresses output. C = A + B C = A - B pause %We can do matrix inverses in two ways: C = inv(A) C = A \ B pause %I have been told that method 2 is faster as it uses a faster algorithm %than inv(), but that could be bunk. Suppose we want to multiply a matrix %by a scalar. lambda = 10; C = lambda * A %Easy, right? Suppose now we want to add a matrix of ones to A. We can do %this in three ways. C = A + ones(2) C = A + repmat(1,2,2) C = A + 1 pause %The third technique is nice as it shows us that Matlab will automatically %make a scalar conformable to a matrix for algebraic operations. Matlab %has to ways of operating on matrices: all full matrices or %element-by-element. For example, if we want to compute A*A = A^2, we simply %write: C = A^2 %If however we want to compute the matrix of squares of each element of A, %we write: C = A.^2 pause C = A.*B %Functions like exp, log, sin, etc. work element-wise in Matlab. C = log(A) C = exp(A) C = sin(A) pause %You can write do/while loops in Matlab if you please, they are done in the %following manner. B = eye(2); [na,ma] = size(A) [nb,mb] = size(B) if (ma ~= nb) disp('Houston, we have a problem: A and B are not conformable') break else for i = 1:na for j = 1:mb C(i,j) = A(i,:) * B(:,j); end; end; disp(C) end; %In general, the above way to compute A*B is inefficient - use A*B instead pause %Since we are now comfortable with the basic methods of using Matlab, lets %do some plotting. The easiest way to do this is via the ``plot'' %function. tvec = [0:100]'; svec = sin(tvec/100*2*pi); plot(tvec,svec) pause %Now let's plot the cosine function instead cvec = cos(tvec/100*2*pi); plot(tvec,cvec) %Note that the old plot has been overwritten by the new plot. If we wanted %to plot them both together, we can do so by doing: pause plot(tvec,svec,tvec,cvec) legend('sin','cos') %or plot(tvec,svec, 'bo') hold on plot(tvec,cvec,'g+') legend('sin','cos') hold off %There are a TON of plot options - type help plot in the Command Window to %see. If you want to plot the above two functions in separate figures, %there are two ways to do this. %close pause figure hold on box on plot(tvec,svec) title('sin') xlabel('t') ylabel('sin(t)') hold off figure hold on box on plot(tvec,cvec) title('cos') xlabel('t') ylabel('cos(t)') hold off %or pause figure subplot(2,1,1) hold on box on plot(tvec,svec) title('sin') xlabel('t') ylabel('sin(t)') hold off subplot(2,1,2) hold on box on plot(tvec,cvec) title('cos') xlabel('t') ylabel('cos(t)') hold off