Вы находитесь на странице: 1из 40

MATLAB Language of

Technical Computing and


Visualization

Presented by
Dr. M. Nuruzzaman
Electrical Engineering Department, KFUPM
Office: 59-0077, Email: nzaman@kfupm.edu.sa, Ph: 2830
Under the auspice of ETS sem 152

Overview
What is MATLAB?

What can be conducted in MATLAB?


What are MATLAB Toolboxes?
Where to start in MATLAB?
prompt - history - mfile
Prep. by: Dr. M. Nuruzzaman

Matrix entering row, column, and rectangular


Enter row matrix [2 3 4 -2 0] into MATLAB
>>R=[2 3 4 -2 0]

7
Enter 8
10
11

>>C=[7 8 10 -11]' or
>>C=[7;8;10;-11]

6
7
20
Enter 5 12 3
1 1
0
19
3
2

Space gap
between
elements,
semicolon
between rows,
and housed in
third bracket

A=[20 6 7;5 12 -3;1 -1 0;19 3 2]

Vector generation row and column by colon operator


Syntax - start : increment: last value
A=1:4

R=1:3:10

C=[0:2: 10]'

linspace (first element, last element, number of points from


first to last)
Prep. by: Dr. M. Nuruzzaman

Functional codes

Prep. by: Dr. M. Nuruzzaman

Prep. by: Dr. M. Nuruzzaman

Prep. by: Dr. M. Nuruzzaman

Logical & Comparative Operators:

Table D.4 Basic logical operations on matrix elements

for NOT(A) operation,


>>A=[0 0;0 1];
>>~A
ans =
1 1
1 0

for A OR B,
>>A=[1 1;0 1];
>>B=[0 1;1 1];
>>A|B

for A AND B,
>>A&B
ans =
0 1
0 1

for A XOR B,
>>xor(A,B)
ans =
1 0
1 0

ans =
1
1

1
1

Prep. by: Dr. M. Nuruzzaman

Matrix of ones, zeroes, and constants


>>O=ones(3,1)

>>z=zeros(3,1)
Syntax: ones(row
number,column number)

>>C=3*ones(3,2)
Matrix arithmetic
A=[0 2 3]

B=[-1 4 5]
A/5
1./A
3./A

Matrix inverse
2

0
0

1 5

2 1
0 2

12 14

1
2
0
0
0

89

14
1
2

>>A=sym([2 1 5;0 2 1;0 0 2]);


>>B=inv(A)

A+5
A.^2
A./B

A^2

x
x y

y
1

y 2 yx x

x y
y 2 yx x

y yx x
x

2
y yx x
2

>>syms x y
>>A=[x y;x+y 1]; B=inv(A); pretty(B)

1/A
Prep. by: Dr. M. Nuruzzaman

Indexing and coloning of arrays

Array indexing starts from 1 rather from 0

A=[2 4 3 -10 0 9 73 29 -31 50];


B=A([2 3 9])

8 64
64 216
1 47
3 87

C=A(3:8)

27 56
729 40
45 72
82
29

E=A([4 1])
Syntax: Array(required row,required column)

A=[8 64 27 56;-64 216 729 40;1 47 45 72;3 87 82 29];


Column selection
A(:,4)
Row selection
A(3,:)

Submatrix selection
A([3 4],[1 2])
Consecutive element selection
A(2:4,2:4)

Changing one element


A(2,3)=0

Column delete
A(:,3)=[ ]
Two row multiplication
A(:,1)=A(:,1).*A(:,2)/4
Particular colm division
A(:,1)=A(:,1)/2
Prep. by: Dr. M. Nuruzzaman

Appending rows

1 3 5
2 6 8

[9 5

0]

[4

7 8]

B=[1 3 5;2 6 8]

B=[B;[9 5 0]]

A=[B;[4 7 8]]

B=

B=

A=

1
2

Appending
1
2

3
6

5
8

columns
3 5
6 8

5 0

1
2
9

3
6
5

5
8
0

1
2
9
4

3
6
5
7

