Post on 12-Jan-2016
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/
What is MATLAB
• High level language useful for technical computing with extensive library of mathematical and graphical subroutines
• Stands for MATrix LABoratory
MATLAB Desktop
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)
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.
MATLAB Introduction
• Colon Notation ‘:’–Generating Vector • Step Size
–Access to part of Matrix• ‘end’ Notation
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
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
MATLAB Introduction
• Element by Element .^ , .* , ./
– Different from Matrix Operation• Flow Controls – If, else, for , while , break..
• M File – text file containing program to run
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
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
2D Plots
• Important Functions– plot , hold , title , xlabel , ylabel , legend– Sub plots , saveas ,
Examples
• MATLAB.. Here we come !!
• Keep on exploring MATLAB .. • May the Coding force be with you
• Thanks..