Presentation is loading. Please wait.

Presentation is loading. Please wait.

Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.

Similar presentations


Presentation on theme: "Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB."— 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 documented MATLAB 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 Current Folder browser MATLAB Editor Workspace 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 In MATLAB, ‘a=5‘ means that the variable a is being assigned a value of 5 What is the power of variable assignment?

7 Working Directory Location where you want to save all your files Current working directory Current Folder browser Files in your 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 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 Row vector Column vector Matrix Size of arrays In MATLAB the size of an array is always defined as number of rows by number of columns

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 Variable name to which the array is assigned When entering array elements, they must be enclosed by square brackets

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]’; Don’t forget the square brackets! Semi-colons within the square brackets indicate a NEW row Apostrophe is matrix transpose

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

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 ) Start value of vector Ending value of vector Number of elements length( v ) – number of elements in a vector

15 Array Indexing: Vectors MATLAB Command: a( 3 )  output 3 rd element MATLAB Command: a(3 : 8)  output 3 rd to 8 th elements MATLAB Command: a(7 : end)  output 7 th to last element in a MATLAB Command: a([2, 9])  output 2 nd and 9 th 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]; Don’t forget the square brackets! Semi-colons within the square brackets indicate a NEW row Make sure each row has the same number of elements

18 Array Indexing: Matrices MATLAB Command: A( i, j ) Element from i th row, j th 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 1 st and 2 nd rows and 1 st and 2 nd 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 2 nd row of a matrix A

22 Special Values


Download ppt "Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB."

Similar presentations


Ads by Google