Professional Documents
Culture Documents
What is MATLAB
History of MATLAB
Why MATLAB
MATLAB Applications
How to start MATLAB
M- File
MATLAB toolboxs
Keys to remember
MATrix LABoratory
Calculator
Interactive
Programming language
Scientific computation and visualization
tool
Symbolic computing tool
program
Easy to Learn
Vector formulation instead of scaler
Programmable calculator
Interpreter, Not Compiler - Easy to
debug
Powerful graphics
Add on toolboxes
Data
structures
require
minimal
attention;
E.g., arrays need NOT be declared
before first use;
An interactive interface allows rapid
experimentation and easy debugging
High-quality
graphics
and
visualization facilities are available;
MR Image formation
Fourier imaging ...
B0
MATLAB:
K
C
X
A
M(t)
z
RF
ky
transmit/receive
R + i*I;
ifft2(K); Z = fftshift(C);
real(Z); Y = imag(Z);
abs(Z); P = atan(Y ./ X);
kx
B1
x
=
=
=
=
Complexvalued
k-space
-1
T
2D FF
ky
kx
Pha = atan(Y/X)
Matrix calculation
Drawing graphs (2-D, 3-D)
Symbolic/Statistic Computation
Reading data from various sources
11
Define Matrix
Matrix Operation
>> determinant=det(A)
Polar plot
t=0:.01:2*pi;
polar(t,abs(sin(2*t).*cos(2*t)));
Line plot
x=0:0.05:5; y=sin(x.^2); plot(x,y);
Stem plot:
x = 0:0.1:4; y = sin(x.^2).*exp(-x);
stem(x,y)
13
Mesh plot:
z=peaks(25); mesh(z);
Surface plot:
z=peaks(25); surf(z);
Contour plot:
z=peaks(25); contour(z,16);
14
14
15
Differentiation:
>> g=sin(x)*cos(y);
>> diff(g)
% differentiate w.r.t. x
>> diff(g,y,3)
Integration:
>> g=(y^2-1)/(x^5+1); int(g,y) % integrate w.r.t. y
15
16
symbolic
computation
incorporated in MATLAB
16
http://www.maplesoft.com/
17
Directory
Window:
show the files
in the directory
Command
Window:
where you
input
MATLAB
commands
History
Window:
record what
you have
done
Matlab Window
Semicolon(;):
Typed at the end of command ,the output of the
command is not displayed(>> a=2,b=3;)
Typing(%): before line-Comment
Clc : clear the command window
Execute the calculation according to precedence
(1.parentheses 2. Exponential, 3.Multiplication,
Division 4.Addition & subtraction )
>> 27^(1/3)+32^0.2
+ addition
subtraction
* multiplication
^power
conjugate transpose
\ left division[5\3=3/5]
/ right division
x = A\b the solution of A x = b
x = b/A the solution of x A = b
Display format
41.4286
41.428571428571431
4.1429e+00
4.142857142857143e
+001
41.429
41.4285714285714
41.43
Display Formats
a=
Help window
Doc command
26
Core MATLAB
Toolboxes
26
To use
Programs => MATLAB => MATLAB 7.4
Inside MATLAB window, look for
MATLAB prompt >>
Type demo and hit Enter key
On left panel, click + on MATLAB
Click + on any one of the folders
Click on any one of the examples
27
Rounding Function
Functio Description
n
Round to the nearest
round( integer
x)
Fix(x)
Ceil(x)
Floor(x
)
Rem(x,
y)
Sign(x)
Round(17/5)
=3
Returns remainder
Fix(13/5)=2
Ceil(11/5)=3
Floor(-9/4)=3
Rem(13,5)=3
Returns 1 if x>0,-1if
Sign(5)=1
Scalar Variable
Example
>> x=15
x=
15
>> x=3*x-12
x=
33
>> a=12,b=4;c=(a-b)
+40
a=
12
c=
48
Variable names:
Can be upto 63 character long
Contain letters,digits and underscore
character
Must begin with letter
Matlab is case sensitive,different aa,aA,AA
Avoid using the name of built in function
for a variable(avoid cos,sin,sqrt)
Predefined variable
ans Variable has the value of last
expression ,not assigned specific variable
Pi
Number
Eps The smallest difference bet two numbers
Inf
Defined as
Same as I1
Na Not aNumber,0/0
N
Command Outcome
clear
35
Numbers
Integers (exact): 0, 1, 2,
Real numbers (approximate): 1., 2.1,
3e-8
Complex numbers: i = j = -1
Special values: pi
35
Arithmetic Operators
Unary: +, Binary: +, -, *, /, \, ^
Strings: Department of Electronics
36
Unary: ~
Binary (relational): ==, ~=, >, >=, <, <=,
&, |
Short-circuit: &&, ||
38
A=1; a =2*pi;
+ addition
subtraction
* multiplication
^power
conjugate transpose
\ left division
/ right division
x = A\b the solution of A x = b
x = b/A the solution of x A = b
>> -5/(3.45+2.75)^2
ans =
-0.1301
>> (2+6i)*(2-6i)
ans =
40
>> sin(pi/4)
ans =
0.7071
>> exp(acos(0.3))
ans=3.547
>> b= a* cos(.76);
A = [1 2 0; 2 5 -1; 4 10 -1]
;semicolon starts new row
B = [3,4,5]
;create row vector: , to
;separate elements
C = [A;B]
;insert new row in matrix
D = [A,B]
;insert new column in matrix
E = eye(5)
;create 5 x 5 identity
matrix
F = ones(2,3) ;create 2 x 3 matrix of ones
41
B = A
;transpose A
C = A + 3*B
D = A*B
D = A.*B
;element by element
multiplication
E = A^2
;multiply A by itself
E = A.^2
;raise each element of A to
2ndpower
X = inv(A)
;form inverse of matrix
42
Matlab Command
[1,2,3].*[5,6,7]
[10:-2:0]. ^ 2
2. ^ [1:6]
[2,4,6]./ 2
[2,4,6] . \ 2
Output
[5,12,21]
[100,64,36,16,4,0]
[2,4,8,16,32,64]
[1,2,3]
[1.000 0.5000 0.3333]
Function
Collections of commands
use define function
Executed in sequence when called Has input -output
Saved with extension .m
File
New M file
x = linspace(0,2*pi,200);
Y1= sin(x);
Y2 = cos(x);
Plot((x,Y1,x,Y2)
Save as filename.m
Run
Function
Output
functiony=react_C(c,f)
%react_C
calculates
the
reactance
of
a
capacitor.
%The inputs are: capacitor value and frequency
in hz
%The output is 1/(wC) and angular frequency in
rad/s
y(1)=2*pi*f;w=y(1);y(2)=1/(w*c);
File must be saved to a known path with
filename the same as the function name
andwithanextension.m(i.e react_c.m)
Callfunctionbyitsnameandarguments
Communications Toolbox
Control System Toolbox
Database Toolbox
Model-Based Calibration Toolbox
Neural Network Toolbox
Optimization Toolbox
Partial Differential Equation (PDE) Toolbox
Signal Processing Toolbox
Statistics Toolbox
Symbolic Math Toolbox
System Identification Toolbox
Wavelet Toolbox
; (Semicolon)
Up-arrow
Esc
Ctrl+C
Clear all
who
save filename
save filename x
yz
Thank You
det(A)
rank(A)
inv(A)
trace(A)
eig(A)
determinant
inverse
eigenvalues
52
Accessing: a(2, 1)
Assigning : a(2, 1) = 0
53
Ranges of numbers:
1:10
0:0.3:2
[1 2 3 4 5 6 7 8 9 10]
[0 0.3 0.6 0.9 1.2 1.5 1.8]
Dimensional range:
a(2, 1:2)
a(2:3, 1)
a(2, : )
a(:, 1)
a(:, : ) == a
[4 5]
[4; 7]
[4 5 6] row vector
[1; 4; 7] column vector
54
1
11
2
12
4x4 array
4
u = [3; 1; 4];
v = [2 0 -1];
1x3 row vector
Inner product:
x=v*u
2
1x3 x 3x1 1x1
(also known as scalar product and dot product)
Outer product:
x=u*v
6 0 -3
2 0 -1
8 0 -4
3x1 x 1x3 3x3
Cross product:
x = cross(u, v)
[-1 11 -2]
57
58
Arguments specified
brackets and commas:
using
round
e.g. atan2(y, x)
e.g. a = [1 2 3; 4 5 6];
size(a)
[2 3]
mn = size(a)
[m n] = size(a)
59
60
User-defined Functions
function f = fact(n)
Definition line
% Compute a factorial value.
H1 line
% fact(n) returns the factorial of n,
Help text
% usually denoted by n!
% Put simply, fact(n) is prod(1:n) Comment
f = prod(1:n);
Function body
By convention, name the m-file fact.m
62
63
63
64
Weakness of MATLAB
64