Professional Documents
Culture Documents
Introduction to MATLAB
1
Numerical Methods For Engineers
Introduction to MATLAB
Matrices
M ti
and Arrays
2
Numerical Methods For Engineers
Introduction to MATLAB
3
Numerical Methods For Engineers
Introduction to MATLAB
4
Numerical Methods For Engineers
Introduction to MATLAB
>> sum(A
sum(A')'
)
ans =
34
34
34
34
S the
So, th row sums are also
l 34!
>> sum(diag(A))
ans =
34
To calculate the sum of the antidiagonal:
>> A(1,4)+A(2,3)+A(3,2)+A(4,1)
ans =
34
5
Numerical Methods For Engineers
Introduction to MATLAB
12
6
Numerical Methods For Engineers
Introduction to MATLAB
Bad example:
>> sum = 1+2+3
sum =
6
>> sum(1:10)
??? Index exceeds matrix dimensions.
>> clear sum
>> sum(1:10)
ans =
55
ONCE AGAIN, DON'T USE MATLAB
FUNCTION NAMES AS VARIABLES!
13
14
7
Numerical Methods For Engineers
Introduction to MATLAB
Operators
+ Addition
- Subtraction
* Multiplication
/ Division
\ Left division
^ Power
' Complex conjugate transpose
( ) Specify evaluation order
15
Functions
abs, sqrt,
abs sqrt exp
exp, sin etc
etc.
>> help elfun
Elementary math functions.
Trigonometric.
sin - Sine.
sinh - Hyperbolic sine.
asin - Inverse sine.
asinh - Inverse hyperbolic sine.
cos - Cosine.
cosh - Hyperbolic cosine.
................................................................
16
8
Numerical Methods For Engineers
Introduction to MATLAB
pi 3.14159265…
i Imaginary unit,
j Same as i
Inf Infinity
N N
NaN N t
Not-a-number
b
17
Examples of Expressions
rho = (1+sqrt(5))/2
rho =
1.6180
a = abs(3+4i)
a=
5
z = sqrt(besselk(4/3,rho-i))
z=
0.3730+ 0.3214i
18
9
Numerical Methods For Engineers
Introduction to MATLAB
19
20
10
Numerical Methods For Engineers
Introduction to MATLAB
21
22
11
Numerical Methods For Engineers
Introduction to MATLAB
23
24
12
Numerical Methods For Engineers
Introduction to MATLAB
25
26
13
Numerical Methods For Engineers
Introduction to MATLAB
27
Graphics
• There are two ways to handle graphics:
¾The graphic interactive tools
¾Basic plotting functions
28
14
Numerical Methods For Engineers
Introduction to MATLAB
29
Example
x = 0:pi/100:2*pi;
/ 2*
y = sin(x);
plot(x,y)
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot
title( Plot of the Sine Function'
Function ,'FontSize'
FontSize ,12)
12)
30
15
Numerical Methods For Engineers
Introduction to MATLAB
31
legend('sin(x)','sin(x-.25)','sin(x-.5)')
32
16
Numerical Methods For Engineers
Introduction to MATLAB
33
34
17
Numerical Methods For Engineers
Introduction to MATLAB
Graphic markers
x1 = 0:pi/100:2*pi;
x2 = 0:pi/10:2*pi;
plot(x1,sin(x1),'r:',x2,sin(x2),'r+')
35
36
18
Numerical Methods For Engineers
Introduction to MATLAB
Complex Data
plot(Z)
where Z is a complex vector or matrix, is equivalent to
plot(real(Z),imag(Z))
For example,
p ,
t = 0:pi/10:2*pi;
plot(exp(i*t),'-o')
axis equal
37
38
19
Numerical Methods For Engineers
Introduction to MATLAB
39
40
20
Numerical Methods For Engineers
Introduction to MATLAB
Subplots
t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(4*cos(t));
subplot(2,2,1); mesh(X)
subplot(2,2,2); mesh(Y)
p
subplot(2,2,3); mesh(Z)
subplot(2,2,4); mesh(X,Y,Z)
41
42
21
Numerical Methods For Engineers
Introduction to MATLAB
U i the
Using th Print
P i t Command
C d
[x,y,z] = peaks;
pcolor(x,y,z)
shading interp
hold on
contour(x,y,z,20,'k')
hold off
print -dbitmap myfile
43
-dbitmap bitmap
-djpeg jpeg
-deps encapsulated postscript
-dceps encapsulated color postscript
.....................................................................
44
22
Numerical Methods For Engineers
Introduction to MATLAB
Programming
MATLAB has
h severall fl
flow control
t l constructs:
t t
“if”
“switch and case”
“for”
“while”
“continue”
“b k”
“break”
“try - catch”
“return”
We look at the most important ones!
45
If
E
Example
l
if I == J
A(I,J) = 2;
elseif abs(I-J) == 1
A(I,J) = -1;
else
A(I,J) = 0;
end
46
23
Numerical Methods For Engineers
Introduction to MATLAB
For
r = zeros(1,32)
for n = 3:32
r(n) = rank(magic(n));
end
r
47
while
a = 0; fa = -Inf;
b = 3; fb = Inf;
while b-a > eps*b
x = (a+b)/2;
fx = x^3-2*x-5;
if sign(fx) == sign(fa)
a = x; fa = fx;
else
l
b = x; fb = fx;
end
end
x
48
24
Numerical Methods For Engineers
Introduction to MATLAB
>> abs(s)
ans =
104 101 108 108 111
>> ss = double(s)
ss =
104 101 108 108 111
>> char(ss)
ans =
hello
49
>> S = char('A','rolling','stone','gathers','momentum.')
S=
A
rolling
stone
gathers
momentum.
>> size(S)
ans =
5 9
50
25
Numerical Methods For Engineers
Introduction to MATLAB
type magicrank
52
26
Numerical Methods For Engineers
Introduction to MATLAB
53
function r = rank(A,tol)
% RANK Matrix rank.
% RANK(A) provides an estimate of the number of linearly
% independent rows or columns of a matrix A.
% RANK(A,tol) is the number of singular values of A
% that are larger than tol.
% RANK(A) uses the default tol = max(size(A)) * norm(A) * eps.
s = svd(A);
g
if nargin==1
tol = max(size(A)') * max(s) * eps;
end
r = sum(s > tol);
54
27
Numerical Methods For Engineers
Introduction to MATLAB
55
Some Important Commands
• c c – cclear command window
clc ea co a d do
• Who – shows variables in use
• Whos – shows variables in use with details
• Clear – clears all variables in the work space.
• Clear(A) – clears only the variable A
• Size(B) – gives the size of B.
• [m,n] = size(B) – gives the size assigning m for the size of rows and n for
co u
column.
• A.’ – transpose of matrix A with out conjugate complex transformation
• A’ – transpose of matrix including conjugate complex transpose
• Conj(A) – complex conjugate of A
• flipup(A) – flip upside down
• Fliplr(A) – flip left right
56
28
Numerical Methods For Engineers
Introduction to MATLAB
• Format long – will write longer precision up to 12 digits
• Format short
Format short – gives
gives shorter precision digits
shorter precision digits
• Look for ‐ searches for the string topic in the first comment
line (the H1 line) of the help text in all M‐files.
• Matlab indices starts at 1. so A(0) gives error.
• Which ‐ Locate functions and files
• fopen(filename) ‐ opens the file filename for read access
example
fid = fopen(‘FEM‐results.txt’)
st = fgetl(fid) – returns the next line of the file associated
with the file identifier fid.
fclose(fid) – to close the file.
57
• edit filename – to edit M‐file.
• axis([xmin
([ xmax yymin yymax]) ]) ‐ to change the axis of graph
g g p
• grid on – makes grid on
• Clf – clear figure
• Pwd – print working directory
• ginput ‐ enables you to select points from the figure using the
mouse for cursor positioning.
• [x y] =ginput – gives the x and y coordinates of the selected point
• text(2,3, ‘text’) –
( ‘ ’) the word text will be written on the figure at the
h d ll b h f h
specified coordinates
• text(2,3,’text’, ‘fontsize’,20) – to specify the size of the text.
• max(A) – the maximum value among the vector A
• Min(A) – minimum value
58
29
Numerical Methods For Engineers
Introduction to MATLAB
• [m, I] = max(A) – where m gives the max value and I gives
element number of the vector of the max value.
element number of the vector of the max value.
>> text(I, m, ’text’)
• num2str(m) – coverts number to string
• Str2num(m) – converts string to number.
• Colormap (A) – to get the colormap
59
30