Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

15
Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis

Transcript of Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

Page 1: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

Introduction to MATLAB

CBE 502 Mathematical Methods of Engineering Analysis

Page 2: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

Where to get MATLAB

• Princeton Software : Office of Information Technology (OIT)

• http://www.princeton.edu/software/licenses/software/matlab/

Page 3: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

What is MATLAB

• High level language useful for technical computing with extensive library of mathematical and graphical subroutines

• Stands for MATrix LABoratory

Page 4: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

MATLAB Desktop

Page 5: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

MATLAB Introduction

• Creating Matrices• A=[1,2,3;4,5,6;7,8,9];• Will give a 3 X 3 matrix• A= 1 2 3

4 5 6 7 8 9• Very easy to access/modify each element A(i,j)

Page 6: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

MATLAB Introduction

• zeros(m, n): matrix with all zeros• ones(m, n): matrix with all ones. • eye(m, n): the identity matrix • rand(m, n): uniformly distributed

random• randn(m, n): normally distributed

random• magic(m): square matrix whose

elements have the same sum, along the row, column and diagonal.

Page 7: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

MATLAB Introduction

• Colon Notation ‘:’–Generating Vector • Step Size

–Access to part of Matrix• ‘end’ Notation

Page 8: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

MATLAB Introduction

• Operators – ^: exponentiation – *: multiplication – /: division – \: left division. The operation A\B is effectively

the same as INV(A)*B, although left division is calculated differently and is much quicker.

– +: addition – -: subtraction

• Pay attention to size of the arrays

Page 9: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

Relational/Logical Operator

• == Equal to• ~= Not equal to• < Strictly smaller• > Strictly greater• <= Smaller than or equal to• >= Greater than equal to• & And operator• | Or operator

Page 10: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

MATLAB Introduction

• Element by Element .^ , .* , ./

– Different from Matrix Operation• Flow Controls – If, else, for , while , break..

• M File – text file containing program to run

Page 11: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

Built In Functions• mean(A):mean value of a vector• max(A), min (A): maximum and minimum. • sum(A): summation.• sort(A): sorted vector• median(A): median value• std(A): standard deviation. • det(A) : determinant of a square matrix • dot(a,b): dot product of two vectors• Cross(a,b): cross product of two vectors• Inv(A): Inverse of a matrix A

Page 12: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

Some Useful/Interesting MATLAB Commands

• who List known variables• whos List known variables plus their size• help >> help sqrt Help on using sqrt• lookfor >> lookfor sqrt Search keyword sqrt in m-files• what >> what a: List MATLAB files in a:• clear Clear all variables from work space• clear x y Clear variables x and y from work space• clc Clear the command window• any (x) returns 1 if any element of x is nonzero• all (x) returns 1 if all elements of x are nonzero• isnan (x) returns 1 at each NaN in x• isinf (x) returns 1 at each infinity in x• finite (x) returns 1 at each finite value in x

Page 13: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

2D Plots

• Important Functions– plot , hold , title , xlabel , ylabel , legend– Sub plots , saveas ,

Page 14: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

Examples

• MATLAB.. Here we come !!

Page 15: Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.

• Keep on exploring MATLAB .. • May the Coding force be with you

• Thanks..