Basic MATLAB

Basic MATLAB

Basic MATLAB Scientific Computing, Bridging course Scientific Computing and Matlab n Matlab is a tool and programming environment used in computational science but also in other areas. n A simple environment to quickly test ideas and to study the results n Also available in other tools, such as Freemat, Maple, Mathematica, Comsol Multiphysics, and other programming languages like C/C++, Fortran, Java. Informationsteknologi Institutionen för informationsteknologi | www.it.uu.se 1 What is MATLAB? n Developed by MathWorks, Inc. http://www.mathworks.com n First version already in late 70s n Originally MATrix LABoratory. n Matemathical environment for ® Numerical computations ® Graphics ® Programming n Many powerful predefined functions with the ability to define your own. n Own object oriented programming language. Informationsteknologi Collaboration with C++, Java and Fortran possible. Institutionen för informationsteknologi | www.it.uu.se What is MATLAB? n Over 25 plug-in (toolboxes) are available for special applications, eg: ® Signal analysis ® Image analysis ® Statistics ® Symbolic mathematics ® Finacial mathematics ® Parallel computing ® … n Run under UNIX/Linux, Windows or Macintosh. Informationsteknologi Institutionen för informationsteknologi | www.it.uu.se 2 Application areas Wherever there are computational problems n Education: mathematics (specifically linear algebra), computational science, physics, chemistry, engineering, economics, etc. n Research: lab environment to test the solution methods, study, analyze problems, perform calculations and visualize the results. n Industry:used in the same manner as in research, for example on development Informationsteknologi departments in biotechnology, electronics, automotive, etc. Institutionen för informationsteknologi | www.it.uu.se MATLAB environment n The development environment (MATLAB desktop) has a plurality of windows, Command window, Current folder, Workspace and Command History. Task: Start Matlab on your computer and change to a Informationsteknologi prefered folder by clicking it in Current folder window. Institutionen för informationsteknologi | www.it.uu.se 3 MATLAB environment n MATLAB is usually controlled from the command window (Command Window), but also from the menus. n Commands are given by »-prompter and executed when return key is pressed. n Example: >> 42 + 19 ans = 61 Informationsteknologi >> exit n Exit matlab: Institutionen för informationsteknologi | www.it.uu.se MATLAB environment n In the command window, >> a = 75 you can work interactively as an advanced calculator. a = 75 The semicolon suppresses printing. >> b = 34; >> c = a*b If no variable names specified, the variable is c = ans (= answer) 2550 >> a+b Task: Compute the sum and Informationsteknologi product of a,b,c and store the ans = results in new variables. Note 109 the use of semicolon. Institutionen för informationsteknologi | www.it.uu.se 4 MATLAB environment n Help is available through the Help menu n But even writing demo in the command window provides information on how to use Matlab. Informationsteknologi Task: How do you perform square root in Matlab? Institutionen för informationsteknologi | www.it.uu.se MATLAB environment Help for individual commands can be done directly in the command window >> help command Ex) What does the command exit ? >> help exit EXIT Exit from MATLAB. EXIT terminates MATLAB. Informationsteknologi Institutionen för informationsteknologi | www.it.uu.se 5 Variables in MATLAB A variable in MATLAB n can be viewed as containers of a value of a certain type (integer, float, char, ...) n always have a name. Must begin with a letter. Do not use å, ä, ö, space, hyphen (minus), plus signs etc in the name n can be assigned a value n created when needed, >> a = 3 without special "declaration” a = (is of type "matrix"). 3 n Can be predefined, >> pi Informationsteknologi e.g. pi,eps,realmax ans = 3.1416 Institutionen för informationsteknologi | www.it.uu.se Variables in MATLAB n The variables are displayed in Workspace- window n The issued commands are displayed in Command History Informationsteknologi Institutionen för informationsteknologi | www.it.uu.se 6 Variables in MATLAB n Can also list the variables in the , command window using who whos >> who Your variables are: a ans >> whos Name Size Bytes Class a 1x1 8 double array ans 1x1 8 double array Grand total is 2 elements using 16 bytes Informationsteknologi Institutionen för informationsteknologi | www.it.uu.se Variables in MATLAB n Variables can be displayed in various formats >> y = sin(2*pi/3); >> format short e; y >> y y = y = 8.6603e-001 0.8660 >> format short; y >> format long; y y = y = 0.8660 0.86602540378444 % Standardformat >> format long e; y e-001 equals to 10-1 y = 8.660254037844387e- Informationsteknologi 001 Task: Display pi in different formats (note the accuracy is not changed only the format). Institutionen för informationsteknologi | www.it.uu.se 7 Variables in MATLAB n Variables can be saved to file and loaded up on another MATLAB session later. n In the menu use the buttons Save Load n Variables will be saved as so-called MAT-files, files with the extension.mat n MAT-files are called binary and is not readable or editable. Informationsteknologi n Can also use save and load command directly in the command window Institutionen för informationsteknologi | www.it.uu.se Save and load variables n The button Import Data can be used to load other formats such as spreadsheets (Excel), audio files, video files, etc. n Select a file format in the window that opens after clicking the button Informationsteknologi Institutionen för informationsteknologi | www.it.uu.se 8 Functions There are lots of predefined functions, such as for n Elementary mathematics n Linear algebra n Graphics in 2D and 3D n Integrals and differential equations n Statistics n Curve fitting Example: abs(x),sqrt(x),sin(x),log(x),log10(x),… (Help -> Documentation -> Matlab -> Mathematics) Informationsteknologi There are lot of Toolboxes with specilized functions. You can also create your own functions. Institutionen för informationsteknologi | www.it.uu.se Matrices n Matrix is the basic data type. n A two-dimensional matrix is a table with rows and columns. n A matrix with m rows and n columns, has the size mxn (m times n). n Vectors are special cases of the matrices, then the number of rows or number of columns is 1. Example: row vector, 1xn, and column vector, mx1. n A matrix of size 1x1 called scalar. n Each value in the matrix are called elements. Informationsteknologi Institutionen för informationsteknologi | www.it.uu.se 9 Vectors Column vector and row vector in MATLAB >> vcol = [ 1; 2; 3; 4; 5 ] >> vrow = [5 6 7 8 ], x = 3 The vcol = % Column vector semicolon 1 into vectors 2 provide 3 new row 4 5 vrow = % Row vector 5 6 7 8 Informationsteknologi x = % Scalar 3 Institutionen för informationsteknologi | www.it.uu.se Vectors Change individual elements >> vrad(1) = -1.1; vrad(2) = 3.5; >> vrad vrad = -1.1000 3.5000 >> vkol = vrad' %transpose vkol = -1.1000 3.5000 Informationsteknologi vrad(1) indicates that the index 1 (position 1) to be assigned. The ’ means transpose. Institutionen för informationsteknologi | www.it.uu.se 10 Generate vectors n Colon notation is often used to create vectors start:stride:stop if stride = 1 it can be omitted >> vektor = 0:5 vektor = 0 1 2 3 4 5 >> vektor2 = 0.0:0.05:2.0 vektor2 = Informationsteknologi 0 0.0500 0.1000 0.1500 ... 1.9500 2.0000 Institutionen för informationsteknologi | www.it.uu.se Generate vectors The command linspace also create vectors linspace(start,stop,elements) >> v = linspace(0,2,10) v = 0 0.2222 0.4444 0.6667 0.8889 1.1111 1.3333 1.5556 1.7778 2.0000 These two methods are used, for example, when creating the x-axis at the graphics Informationsteknologi Task: Create a vector in two different ways with equally spaced elements using stride 2*pi/100 in the interval [-pi, pi]. Institutionen för informationsteknologi | www.it.uu.se 11 Generate Matrices >> A = [1 7; 5 3]; ⎡1 7⎤ Create A = ⎢ ⎥ >> A ⎣5 3⎦ A = Semicolon or return 1 7 in the matrix gives a 5 3 new row >> A = [1 7 Semicolon in the end suppresses printing 5 3] A = Task: Create a 3x4 Informationsteknologi matrix and then 1 7 transpose it. 5 3 Institutionen för informationsteknologi | www.it.uu.se Creating a Matrix elementwise ⎡1 7⎤ A = ⎢ ⎥ ⎣5 3⎦ >> A(1,1)=1; A(1,2)=7; >> A(2,1)=5; A(2,2)=3; >> A A = 1 7 Informationsteknologi 5 3 Institutionen för informationsteknologi | www.it.uu.se 12 Special matrix commands n Available built-in >> B = ones(2,3) features to create the B = usual (and unusual) 1 1 1 matrices n Can create more 1 1 1 advanced matrices by >> eye(2) combinations of these ans = 1 0 eye(n) identity-matrix 0 1 ones(m,n) one-matrix zeros(m,n) zero-matrix >> C = zeros(2,2) rand(m,n) rand-matrix C = Informationsteknologi 0 0 + numerous other 0 0 commands Institutionen för informationsteknologi | www.it.uu.se Extending a matrix ⎡1 7⎤ Given A = ⎢ ⎥ >> A = [A; 2 1]; ⎣5 3⎦ alternative ⎡1 7⎤ create A ⎢5 3⎥ >> temp = [2 1]; = ⎢ ⎥ ⎣⎢2 1⎦⎥ >> A = [A; temp]; Separate row and column vectors! >> temp2 = [2;1]; >> A = [A; temp2]; ??? Error using ==> vertcat Informationsteknologi All rows in the bracketed expression must have the same number of columns. Institutionen för informationsteknologi | www.it.uu.se 13 Elements, rows and columns n One can work with individual elements, rows, columns, submatrices >> A(2,1) ans = 5 >> A(2,:) ans = 5 3 >> A(2,:) = [0 0] Colon (:) denotes the whole range (all rows A = or all columns) Informationsteknologi 1 7 0 0 2 1 Institutionen för informationsteknologi | www.it.uu.se Submatrices, colon notation Submatrices of the matrix A, mxn can be created quickly with colon notation n A(:,j) j:th column of A n A(i,:) i:th row of A n A(i:k,j:m) submatrix, rows i-k and cols j-m A = 1 7 6 0 0 5 Example, 2 1 3 Picking out this submatrix >> B = A(2:3,1:2) B = Informationsteknologi 0 0 2 1 Institutionen för informationsteknologi | www.it.uu.se 14 Workspace and Array Editor n If you double-click on a variable in Workspace a new window is opened, Array Editor Double- click on a variable in the Workspace Informationsteknologi n Here you can change the variable value but also size.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    40 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us