1 / 22

Math 252: Math Modeling

Math 252: Math Modeling. Eli Goldwyn. Introduction to MATLAB. MATLAB. Special-purpose computer designed for engineering and scientific calculations Short-form for Mat rix Lab oratory – optimized to perform matrix-based computations Goal

alvareza
Download Presentation

Math 252: Math Modeling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB

  2. MATLAB Special-purpose computer designed for engineering and scientific calculations Short-form for Matrix Laboratory – optimized to perform matrix-based computations Goal • Learn how to write clean, efficient and documentedMATLAB programs to solve math-bio problems

  3. Advantages and Disadvantages Advantages • Ease of use • Platform independence • Pre-defined functions • Plotting capabilities • Building graphical user interfaces (GUI) Disadvantages • Can be slower than high-level languages such as C++, Fortran, etc. • Cost

  4. Anatomy of the MATLAB Environment Workspace Browser MATLAB Editor Current Folder browser Details Window Command Window

  5. Using MATLAB as a calculator • Things to note with trig. operations: • Input must be enclosed in brackets • Inputs must be in radians

  6. Variable Assignment What is the power of variable assignment? In MATLAB, ‘a=5‘ means that the variable a is being assigned a value of 5

  7. Working Directory Location where you want to save all your files Current Folder browser Files in your working directory Current working directory Click to change the working directory

  8. Arrays Array Collection of data values organized into rows and columns and assigned a single name Types of arrays • Vectors: An array with one dimension • Matrices: An array with two or more dimensions Specifying the size of an array • Rows by columns

  9. Variable Variable Region of memory containing an array is known by a user-specified name Naming variables • Similar to naming scripts: Variables must start with a letter and contain only letters, numbers or underscores • Good practice to name it with something with meaning • Don’t name your variables sin, cos, etc. • Make sure characters are unique in the first 63 characters • MATLAB only reads the first 63 characters • Variables are case-sensitive

  10. Examples Matrix Size of arrays In MATLAB the size of an array is always defined as number of rows by number of columns Row vector Column vector

  11. Row Vectors There are a few ways to create row vectors a = [1 , 2 , 3 , 4 , 5]; OR a = [1 2 3 4 5]; What is the output of the command size(a)? You can choose to separate the elements of your vector with a space or a comma When entering array elements, they must be enclosed by square brackets Variable name to which the array is assigned

  12. Column vectors Column vectors are very similar to row, except that each element is in a different row a = [1 ; 2 ; 3 ; 4 ; 5]; a=[1 2 3 4 5]’; Semi-colons within the square brackets indicate a NEW row Don’t forget the square brackets! Apostrophe is matrix transpose

  13. Size of an Array Input Output Size function outputs the size of the array a. Don’t forget the parenthesis since size is a built-in function in MATLAB! Number of columns Number of rows

  14. Creating Row Vectors with Shortcut Expressions If Increment is not specified, MATLAB default is 1 Built in function to generate a row vector y v = Initial Value : Increment : Final Value y = linspace( a , b , n ) length( v ) – number of elements in a vector Start value of vector Number of elements Ending value of vector

  15. Array Indexing: Vectors MATLAB Command: a( 3 )  output 3rd element MATLAB Command: a(3 : 8)  output 3rd to 8th elements MATLAB Command: a(7 : end)  output 7th to last element in a MATLAB Command: a([2 , 9])  output 2nd and 9th element in a

  16. Go to scripts.pptx • Go to plotting.pptx

  17. Creating Matrices Remember that semi-colon within square brackets means go to a new row b = [ -2 3 5 ; 7 23 -9 ; 2 -5 9]; Semi-colons within the square brackets indicate a NEW row Make sure each row has the same number of elements Don’t forget the square brackets!

  18. Array Indexing: Matrices MATLAB Command: A( i , j ) Element from ith row, jth column MATLAB Command: A( m : n , k : l ) Elements from rows m to n and columns k to l MATLAB Command: A( : , 2 : 4 ) All elements from columns 2 to 4 MATLAB Command: A( [ 1 3] , : ) All elements from rows 1 and 3 Colon by itself means all elements

  19. Assigning a Scalar to an Array A( 1 : 2 , 1 : 2 ) = 1 Assigns elements in the 1st and 2nd rows and 1st and 2nd columns of A the value of 1

  20. Common Matrix Functionality

  21. Simple Matrix Manipulation What do the following commands do? • B = [ A ; u]; • D = [ A v ]; A( 2 , : ) = [ ]  deletes the 2nd row of a matrix A

  22. Special Values

More Related