%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % this is a little matlab script to introduce you to some of the syntax of matlab % and how you do basic vector and matrix vector operations. Have fun. Marc % % To run this script...first start Matlab then either just cut or paste in the lines % or just type the name of the script at the prompt. % % For more help and a matlab tutorial, type helpdesk at the prompt and go to " % "Getting Started" under the MATLAB DOCUMENTATION % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all % % defining vectors...A column vector can be written in 2 ways % % use the ; (semi-colon) to separate rows disp([' here is x=[ 1; 2; 3]']); % this just spits out some text x=[ 1; 2; 3] % % or write x as a transposed row vector (using the ' (quote) transpose operator) % disp([' here is x=[ 1 2 3]''']); x=[ 1 2 3]' % % note the difference between a row vector and column vector (here we use the % transpose operator ' again % x_transpose=x' % % We can calculate the length of a vector x several ways % % % 1) use the function norm % disp([' this is length_x=norm(x)']); length_x=norm(x) % % 2) use the functions dot and sqrt (square root) % disp([' this is length_x=sqrt(dot(x,x))']); length_x=sqrt(dot(x,x)) % % or we can use the transpose definition of the dot product and sqrt % disp([' this is length_x=sqrt(x''*x)']); length_x=sqrt(x'*x) % they should all be the same %-------------------------------------------------------------- % Fun with matrices: % consider the 3x3 matrix given in class A=[ 2 1 3 ; 4 3 8 ; -2 0 3 ] % % and the column vector % x=[ 1 -2 1 ]'; % % calculate b=A*x % disp([' b=A*x']); b=A*x % % let v=the third column of A % disp([ ' the third column of A can be extracted using v=A(:,3)']); v=A(:,3)