%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% FIRST DAY MATLAB worksheet %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for MTH 654 %%% this file helps to get familiar with basic elements of MATLAB %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% M. Peszynska, 2009/10 %% Copyright Department of Mathematics, Oregon State University %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% INSTRUCTIONS: please type every line yourself %%% ignore all lines and text beginning with % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diary yourname %% allocate (reserve, initialize) space for a vector n = 100; x = zeros(n,1); %% how large can a vector be ? n = 10^5; x = zeros(n,1); n = 10^9; x = zeros(n,1); %% how to input a matrix A = [2 3 5; 7 11 13; 17 19, 23] %% how to access its elements A(1), A(3) %% the transpose of a matrix A' %% how to access the first column and rows from 1:2 A (1:2,1) %% how to access the first column and all rows A (1:3,1) A (:,1) %% how to get a matrix with random entries B = rand (3,5) %%%%%%%%%% EXERCISE: swap the first and last columns of B %% input a column vector x = [1; 2; 3] %% input a row vector y = [1 2 3] %% change from a row vector to column vector x = y' %% operations on vectors: multiply a vector by a scalar pi * x y = y' %% (now y is a column vector) %% dot (inner) product of two column vectors x, y, x'*y %% compute outer product x * y' %% compute a vector as a component-by-component product of x and y x.*y %%%%%%%%%% EXERCISE: compute all possible products of A and B %% how to set-up a simple loop to compute the dot product: try n = 3; c = 0; for i=1:n c = c + x(i) * y(i); end %%%%%%%%%% EXERCISE: compute Frobenious norm of matrix A directly %% how to get the size of a vector/matrix if forgotten size(x) %% identity matrix eye(4) %% eigenevalues of a matrix eig (A) %% various norms of matrices: norm(A), norm(A,1), norm(A,2), norm(A,infty) %%%%%%%%%% EXERCISE: compare these norms with eigenvalues %% how to plot x = 0:.01:6*pi; plot(x,sin(x)); %%%%%%%%%% EXERCISE: plot exp(x) and use log log plots %% close the session