D=[D [9 0 1]']

C=[D [3 1 9]']

D=

D=

C=

3
6
5

5
8
0

1
2
9

3
6
5

5
8
0

5
8

0
8

5
8
0
8

D=[1 3 5;2 6 8;9 5 0]

1
2
9

3
6
5
7

1 3 5 9 3
2 6 8 0 1

9
5
0
1
9

3
1
9

9
0
1

1
2

9
4

9
0
1

1
2
9

3
6
5

5
8
0

9
0
1

3
1
9

Prep. by: Dr. M. Nuruzzaman

Data accumulation by using the two appending techniques


f=[ ]; k=2;

f=[f k]

f=[f;k]

For-loop syntax
for counter = starting value:increment or decrement of the counter value : final value
end

Executable MATLAB command(s)

for k=10:10:70
y(k/10)=cosd(k);
end
for the right shifting,
>>f=[ ]; for k=1:3 f=[f k^2]; end
>>f
f=
1
4
9

minimum/maximum/sum
/product/sort of data
max
min
sum
sort
prod

Or, as a one line:


for k=10:10:70, y(k/10)=cosd(k); end
for the left shifting,
>>f=[ ]; for k=1:3 f=[k^2 f]; end
>>f
f=
9
4
1

Row or column matrix


one function
Rectangular matrix two
functions

>>B=[1 3 7 5 -8]
>>max(B)
>>B=[1 3 5;2 6 8]
>>max(max(B))

Index returning
>>B=[1 3 7 5 -8]
>>[M,I]=max(B)
Prep. by: Dr. M. Nuruzzaman

Scalar code for functional computing


Compute

y (12x4 4 x 1)(9x3 5)

x=[0 1 2]
y=(12*x.^4-4*x+1).*(9*x.^3-5)
Compute

y (12x4 4 x 1)(9x3 5)

1 x 2

x 0.5

x=-1:0.5:2;
y=(12*x.^4-4*x+1).*(9*x.^3-5)
Compute

f ( x, y) (12x4 xy 1)(9 y3 5)
for x=[0 1 2]

and y=[3 -2 1]

x=[0 1 2]; y=[3 -2 1]


f=(12*x.^4-x.*y+1).*(9*y.^3-5)

Prep. by: Dr. M. Nuruzzaman

Rectangular Matrix based computing for 2D function

f ( x, y) (12x4 xy 1)(9 y3 5)

1 x 2

x 1

4 y 8

y 2

[x,y]=meshgrid(-1:1:2,4:2:8)

y=

x=

-1
-1
-1

0
0
0

1
1
1

4
6
8

2
2
2

4
6
8

4
6
8

4
6
8

f=(12*x.^4-x.*y+1).*(9*y.^3-5)

f=
9707
36841
96663

571
1939
4603

5139
105635
13573
350959
23015
814731
Prep. by: Dr. M. Nuruzzaman

Symbolic differentiation
d
dx

d
dx

(sin x) cos x

(cot1 x)

1
1 x2

d
[(12x 4 4x 1)(9x3 5)]
dx

d3
4
3
[(
12
x

4
x

1
)(
9
x
5)]
3
dx

>>syms x
>>y=diff(sin(x))

use pretty for math


readable form,
expand for
expansion

>>syms x
>>y=diff(acot(x))
>>syms x
>>y=diff((12*x^4-4*x+1)*(9*x^3-5))

>>syms x
>>y=(12*x^4-4*x+1)*(9*x^3-5)

>>yn=diff(y,3)

Prep. by: Dr. M. Nuruzzaman

Indefinite symbolic integration

sin xdx cos x


1
dx

x(x2 1)2

>>syms x
>>y=int(sin(x))

>>syms x
>>y=int(1/x/(x^2+1)^2)

Definite symbolic
integration

02 sin xdx
x3

1
dx

2
2
x2 x( x 1)
(ln x) 4
dx

x
x 3
5

use pretty for math


readable form

(ln x)4
dx

x
>>syms x

>>syms x

>>y=int(log(x)^4/x)

>>y=int(sin(x),0,pi/2)
>>syms x
>>y=int(1/x/(x^2+1)^2,2,3)
>>syms x
>>y=int(log(x)^4/x,3,5)
Prep. by: Dr. M. Nuruzzaman

Simple if

x=2;
if x>=1
y=sin(x);
end

if logical expression
end

Executable MATLAB command(s)

If-else

x=1;
if x==1
y=sin(x*pi/2);
else
y=cos(x*pi/2);
end

if logical expression

Executable MATLAB command(s)


else

Executable MATLAB command(s)

end

Nested-if
if logical expression

Executable MATLAB command(s)


elseif logical expression
Executable MATLAB command(s)
elseif logical expression

Executable MATLAB command(s)

else
end

Executable MATLAB command(s)

elseif
elseif
elseif
elseif

N=77;
if N>=90
g='A';
(N<90)&(N>=80)
g='B';
(N<80)&(N>=70)
g='C';
(N<70)&(N>=60)
g='D';
(N<60)&(N>=50)
g='E';
else
g='F';
end
Prep. by: Dr. M. Nuruzzaman

User input during the run time of an M-file

>>A=input('Enter any integer from 1 to 10: ');


>>Enter any integer from 1 to 10: 5
While-end syntax
General syntax:
while logical expression

Executable command(s)

end
where while and end are the reserve words

Switch-case-otherwise syntax

>>A=input('Enter your name: ','s');


>>Enter your name: mohammad
Example: A positive integer
greater than 1 will be asked
from the user. The sum of the
squares from 1 to that integer
is required to compute.
Executable M-file:
I=input('Enter integer > 1: ');
k=0;
s=0;
while ~(k>I)
s=s+k^2;
k=k+1;
end

Prep. by: Dr. M. Nuruzzaman

Prep. by: Dr. M. Nuruzzaman

Algebraic equation solving

x y 8
9 x 8 y 5

x 2 y 2 z 2 8
2 y x 4

solve(eqn 1, eqn 2, eqn 3, so on,var 1,var 2, so on)

>>e1='x-y=-8';
>>e2='9*x+8*y=5';
>>s=solve(e1,e2)
>>[s.x s.y]

>>e1='x^2-y^2-z^2=-8';
>>e2='2*y+x=4';
>>e3='z-x=2';
>>s=solve(e1,e2,e3)
>>[s.x s.y s.z]

double for decimal

pretty for math readable


form

For structure array

3 cos x 2 sin x 2

array.member for any


element
>>e='3*cos(x)+2*sin(x)=2';
>>s=solve(e)

Prep. by: Dr. M. Nuruzzaman

Fourier transform forward


fourier(function in string form, independent variable, transform variable)

F ( )
t

jt
e
f (t) dt

Prep. by: Dr. M. Nuruzzaman

Fourier transform inverse


ifourier(function in string form, independent variable, transform variable)

f (t )

1
2

it
F
(

)
e
dt

Prep. by: Dr. M. Nuruzzaman

Laplace transform forward


laplace(function in string form, independent variable, transform variable)

F (s)
t

st
f
(
t
)
e
dt

t 0

Prep. by: Dr. M. Nuruzzaman

Laplace transform inverse


ilaplace(function in string form, independent variable, transform variable)

f (t )

1 s c ist
e F (s) ds
2 i s c i

Prep. by: Dr. M. Nuruzzaman

Z transform forward

F (z )

n
f [n] z

n0

ztrans(function in string form, independent variable, transform variable)

Z transform inverse

f [n]

F (z) z

2 i C

n 1

dz

Prep. by: Dr. M. Nuruzzaman

Z transform inverse
iztrans(function in
transform variable)

string

form,

independent

variable,

Prep. by: Dr. M. Nuruzzaman

11

For loop conversion

2
(x 2x)

x 2

For loop computation:


S=0;
for x=-2:11
S=S+x^2+2*x;
end

Vector form computation:


x=-2:11;
S=sum(x.^2+2*x);

partial fraction symbolic and linear decimal


residue(Numerator coefficient,Denominator coefficient)
maple('convert',f,'parfrac',x)

1
x2
1

3 (x 1) 3 (x 2 x 1)
x3 1
>>syms x
>>f=1/(x^3+1);
>>R=maple('convert',f,'parfrac',x);
>>pretty(R)

1
0.3333 0.1667 j 0.2887 0.1667 j 0.2887

x3 1
x 1
x 0.5 j 0.866
x 0.5 j 0.866
>>syms x
>>N=1;
>>D=sym2poly(x^3+1);
>>[R P K]=residue(N,D)
R=
-0.1667 - 0.2887i
-0.1667 + 0.2887i
0.3333
P=
0.5000 + 0.8660i
0.5000 - 0.8660i
-1.0000
K=
[]
Prep. by: Dr. M. Nuruzzaman

Polynomial roots

x3 1 0

Polynomial multiplication

( x3 x 1)( x2 3)

roots(polynomial as a row matrix)


syms x
>> r=roots([1 0 0 1])
y=(x^3-x+1)*(x^2-3)
r=
pretty(expand(y))
-1.0000
0.5000 + 0.8660i
Algebraic substitution
0.5000 - 0.8660i
maple('algsubs', variable to be eliminated as
Numerator denominator
an equation but left side of the equation must
separation
be the variable itself, expression from which
1
5
the variable is to be eliminated)

Vo
V1 Vo
x2 4 x 7
?
Vo AV1
(Vi V1 ) sC
Vi
R
syms x
syms s R C V1 Vo Vi
sRCA

y=1/(x^2+4)+5/(x-7);
RCs A 1
e1=(Vi-V1)*s*C-(V1-Vo)/R;
[N,D]=numden(y)
e2='V1=-Vo/A';
pretty(N)
O1=maple('algsubs',e2,e1)
pretty(D)
O2=solve(O1,Vo)
Vi=1;
Use expand if
subs(O2)
necessary
Prep. by: Dr. M. Nuruzzaman

Creating function file:

f ( x) x 2 x 8

File starts with function

f ( x1 , x2 , x3 ) x1 2 x1 x2 x3
2

= is a must for return


Input argument separated
by comma
Output argument
separated by comma
Single input single
output function file

Input argument under first


brace
Output argument under
third brace

p1 x1 2 x1 x2 x3
2

Multiple inputs single


output function file

p2 x1 x2 x3

Function file for three input and two output


arguments

Prep. by: Dr. M. Nuruzzaman

ODE without boundary condition

first derivative Dy
Second derivative D2y
Third derivative D3y

dsolve(ODE code,indpendent variable)

dy y 2
2x

4y
dx x

2x2
y
Cx

d2y
dy

2
15y 0
dt
dt 2

y(t)

S=dsolve('2*x*Dy=y^2/x+4*y','x');

pretty(S)

C1e5t C2e3t

S=dsolve('D2y-2*Dy-15*y=0');
pretty(S)

y y 2y 0
'''

'

y(x) C1e
x

7
x
7
x

e C2 cos
C3 sin

2
2

x
2

>>S=dsolve('D3y+Dy-2*y=0','x');

pretty(S)

Prep. by: Dr. M. Nuruzzaman

ODE with boundary conditions


dsolve(ODE code, initial conditions separated by comma but as a
single string under quote, independent variable)

System of ODE with boundary conditions


dsolve(ODE 1 code, ODE 2 code, and so on, initial conditions separated by
comma but as a single string under quote, independent variable)

Prep. by: Dr. M. Nuruzzaman

dy1

5
y

2
y

1
2

dt
dy

2 y1 2 y2
dt

y1(0) 1

y (0) 2

y1
y2

3 t 8 6t
e e
5
5
6 et 4 e 6t
5
5

Prep. by: Dr. M. Nuruzzaman

y=f(x) versus x graph


From expression

From data

ezplot(function,interval
as a row matrix)

plot(x data,y data)

y 2x 2 3x 5
Tabular data

3 x 3
y='2*x^2-3*x+5';

Command to plot y vs x:

-6

-4

-3

-5

>>x=[-6 -4 0 4 5 7];
>>y=[9 3 -3 -5 2 0];
>>plot(x,y)

ezplot(y,[-3,3])
Multiple y

Multiple y
hold

ezplot(x+4',[-3,3])
ezplot(sin(x)',[-3,3])

-6

-4

y1

-3

-5

y2

-2

7.7

y3

-1

-3

plot(x,y1,x,y2,x,y3)

Prep. by: Dr. M. Nuruzzaman

Surface plot
from expression
ezsurf(function code,[x min x max y min ymax])
default

2 x, y 2
2
2
f ( x, y) 8(x y )

ezsurf('-8*(x^2+y^2)')
Parametric equation
x 2 cost
y sint

2 x 2
0 t 2

1
y

2.5 x 5.1

0 y4

f ( x, y) x 2 xy y 2

x='2*cos(t)'; y='sin(t)';
ezplot(x,y,[0,2*pi])
Implicit function

Surface plot
from data
Use surf or mesh
surf(functional data as a
retangular matrix,x
variation as a row matrix, y
variation as a row matrix)
Needs computing or
sample generation

3 y 2

ln y ln(x y 1) 1 y 2
E='1/y-log(y)+log(-1+y+x)+y^2-1';
D=[-2.5 5.1 -3 2];
ezplot(E,D)

x=-2:0.2:2;
y=0:0.2:4;
[X,Y]=meshgrid(x,y);
f=X.^2+X.*Y+Y.^2;
surf(x,y,f)

Prep. by: Dr. M. Nuruzzaman

Contour plot for

f ( x, y)
Syntax: ezcontour(code of f(x,y )under quote, x interval bounds as two
element row matrix, y interval bounds as two element row matrix)
1 2 2

f ( x, y) x y e3 x 4 y
2

1.2 x 1.2

1 y 1

Subplot for window splitting


Treat graphs as matrix elements
Syntax: subplot(m-row directed graph number, n-column directed graph
number, particular number from mxn)
All are integers
Suppose m=2, n=2, particular number is 1, 2, 3, 4 consecutively
rowwise
Discrete plot by stem, bar, bar3,

Prep. by: Dr. M. Nuruzzaman

Random numbers
Decimal random numbers:
unifrnd(lower bound,upper bound,row number,column number)
Generation of a single random
number X between 4 and 5,
X=unifrnd(-4,5)

Generation of a rectangular matrix X of order 23 in


which every element is inbetween -4 and 5,
X=unifrnd(-4,5,2,3)

Integer random numbers:


unifrnd(row number,column number, [lower bound upper bound])
Generation of a single random integer X between -3 and 5,

X=randint(1,1,[-3 5])
Generation of a rectangular matrix X of order 33 in which each element is in
between -3 and 5,

X=randint(3,3,[-3,5])
Data statistics
datastats(y)

Excel file reading


f=xlsread(book1.xls')

Prep. by: Dr. M. Nuruzzaman

Prep. by: Dr. M. Nuruzzaman

Prep. by: Dr. M. Nuruzzaman

Prep. by: Dr. M. Nuruzzaman

Prep. by: Dr. M. Nuruzzaman

Вам также может понравиться