%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% FIRST DAY MATLAB worksheet %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for MTH 451-551 %%% this file helps to get familiar with basic elements of MATLAB %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% M. Peszynska, 2017/09/25 %% Copyright Department of Mathematics, Oregon State University %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% INSTRUCTIONS: please type every line yourself %%% ignore all lines and text beginning with % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diary yourname doc doc %% 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 = 1e9; x = zeros(n,1); %%%%%%%%% EXERCISE: push it further. How far can you go ? %% 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: do SAXPY z=x+alpha*y where alpha is a scalar %% how to set-up a simple loop to compute the sum n = 3; s = 0; for j=1:n s = s + x(j); end s %% compare to MATLAB's version sum(x) %%%%%%%%% EXERCISE: write loops to calculate inner product, SAXPY, %%%%%%%%%% EXERCISE: compute Frobenius 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: (check 'fro') norm(A), norm(A,1), norm(A,2), norm(A,infty) %%%%%%%%%% EXERCISE: compare these norms with eigenvalues %%%%%%%%%% EXERCISE: verify the inequality between %%%%%%%%%%%% norm(A,2) and product of norm(A,1),norm(A,inf) %%%%%%%%%%%% (make some observations, then a conjecture). %%%%%%%%% WHAT is "i" and play with complex matrices i A = [ 1, i; -2+3*i; 0] norm(A) norm(A,1) eig(A) %%%%%%%%% EXERCISE: experiment with unitary matrices %%%%%%%%% EXERCISe: what about A=[1 1; 0 1] ? %% how to plot x = 0:.01:6*pi; plot(x,sin(x)); %%%%%%%%%% EXERCISE: plot exp(x) and use log log plots %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%% EXERCISE: what does A do to a vector ? %% set up some 2x2 matrix of your choice theta = linspace(0,2*pi); u=[cos(theta);sin(theta)]; size(u); plot(u(1,:),u(2,:)); axis equal; u=A*u; %% now change u to be an ellipse. How ? %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%% understand floating point precision %% find x for which ((1-x)-1)/x is not what it "should" be %% find a and b for which a+b %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% HOMEWORK %%%%%%%% Go to mathworks.com/moler/chapters.html %%%%%%%% Read Chapter 1: Introduction to Matlab. Solve 1.2 and 1.38 %%%%%%%% Read Chapter 2: Linear Equations. Solve 2.1 and 2.5a %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%