250 likes | 450 Views
Introduction to MATLAB Session 1. Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011. About this course. Focus of this course. ...in learning to use MATLAB software Not in numerical methods (but some examples)
E N D
Introductionto MATLABSession 1 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011
Focus of this course • ...in learning to use MATLAB software • Not in numerical methods (but some examples) • …to get some idea of the possibilities of MATLAB • We will not go through all properties of MATLAB. • To get into a position for learning application specific features. Introduction to MATLAB - Session 1
Schedule and passing Schedule • Five 3-hour sessions • Homework Session working style • Introduction to the topic + exercises, learning by doing Homework • Estimated work load 5-10 hours • More information later Passing the course (2 credits, passed/failed) • Attendance min 3/5 sessions + passing the homework Introduction to MATLAB - Session 1
Session 2 Some matrix commands Logical expressions Graphics 1 Session 3 My functions + strings, cells Controlling program flow Session 4 Function functions Session 5 Graphics 2 More linear algebra Starting homework Contents of this course • Session 1 • General • Matrices • M-files Introduction to MATLAB - Session 1
What is MATLAB? • Matrix Labratory • Array/Matrix is the basic data element • Environment for numerical computing >> quad(@(x) exp(sqrt(x.^2+1)), 0, 1) ans = 3.1769 • not for symbolic calculus • Library of mathematical functions • Programming language • Application-specific toolboxes Introduction to MATLAB - Session 1
MATLAB desktop view Introduction to MATLAB - Session 1
Basic idea Workspace Matrices and other data elements are storaged in the workspace Commands who and whos for listing the variables Execute commands (functions) to manipulate the matrices in the workspace Matlab interpretes the commands, no compiler Getting started • Start MATLAB • Create a working directory • Under your personal home directory • Set ”Current directory” to your working directory • Create a variable v by typing >> v = 5 in a command window and try who and whos. • Write >> exp(v) and check the workspace. Introduction to MATLAB - Session 1
Matrix: the basic data element (n,m) array Vector (n,1) matrix = column vector (1,m) matrix = raw vector Scalar (1,1) matrix Two ways to create matrices: List the elements = is the substitution Use [ ] brackets Built-in functions (Load from a file) Try the following: >> A = [1 2 3; 5,6,7] >> [1 2 3; 5,6,7] >> B = [1 2 3; 5,6,7]; >> B >> C=[A; B] >> D = ones(2,4) >> E = zeros(3) >> E2 = zeros(1000) >> F = zeros(1,4) >> G = rand(10,1) >> H = randn(10,1) >> I = eye(5) >> J = [ ] Matrices (and vectors and scalars) Introduction to MATLAB - Session 1
Creating vectors with :, the colon command a:b from a to b with step one a:d:b from a to b with step d Multi-dimensional matrices Some scalars i and j : complex number eps : small number pi : p Inf, NaN Hint: Use ; for not showing the result on the command window Hint: Command size(A) returns the size of matrix A Try the following >> 1:4 >> 0:5:20 >> v = 20:-5:0 >> rand(2,3,4) >> i >> 5+3*i >> i = 5 >> 5+3*i >> eps >> pi >> 5e3 >> 5.4e-3 >> beta >> help beta More matrices Introduction to MATLAB - Session 1
Matrix arithmetics A+B, A-B A+c, A-c Matrix multiplication A*B Transpose A.’ and adjoint A’ Matrix power A^c For noninteger c with spectral calculus \ left matrix divide Ax=y x=A\y = inv(A)*y / right matrix divide Array arithmetics A+B, A-B A+c, A-c Array multiplication A.*B Array power A.^c, A.^B Array divide A./B Matrix arithmetics A, B matrices, c scalar – matrix sizes must match! Introduction to MATLAB - Session 1
Let v = [v1 v2 ... vn]. Refer to element vj with v(j) v(J) J can be a matrix of indeces v(J) is a matrix of size(J) and of elements defined by J Hint: Command length(v) returns the length of vector v Try the following: >> v = 0:5:30 >> v([1 1 3]) >> v(2) = 15 >> v(2:4) = 3:5 >> J = [1 1; 2 2]; >> v(J) >> v(10) >> v(10)=100; Reffering to vector elements Introduction to MATLAB - Session 1
Some properties Refer to entire column/raw with the colon : A(:,5) A(2,:) A(:) is the matrix A as a vector Referring with end –command A(3,5:end) A(2:end, : ) Let A = [a11 a12 ... a1m a21 a22 ... a2m an1 an2 ... anm]. Refer to element ajk with A(j,k) or A(n*(k-1)+j) Consider matrix as a column vector (columns consecutively) A(J,K) with index matrices J,K Reffering to matrix elements Introduction to MATLAB - Session 1
Try the following >> A = [1:4;5:8;9:12] >> A(2,4) >> A(11) >> A(2,1:3) >> A(2,:) >> A([2 3],[3 1]) >> A(2:end,3) >> A(5:end) >> reshape(A,4,3) >> help reshape …Reffering to matrix elements Introduction to MATLAB - Session 1
MATLAB m-files • Not practical to write all commands in the command window use m-files • Text files of type *.m • For example, test1.m • Each line of an m-file is a MATLAB command line • MATLAB executes the lines of an m-file by writing the name of the file in the command window, (or F5 from the editor) • >> test1 • Visibility: m-file has to be in the MATLAB’s current directory (or in the MATLAB root) • Can be written with any text editor • …but the MATLAB editor is preferable • File New m-file (blank) Introduction to MATLAB - Session 1
Problems • Go throw the previous ”Try the following” exercises • Are the following vectors the same? • a = [1 -2 3] • b = [1 - 2 3] • c = [1-2 3] 3. How much memory does the matrix I=ones(1000) need? How about I=ones(10000)? Clear memory with ”clear” command. 4. Set a=1 and b=i. Check the memory usage (whos). 5. Compute a) log(-1), b) sqrt(-1), c) 1/0. 6. See help format, and try: >> format long >> format short >> pi >> pi Introduction to MATLAB - Session 1
Problems Matrix manipulation – write your answers to m-files 7. Create (5,5) –matrix of zeros. • Substitute random numbers (with rand –command) to raws 3-5. • Delete the first and the last columns (=substitute empty matrix). • Create a vector [0 1 0 1 … 0 1] of length 20. • Create a vector [1,3,5,...,999,2,4,6,...,1000]. • Create a vector [2,1,4,3,6,5,...,998,997,1000,999]. 8. Create a (100,100) -matrix 1 2 3 4 5 … 100 1 2 3 4 5 … 100 … 1 2 3 4 5 … 100 Hint: Multiplication with matrix ones(100,1). Introduction to MATLAB - Session 1
Problems 9. Let A = [1 2; 0 3]; • Compute A*ej for e1 = [1;0] and e2=[0;1]; • Compute A^2 and A.^2. • Solve equation Ax=y for a) y = [1;0]; b) y= [2;3]; c) y = [ i ; 0]; 10. Create a ( : ,1000) matrix • X2 whose columns are R2 rand vectors in the unit square, • X3 whose columns are R3 rand vectors in the unit cube. • Compute the norms of the random vectors. • Calculate the average of norms of the random vectors. 11. Let p be a vector of annual interest rates% and let s be a vector of initial investments. Create a table A for the values of the investments after 10 years, A(j,k) = s(k)*(1+p(j)/100)10. You can use e.g., p= .5:.5:5 and s = 2000:2000:10000. Introduction to MATLAB - Session 1
Problems 12. Study sparse matrices from the MATLAB help >> help sparse • Create a sparse matrix S of size 2000x2000 with all diagonal elements 1, S(j,j)=1, and random lower diagonal S(j+1,j) = random number, j=1,…,1999. • Check that S is correct with full(S(1:10,1:10)). • Set y = ones(2000,1); and S2=full(S); • Compare with tic toc –commands (see help tic) the speed of solving Sx=y by • tic; x=S\y; toc • tic; x=S2\y; toc • tic; x=inv(S)*y; toc (see help inv) • Can you explain the result? Introduction to MATLAB - Session 1
>>quit …to exit MATLAB.