echo on % This script file introduces MATLAB in the contest of solving a simple % linear algebra problem. It then explores some of MATLAB's plotting % capabilities. The lines that begin with % are comments to the user. % Lines without a % are MATLAB commands that will be executed. These % commands could, of course, also be typed in interactively. % % The linear algebra problem: % % What is the rank of: % % | 1 7 -2 6 | % A = | 3 1 1 5 | % | 2 0 6 8 | % | -6 1 11 6 | % % % press any key to continue pause % Enter the Matrix A into MATLAB A = [1 7 -2 6;3 1 1 5;2 0 6 8;-6 1 11 6] % Extract all elements of the columns of A into vectors. % The semicolons following each line suppress the output. A1 = A(:,1); A2 = A(:,2); A3 = A(:,3); A4 = A(:,4); % press any key to continue pause % Sum the columns to show that the columns are not linearly independent. % I could see this by inspection. col_sum = A1 + A2 + A3 - A4 % Since column 4 is the sum of the first three columns rank(A) % is at most 3. We can show that the first three columns are in fact % linearly independent by doing row reduction on the matrix A. This % can be done by hand, or we can let MATLAB do the arithmetic with the % command rref(A). % press any key to continue pause rref(A) % There are three linearly independent columns in A, and its rank is 3. % of course, MATLAB could have computed this directly: rank_of_A = rank(A) % press any key to continue pause % MATLAB can also find the null space of A. The command null(A) % returns an orthonormal basis for the null space of A null_A = null(A) % In this case A's null space consists of multiples of this vector % press any key to continue pause % So, by definition, multiplying this vector by A should result in zero: A*null_A % Since we're doing this with floating point arithmetic, there is some % error. We didn't get exactly zero. Notice that in double precision % (MATLAB's default) the largest error is about 10^(-15). % press any key to continue pause % Plotting in MATLAB % generate a 100 element equally spaced vector from 0 to L L = 1; x = linspace(0,L); % We can create a matrix of eigenfunctions % Note that MATLAB functions often accept vector arguments N = 3; % number of eigenfunctions y = zeros(4,length(x)); % I'll have MATLAB stop echoing the commands while it executes the following % for loop. Echo during loops will make you dizzy! % % for count = 1:N, % y(count,:) = sin(count*pi*x/L); % end echo off for count = 1:N, y(count,:) = sin(count*pi*x/L); end echo on % press any key to continue pause % The n_th row of the 4 x 100 matrix y now contains sin(n*pi*x/L). We can % plot these against x, using different line types. plot( x,y(1,:) ) hold on % leave on the previous plot and any other plots until "hold off" plot( x,y(2,:),'--') plot( x,y(3,:),'.-') hold off % the next plot command will erase all of these plots % add labels, titles, and grid marks xlabel('x (units of L)') ylabel('f_n(x)') title('A Multiple Plot') legend('sin(\pix/L)','sin(2\pix/L)','sin(3\pix/L)',-1) grid on echo off