Download presentation
Presentation is loading. Please wait.
Published byMarshall Marshall Modified over 9 years ago
1
Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010
2
About this course
3
Introduction to MATLAB - Session 1 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.
4
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
5
Introduction to MATLAB - Session 1 Contents of this course 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 Session 1 Genaral Matrices M-files
6
MATLAB
7
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
8
Introduction to MATLAB - Session 1 MATLAB desktop view
9
Introduction to MATLAB - Session 1 Getting started 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 1. Start MATLAB 2. Create a working directory Under your personal home directory 3. Set ”Current directory” to your working directory 4. Create a variable v by typing >> v = 5 in a command window and try who and whos. 5. Write >> exp(v) and check the workspace.
10
MATRICES
11
Introduction to MATLAB - Session 1 Matrices (and vectors and scalars) 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: 1. List the elements = is the substitution Use [ ] brackets 2. 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 = [ ]
12
Introduction to MATLAB - Session 1 More matrices 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 : 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
13
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 A, B matrices, c scalar – matrix sizes must match !
14
Introduction to MATLAB - Session 1 Reffering to vector elements 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;
15
Introduction to MATLAB - Session 1 Reffering to matrix elements 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
16
Introduction to MATLAB - Session 1 …Reffering to matrix elements 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
17
M-FILES
18
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)
19
Problems Session 1
20
Introduction to MATLAB - Session 1 Problems 1. Go throw the previous ”Try the following” exercises 2. 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
21
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 Hint: Multiplication with matrix ones(100,1).
22
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 a)X2 whose columns are R 2 rand vectors in the unit square, b)X3 whose columns are R 3 rand vectors in the unit cube. Compute the lengths of the random vectors. Calculate the average length 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.
23
Introduction to MATLAB - Session 1 Problems 12.Study sparse matrices from the MATLAB help >> help sparse 13. 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?
24
>> quit …to exit MATLAB.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.