% Script file for Math 305 - lab session 1 % % Copy into your command window each of the following commands. % Try to understand the commands as you go. % Do the exercises, which are variations on what is done; copy % the commands you make in the three exercises into an email and % send it to me at gautier@math.hawaii.edu % Save your work either using 'Save workspace' in the Edit Menu % or by copying all your work into a file using Edit and saving % it. % Matlab can act as a calculator; operators are: + - * / ^ [5+8, 87*123, 216/4, 10^2] % Matlab has many standard functions a=sqrt(10) a=sin(3*pi) % this should = 0 but doesn't because matlab uses approximate arithmetic log(10) %Natural log atan(0) % Arctan % help command - gives you the help page for the command help tan % doc command - gives you a better formated help page doc tan % create a vector from 1 to 10 using colon operator x=1:10 % create a vector from 1 to 10 with increments of 2 x=1:2:10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % EXERCISE 1 % Write as progressions (like in the exemple above) the vectors [1,2.5,4,...,34] and [2, 1.9, 1.8,1.7,...,0] %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % length command returns the length of a vector length(x) % Entering matrices: % 1. separate elements of a row with blanks or commas x = [16 3 2 13] xx = [16,3,2,13] % 2. use semicolon to separate rows y = [16;5;9;4] % 3. The default multiplication operator * is matrix multiplication: x*y y*x % 4. to force element by element multiplication use .* x.*x y.*y % 5. Enter the matrix: A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] % 6. What are the dimensions of this matrix? size(A) % gives the size in each dimension % 7. This matrix is known as a magic square it has some special properties: % 8. All row sums and column sums are equal: sum(A) % sums the elements in each column of A % 9. To get the row sums, first transpose the matrix A' % note the columns of A are the rows of A' and vice-versa sum(A')' % this gives the row sums of A % 10. to sum the diagonal elements of A diag(A) % gets the diagonal of A sum(diag(A)) %sums the diagonal elements % The element in ith row ans jth column is denoted by A(i,j) X=A A(2,3) A(2,3)+A(3,2) % sums two elements A(2,3)=A(2,3)+A(3,2) % replaces an element A=X % You get an error message if you ask for an element % outside the matrix A(4,5) % You can increase the size of the matrix by adding an element X=A; X(4,5) = 17 % you can get a row or column by using the : instead a subscript r1=A(1,:) % row 1, all columns c1=A(:,1) % column 1, all rows %Extract a subset of a row B=[1 2 3 4 5 6; 7 8 9 10 11 12; 13 14 18 16 17 18] B(2,1:2:size(B,2)) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % EXERCISE 2 % Extract the entries in the odd columns of the third row of the matrix B %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % deleting a column X=A; X(:,2)=[] % deletes the second column X=A; X(2,:)=[] % deletes the second row % Concatination is the process of joining small matrices to make bigger ones. % Note: [] is the concatination operator B=[A A+2; A+10 A] % This produces an 8x8 matrix % Matlab provides buit-in functions for generating matrices Z=zeros(2,4) % 2x4 matrix of zeros F=5*ones(3,3) % 3x3 matrix of 5's % Identity matrix eye(3) % A simple plot x=1:0.1:10; y=sin(x*pi/2); plot(x,y) % add another line z=sin(x*pi/3); hold on plot(x,z,'r-') hold %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % EXERCISE 3 % Plot the function arctan from -10 to 10. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%