Title: Matlab???? From getstart.pdf
1Matlab???? From getstart.pdf
- Hangzhou Dianzi University
- Science School
- 2005?
2????
3MATLAB
??www.mathworks.com ???comp.soft-sys.matlab.
Math and computation Algorithm
development Modeling, simulation, and
prototyping Data analysis, exploration, and
visualization Scientific and engineering
graphics Application development, including
graphical user interface building
4MATLAB
matrix laboratory. ??????array,no
t require dimensioning.
5??????
Development Environment. The MATLAB Mathematical
Function Library. The MATLAB Language. Handle
Graphics. The MATLAB Application Program
Interface (API).
6Development Environment.
This is the set of tools and facilities that help
you use MATLAB functions and files. Many of these
tools are graphical user interfaces. It includes
the MATLAB desktop and Command Window, a command
history, and browsers for viewing help, the
workspace, files, and the search path.
7(No Transcript)
8The MATLAB Mathematical Function Library.
This is a vast collection of computational algorit
hms ranging from elementary functions like sum,
sine, cosine, and complex arithmetic, to more
sophisticated functions like matrix inverse,
matrix eigenvalues, Bessel functions, and fast
Fourier transforms.
9The MATLAB Language
This is a high-level matrix/array language with
control flow statements, functions, data
structures, input/output, and object-oriented
programming features. It allows both programming
in the small to rapidly create quick and dirty
throw-away programs, and programming in the
large to create complete large and complex
application programs.
10The MATLAB Application Program Interface (API).
This is the MATLAB graphics system. It includes
high-level commands for two-dimensional and
three-dimensional data visualization, image
processing, animation, and presentation graphics.
It also includes low-level commands that allow
you to fully customize the appearance of graphics
as well as to build complete graphical user
interfaces on your MATLAB applications.
11????
12Flow Control
- if ????
- switch??
- for??
- while ??
- continue??
- break??
13IF
if rem(n,2) 0 M odd_magic(n) elseif rem(n,4)
0 M single_even_magic(n) else M
double_even_magic(n) end
14IF
a1 23 4
a1b4 if ab a3 else a4 end
if all(a())
if a
b 2 3 4 7 ab isequal(a,b)
15IF
if agtb greater else if altb less
elseif ab equal else error(unepected
situation) end
isequal isempty all any
16Switch and Case
switch (rem(n,4) 0) (rem(n,2)0) case 0
M odd_magic(n) case 1 M
single_even_magic(n) case 2 M
double_even_magic(n) otherwise error(This
is impossible) end
17For
for n 332 r(n) rank(magic(n)) end r
for i1m for j1n H(i,j) 1/(ij)
end end
18While
a0 fa -Inf b3 fbInf while b-agt epsb
x (ab)/2 fxx3-2x-5 if sign(fx)
sign(fa) ax fafx else
bxfbfx end end x
19Continue
fid fopen(magic.m,r) count0 while
feof(fid) line fgetl(fid) if
isempty(line)strncmp(line,,1)
continue end count count 1 end
disp(sprintf(d lines,count))
20Break
a0 fa -Inf b3 fbInf while b-agt epsb
x (ab)/2 fxx3-2x-5 if fx 0
break elseif sign(fx) sign(fa) ax
fafx else bxfbfx end end
21Script
Investigate the rank of magic squares r
zeros(1,32) for n332 r(n)rank(magic(n))
end r bar(r)
?edit???????,????magicrank.m ?matlab??? gtgt
magicrank.m (gtgt????)
22(No Transcript)
23Function
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) if
nargin1 tol max(size(A)') max(s)
eps end r sum(s gt tol)
rank.m
??lookfor
help
s,tol,r?????
24rank
help rank lookfor rank rank(A) r rank(A) r
rank(A,1.e-6)
25Global Variables
function h falling(t) global GRAVITY h
1/2Gravityt.2
gtgtglobal GRAVITY gtgtGRAVITY 32 gtgty
falling((0.15))
26Passing String Arguments to Functions
foo a b c
foo(a,b,c)
legend apples orange legh,objh
legend(apples,oranges)
27Constructing string arguments in code
for d 131 s August int2str(d) .dat
load(s) code to process the contents of the
d-th file end
A0 6 1 6 2 16 -5 20 10 eig(A) eig
A ?
28eval
for d 131 s load August int2str(d)
.dat load(s) code to process the
contents of the d-th file end
29Vectorization and Preallocation
x .01 for k 11001 y(k) log10(x)
x x .01 end
x .01.0110 y log10(x)
r zeros(32,1) for n132
r(n)rank(magic(n)) end
30Function Handles
function x plot_fhandle(fhandle,data)
plot(data,fevel(fhandle,data))
plot_fhandle(_at_sin, -pi0.01pi)
plot_fhandle(sin, -pi0.01pi)
31Function Functions
- Zero finding
- Optimization
- Quadrature
- Ordinary differential equations
function y hupms(x) y 1./((x-.3).2
.01)1./((x-.9).2.04)-6
x 0.0021 y humps(x) plot(x,y)
32(No Transcript)
33Function Functions
p fminsearch(_at_humps,.5) Q
quad1(_at_humps,0,1) z fzero(_at_humps, .5)
34(No Transcript)
35close all rand('seed',.123456) NumberInside
0 PiEstimate zeros(500,1) for k1500 x
-12rand(100,1) y -12rand(100,1)
NumberInside NumberInside sum(x.2 y.2 lt
1) PiEstimate(k) (NumberInside/(k100))4
end plot(PiEstimate) title(sprintf('Monte
Carlo Estimate of Pi 5.3f',PiEstimate(500))) x
label('Hundreds of Trials')
36Other data structures
- Multidimensional arrays
- Cell arrays
- Characters and text
- Structures