Date post: | 09-Mar-2016 |
Category: |
Documents |
Upload: | badran-al-ibrahim |
View: | 225 times |
Download: | 0 times |
of 40
MATLAB Language of Technical Computing and
Visualization
Presented by
Dr. M. Nuruzzaman
Electrical Engineering Department, KFUPM
Office: 59-0077, Email: [email protected], 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]
111087
Enter
231901131257620
Enter
Space gap
between
elements,
semicolon
between rows,
and housed in
third bracket
>>C=[7 8 10 -11]' or
>>C=[7;8;10;-11]
A=[20 6 7;5 12 -3;1 -1 0;19 3 2]
Prep. by: Dr. M. Nuruzzaman
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)
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
ans =
1 1
1 1
for A AND B,
>>A&B
ans =
0 1
0 1
for A XOR B,
>>xor(A,B)
ans =
1 0
1 0
Prep. by: Dr. M. Nuruzzaman
Matrix of ones, zeroes, and constants
>>O=ones(3,1) >>z=zeros(3,1)
>>C=3*ones(3,2)
Syntax: ones(row
number,column number)
Prep. by: Dr. M. Nuruzzaman
A=[0 2 3]
B=[-1 4 5]
A/5
1./A
3./A
A+5
A.^2
A./B
A^2
1/A
Matrix arithmetic
200
120
512
21
41
21
89
41
21
00
0
>>A=sym([2 1 5;0 2 1;0 0 2]);
>>B=inv(A)
1yx
yx
xyxy
x
xyxy
yx
xyxy
y
xyxy
22
22
1
>>syms x y
>>A=[x y;x+y 1]; B=inv(A); pretty(B)
Matrix inverse
Prep. by: Dr. M. Nuruzzaman
A=[2 4 3 -10 0 9 73 29 -31 50];
Indexing and coloning of arrays
B=A([2 3 9]) C=A(3:8)
E=A([4 1])
2982873724547140729216645627648
A=[8 64 27 56;-64 216 729 40;1 47 45 72;3 87 82 29];
Array indexing starts from 1 rather from 0
Changing one element
A(2,3)=0
Column delete
A(:,3)=[ ]
Syntax: Array(required row,required column)
Particular colm division
A(:,1)=A(:,1)/2
Two row multiplication
A(:,1)=A(:,1).*A(:,2)/4
Column selection
A(:,4)
Row selection
A(3,:)
Submatrix selection
A([3 4],[1 2])
Consecutive element selection
A(2:4,2:4)
Appending rows
874
059
862
531
862
531[9 5 0] [4 7 8]
B=[1 3 5;2 6 8]
B =
1 3 5
2 6 8
B=[B;[9 5 0]]
B =
1 3 5
2 6 8
9 5 0
A=[B;[4 7 8]]
A =
1 3 5
2 6 8
9 5 0
4 7 8
Prep. by: Dr. M. Nuruzzaman
059
862
531
913
109
Appending columns
91059
10862
39531
D=[1 3 5;2 6 8;9 5 0]
D =
1 3 5
2 6 8
9 5 0
D=[D [9 0 1]']
D =
1 3 5 9
2 6 8 0
9 5 0 1
C=[D [3 1 9]']
C =
1 3 5 9 3
2 6 8 0 1
9 5 0 1 9
for counter = starting value:increment or decrement of the counter value : final value
Executable MATLAB command(s)
end
For-loop syntax
for k=10:10:70
y(k/10)=cosd(k);
end
Or, as a one line: for k=10:10:70, y(k/10)=cosd(k); end
Data accumulation by using the two appending techniques
f=[ ]; k=2; f=[f;k] f=[f k]
Prep. by: Dr. M. Nuruzzaman
for the right shifting,
>>f=[ ]; for k=1:3 f=[f k^2]; end
>>f
f =
1 4 9
for the left shifting,
>>f=[ ]; for k=1:3 f=[k^2 f]; end
>>f
f =
9 4 1
minimum/maximum/sum
/product/sort of data
max
min
sum
sort
prod
Row or column matrix
one function
Rectangular matrix two
functions >>B=[1 3 5;2 6 8]
>>max(max(B))
>>B=[1 3 7 5 -8]
>>max(B)
Index returning
>>B=[1 3 7 5 -8]
>>[M,I]=max(B)
Prep. by: Dr. M. Nuruzzaman
Scalar code for functional computing
)59)(1412( 34 xxxy
)59)(1412( 34 xxxy
Compute
for x=[0 1 2] and y=[3 -2 1]
x=[0 1 2]
y=(12*x.^4-4*x+1).*(9*x.^3-5)
21 x 5.0xCompute
x=-1:0.5:2;
y=(12*x.^4-4*x+1).*(9*x.^3-5)
)59)(112(),( 34 yxyxyxfCompute
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
)59)(112(),( 34 yxyxyxf 21 x 84 y
1x 2y
[x,y]=meshgrid(-1:1:2,4:2:8)
x =
-1 0 1 2
-1 0 1 2
-1 0 1 2
y =
4 4 4 4
6 6 6 6
8 8 8 8
f=(12*x.^4-x.*y+1).*(9*y.^3-5)
f =
9707 571 5139 105635
36841 1939 13573 350959
96663 4603 23015 814731
Symbolic differentiation
xxdxd cos)(sin
2
1
1
1)(cot
xx
dxd
)]59)(1412[( 34 xxxdx
d
>>syms x
>>y=diff(sin(x))
>>syms x
>>y=diff(acot(x))
>>syms x
>>y=diff((12*x^4-4*x+1)*(9*x^3-5))
Prep. by: Dr. M. Nuruzzaman
)]59)(1412[( 343
3
xxxdx
d>>syms x
>>y=(12*x^4-4*x+1)*(9*x^3-5)
>>yn=diff(y,3)
use pretty for math
readable form,
expand for
expansion
Indefinite symbolic integration
xxdx cossin
dxxx
22 )1(
1
dxx
x
4)(ln
>>syms x
>>y=int(sin(x))
>>syms x
>>y=int(1/x/(x^2+1)^2)
>>syms x
>>y=int(log(x)^4/x)
Prep. by: Dr. M. Nuruzzaman
Definite symbolic
integration
xdxsin20
3
222 )1(
1x
x
dxxx
>>syms x
>>y=int(sin(x),0,pi/2)
>>syms x
>>y=int(1/x/(x^2+1)^2,2,3)
dxx
x
x
5
3
4)(ln >>syms x
>>y=int(log(x)^4/x,3,5)
use pretty for math
readable form
Simple if
if logical expression
Executable MATLAB command(s)
end
x=2;
if x>=1
y=sin(x);
end
If-else
if logical expression
Executable MATLAB command(s)
else
Executable MATLAB command(s)
end
x=1;
if x==1
y=sin(x*pi/2);
else
y=cos(x*pi/2);
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 Executable MATLAB command(s)
end
N=77;
if N>=90
g='A';
elseif (N=80)
g='B';
elseif (N=70)
g='C';
elseif (N=60)
g='D';
elseif (N=50)
g='E';
else
g='F';
end
Prep. by: Dr. M. Nuruzzaman
Prep. by: Dr. M. Nuruzzaman
Switch-case-otherwise syntax
While-end syntax
General syntax:
while logical expression
Executable command(s)
end
where while and end are the reserve words
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
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
>>A=input('Enter your name: ','s');
>>Enter your name: mohammad
Prep. by: Dr. M. Nuruzzaman
Prep. by: Dr. M. Nuruzzaman
5898
yxyx
Algebraic equation solving 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]
242
8222
xzxy
zyx >>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]
2sin2cos3 xx
>>e='3*cos(x)+2*sin(x)=2';
>>s=solve(e)
double for decimal
pretty for math readable
form
For structure array
array.member for any
element
Prep. by: Dr. M. Nuruzzaman
Fourier transform forward
fourier(function in string form, independent variable, transform variable)
t
t
tj dttfe )(
)(F
Prep. by: Dr. M. Nuruzzaman
Fourier transform inverse
ifourier(function in string form, independent variable, transform variable)
dteF ti)(2
1
)(tf
Laplace transform forward
laplace(function in string form, independent variable, transform variable)
Prep. by: Dr. M. Nuruzzaman
dtetft
t
st
0
)(
)(sF
Prep. by: Dr. M. Nuruzzaman
Laplace transform inverse
ilaplace(function in string form, independent variable, transform variable)
ics
ics
st dssFei
)(2
1
)(tf
Prep. by: Dr. M. Nuruzzaman
Z transform forward
ztrans(function in string form, independent variable, transform variable)
Z transform inverse
n
n
nznf0
][)(zF
C
n dzzzFi
1)(2
1
][nf
Z transform inverse
iztrans(function in string form, independent variable,
transform variable)
Prep. by: Dr. M. Nuruzzaman
Prep. by: Dr. M. Nuruzzaman
)2( 211
2
xxx
For loop conversion
For loop computation: Vector form computation:
S=0; x=-2:11;
for x=-2:11 S=sum(x.^2+2*x);
S=S+x^2+2*x;
end
partial fraction symbolic and linear decimal
)1(3
2
)1(3
12
xx
x
x
1
13x
>>syms x
>>f=1/(x^3+1);
>>R=maple('convert',f,'parfrac',x);
>>pretty(R)
maple('convert',f,'parfrac',x)
866.05.0
2887.01667.0
866.05.0
2887.01667.0
1
3333.0
jx
j
jx
j
x
1
13x
>>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 =
[ ]
residue(Numerator coefficient,Denominator coefficient)
Prep. by: Dr. M. Nuruzzaman
Polynomial roots
013 x
roots(polynomial as a row matrix)
>> r=roots([1 0 0 1])
r =
-1.0000
0.5000 + 0.8660i
0.5000 - 0.8660i
Polynomial multiplication
)3)(1( 23 xxx
syms x
y=(x^3-x+1)*(x^2-3)
pretty(expand(y))
Numerator denominator
separation
7
5
4
12
xx
syms x
y=1/(x^2+4)+5/(x-7);
[N,D]=numden(y)
pretty(N)
pretty(D)
Use expand if
necessary
Algebraic substitution
R
VVsCVV oi
11)( 1AVVo ?
i
o
V
V
maple('algsubs', variable to be eliminated as
an equation but left side of the equation must
be the variable itself, expression from which
the variable is to be eliminated)
syms s R C V1 Vo Vi
e1=(Vi-V1)*s*C-(V1-Vo)/R;
e2='V1=-Vo/A';
O1=maple('algsubs',e2,e1)
O2=solve(O1,Vo)
Vi=1;
subs(O2)
1
ARCs
sRCA
Prep. by: Dr. M. Nuruzzaman
Single input single output function file
Multiple inputs single output function file
Function file for three input and two output
arguments
),,( 321 xxxf2
321
2
1 2 xxxx 8)(2 xxxf
2
321
2
11 2 xxxxp 3212 xxxp
Creating function file:
File starts with function
= is a must for return
Input argument separated
by comma
Output argument
separated by comma
Input argument under first
brace
Output argument under
third brace
Prep. by: Dr. M. Nuruzzaman
yx
y
dx
dyx 42
2
xC
xy
22
ODE without boundary condition
dsolve(ODE code,indpendent variable)
S=dsolve('2*x*Dy=y^2/x+4*y','x');
pretty(S)
01522
2
ydt
dy
dt
yd)(ty tt eCeC 32
5
1
=
S=dsolve('D2y-2*Dy-15*y=0');
pretty(S)
02'''' yyy xeCxy 1)(
2
7sin
2
7cos 32
2x
Cx
Cex
>>S=dsolve('D3y+Dy-2*y=0','x');
pretty(S)
first derivative Dy
Second derivative D2y
Third derivative D3y
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)
21
2
21
1
22
25
yydt
dy
yydt
dy
2)0(
1)0(
2
1
y
y
tt
tt
ee
ee
6
6
5
4
5
65
8
5
3
2
1
yy
Prep. by: Dr. M. Nuruzzaman
Prep. by: Dr. M. Nuruzzaman
y=f(x) versus x graph
From expression
From data
ezplot(function,interval
as a row matrix)
532 2 xxy
33 x
y='2*x^2-3*x+5';
ezplot(y,[-3,3])
plot(x data,y data)
Tabular data Command to plot y vs x:
X -6 -4 0 4 5 7 >>x=[-6 -4 0 4 5 7];
>>y=[9 3 -3 -5 2 0];
>>plot(x,y) y 9 3 -3 -5 2 0
hold
ezplot(x+4',[-3,3]) ezplot(sin(x)',[-3,3])
X -6 -4 0 4 5 7
y1 9 3 -3 -5 2 0
y2 0 -2 1 0 5 7.7
y3 -1 2 8 1 0 -3
plot(x,y1,x,y2,x,y3)
Multiple y
Multiple y
Surface plot
)(8 22 yx ),( yxf
ezsurf('-8*(x^2+y^2)')
from expression from data
Use surf or mesh ezsurf(function code,[x min x max y min ymax])
default surf(functional data as a
retangular matrix,x
variation as a row matrix, y
variation as a row matrix)
Needs computing or
sample generation
Prep. by: Dr. M. Nuruzzaman
2,2 yx
22 x 40 y
22),( yxyxyxf
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)
tx cos2 ty sin 20 t
Parametric equation
x='2*cos(t)'; y='sin(t)';
ezplot(x,y,[0,2*pi])
21 1)1ln(ln yyxyy
Implicit function 1.55.2 x 23 y
E='1/y-log(y)+log(-1+y+x)+y^2-1';
D=[-2.5 5.1 -3 2];
ezplot(E,D)
Surface plot
Contour plot for
),( yxf
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)
2423
2
1),( yxeyxyxf
2.12.1 x 11 y
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
Prep. by: Dr. M. Nuruzzaman
Discrete plot by stem, bar, bar3,
Prep. by: Dr. M. Nuruzzaman
Random numbers
Generation of a single random
number X between 4 and 5,
X=unifrnd(-4,5)
Decimal random numbers:
unifrnd(lower bound,upper bound,row number,column number)
Generation of a rectangular matrix X of order 23 in
which every element is inbetween -4 and 5,
X=unifrnd(-4,5,2,3)
Generation of a single random integer X between -3 and 5,
X=randint(1,1,[-3 5])
Integer random numbers:
unifrnd(row number,column number, [lower bound upper bound])
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])
Excel file reading
f=xlsread(book1.xls')
Data statistics
datastats(y)
Prep. by: Dr. M. Nuruzzaman
Prep. by: Dr. M. Nuruzzaman
Prep. by: Dr. M. Nuruzzaman
Prep. by: Dr. M. Nuruzzaman