Sie sind auf Seite 1von 34

# Special applications

## Miscellaneous operations: Eigenvalues,

SVD, Root Finding, Minimization
Curve fitting
Differential equations
Interfacing with C and Fortran, MEX files
Fast Fourier Transform (FFT)
Image restoration
Eigenvalues: eig(A) and eigs(A)
Eigenvectors returned as columns of V
Eigenvalues returned as main diagonal
elements of a diagonal matrix D
eigs(A,m,option) returns m
eigenvalues of smallest/largest
magnitude as specified by option.
Singular Value Decomposition (SVD)
1
2 2 2
0 ...
0 0 ...
, ,
m
A U V where A Av v AA u u

| |
|
|
= = = =
|
|
|
\ .
# %
matrix of
singular
values
svds(A,m,options) finds m
smallest/largest singular values
Other standard matrix decompositions
available in the basic Matlab package
are
lu(A) LU decomposition
qr(A) QR decomposition
chol(A) Cholesky factorization
schur(A) Schur decomposition
Rootfinding and Finding Minima
>> y = inline(tanh(x)-x^2,x);
>> fplot([tanh(x),x^2],[-2,2]);
fzero(y,x0) finds a
root of y(x) close
to x0.
fplot plots inline
functions
>> y = inline(sin(x^2)-sqrt(x),x);
>> fplot(y,[0,6]);
fminbnd(y,x1,x2) finds
a minimum of y(x)
between x1 and x2
Linear least squares fit (polyfit)
Given vectors of input data x(1:N) and output
data y(1:N) find the best straight line fit:
>> polyfit(x,y,m) finds coefficients of a best fit
polynomial of m
th
order. Here we do a linear
fit of order 1 for a linear function with noise.
( ) 3 1 (0,1) y x x N = +
y c x d = +
Gaussian noise (zero
mean, unit variance)
Nonlinear curve fitting 1
Least-Square Parameter Estimation for Non-linear Models
Useful when:
No transformation (e.g. log, exp, etc.) is available to make the model linear in parameters.
Transformation results in non-Gaussian noise, but need to do statistics on the parameter estimates.
Step 1: Define the model.
Step 2: Define the cost function (sum of square-error) as a M-file or as an inline funciton.
Step 3: Run nonlinear unconstrained minimization routine (fminsearch).
Example:
Modified Michaelis-Menton
max
min
i
i
n
n
i i
n
x
e
R
R
c x
y = + +
+
data
explanatory var
parameters
parameters
noise
Nonlinear curve fitting 2
min max
2
2
max
min
, , ,
arg min
n
i
i i
n
n
R R c n
i i
i
R x
e y R
c x

| |

= +
| `
+

\ .
)

Cost Function (SSE = Sum of Square Errors)
Algorithm Ingredients
A vector of indep. var. x = [x
1
, x
2
, ...]
A vector of obsd. var. y = [y
1
, y
2
, ...]
A vector of initial guesses p0 = [R0
min
, R0
max
, c0, n0]
A vector of parameters p = [R
min
, R
max
, c, n]
User must
supply these
inputs.
Algorithm makes improved guesses iteratively to make SSE smaller.
Algorithm stops when it can no longer improve SSE.
data
model
Nonlinear curve fitting 3
Algorithm Ingredients
A vector of independent var. x = [x
1
, x
2
, ...]
A vector of observed. var. y = [y
1
, y
2
, ...]
A vector of initial values p0 = [R0
min
, R0
max
, c0, n0]
A vector of parameters p = [R
min
, R
max
, c, n]
Systems of 1
st
order ODEs
Implementing ODE solver
Create a myfunction.m function file with the RHS of the ODE.
function dydt = myfunction(t,y,params)
dydt(1) = F
1
(y)
dydt(2) = F
2
(y)
In a separate matlab script run a wisely chosen integration routine,
choose integration time range, initial conditions, error tolerances,etc.
tspan = [0 20]; % Time domain for integration
y0 = [.1; -2]; % Initial data
[t,y] = ode_routine[myfunction,tspan,y0,options,params];
plot(t,y)
Parameter vector (params) may be defined globally or passed through
the integration calling routine to the function file as local variables.
# #
Choosing an integration routine
Primary considerations:
Accuracy desired (higher accuracy more computation time)
Stiff vs. non-stiff equations (large or small parameters can cause
highly disparate dynamical timescales)
Complexity of dynamical equations (i.e. when RHS is expensive
to compute it is advantageous to specify the Jacobian of the
system explicitly, if possible).
Standard Routines
help funfun >>
Van der Pohl oscillator
Function file:
( )
2
1 0 y y y y + =

Calling routine:
(VanDerPohl_nonstiff.m):
Screen output:
Van der Pohl integration (non-stiff)
ODE45: 12.5 sec integration
31950 timesteps
ODE15s: 24 sec integration
29725 timesteps
algorithm (ODE45) is superior, even
though more timesteps were taken.
Note that smaller timesteps are required
near regions of sharp variation
Van der Pohl integration (stiff)
ODE45: > 1 hr integration
ABORTED!
ODE15s: .65 sec integration
592 timesteps
ODE23s: 1 sec integration
743 timesteps
algorithm (ODE45) is worthless for this
stiff problem.
Stiff routines (ode15s,ode23s) were
far more efficient for the stiff system
than the non-stiff system. (multi-step
Gear, Rosenbrock method)
Another ODE example: Lorenz equations
convective roll pattern
Lorenz equations: Matlab Code
Function file
Main code
Note: Parameters passed as array argument this time
nargin determines number of input arguments
Lorenz attractor
>> subplot(2,1,1)
>> plot3(x,y,z);
>> subplot(2,1,2)
>> plot3(x,y,z);
>> view(0,90);
0
90
Azimuthal angle
Elevation angle

=
=
Default view is (AZ,EL) = (-37.5,30)
Lorenz attractor: movie of views
mpeg movie
created using
a matlab script
mpgwrite.m
which may be
the MathWorks
website
Lorenz attractor: moving orbit
>> plot3(x,y,z); hold; view(0,90);
>> line_handle = plot3(x(1),y(1),z(1),'ko'); set(gca,'box','on'); grid on;
>> set(line_handle,'erasemode','xor');
>> for i=1:length(x),
>> set(line_handle,'xdata',xx(i),'ydata',yy(i),'zdata',zz(i));
>> drawnow; movieframe(i) = getgrame(gcf);
>> end;
set(h,)
Alternate way
to plot points
Partial Differential Equations
( ) ( )
2
2
( , , ), ,
u u
a b c u du f x y u x y
t t

+ + =

Matlab has a PDE toolbox that can numerically solve 2nd-order
nonlinear partial differential equations of the form:
Features:
Handles Dirichlet or generalized Neumann boundary
conditions
Can tackle two coupled nonlinear PDEs of the above form
Allows user to define finite element grids to solve
problems on irregular domains
Graphical user interfaces (GUIs) facilitate the
construction of a computational mesh
Example finite
element grid
MEX files
Matlab provides an Application Program Interface (API) that can implement
Fortran/C programs within the Matlab environment. Matlab functions may also be
accessed within Fortran/C programs.
The Matlab script mex.m compiles Fortran/C code to create matlab function
files, called Matlab Executables or mex files
>> mex my_C_file.c
creates MEX file: my_C_file.mexlx (Linux), my_C_file.dll (Win32)
>> [out1,out2,] = my_C_file(in1,in2,)
MEX file can be executed like any other Matlab function
Mex files are especially convenient when you have C/Fortran source code that
you want to implement within matlab but do not want to rewrite completely.
Mex files also speed up your code when computations involving for-loops, while-
do-loops, and other flow control operations are involved. Matlab deals with for-
loops very slowly (although version 6.5 with the JIT-accelerator is improved in this
regard). (** The STUDENT VERSION of 6.5 is now available **)
Example: Dumb Matlab (for loops),
Vectorized Matlab, MEX Matlab
Consider a Matlab function that counts the number
of times that each element of a vector A occurs in
another vector B
C = count(A,B,tol)
Input: vector A (any length)
comparison vector B (any length)
tol = criterion for a hit
Output: vector C (length A) with counts of A(i) in B
Example: A = [1,2,3]
B = [-1,.5,1,1,2.000001,4]
C = count(A,B,.001)
C = [2,1,0]
Matlab with and without MEX file
Matlab for-loop Matlab with Mex file
slow fast!
Vectorized Code
medium
vectorized
code
Fast Fourier Transform
MATLAB calculates discrete Fourier transforms
using the fftw libraries (http://www.fftw.org).
FFT algorithm progressively factors the
problem down to pieces which are multiples of
primes. Functions work best for powers
of 2 but can handle the primes, 3-13 with
some efficiency.
The basic Fourier transform functions are:
fft, ifft, fft2, ifft2, fftn, ifftn.
e.g. image = rand(128,128);
transform = fft2(image,256,256);
Fast Fourier Transform (cont.)
Image Restoration
Limitations of digital imaging systems
noise limitations (Poisson process)
resolution limited by finite bandpass of imaging
system
Goal of restoration is to
improve the image resolution beyond systems
bandwidth
improve the signal-to-noise ratio (SNR)
without a priori knowledge of the scene
How can we do this in MATLAB?
Linear Systems
Is the point spread function known? Is there a model
for the dominant noise process?
Linear imaging system
g = f * h + n
FT
G = F x H + N
object: f image: g inverse: f
F ~ G / H
Linear Systems with Noise
blurred + gaussian
noise (mean = 0, var
= 0.001)
inversion
Can we do better?
Yes
Improvements include
linear algorithms: Wiener filtering and Tikhonov
regularization
non-linear iterative algorithms: Lucy-Richardson
(capable of hyperresolution)
basic a priori assumptions of typical noise and
PSF/OTF properties
Wiener filter: deconvwnr(img,psf,nsr)
linear space-invariant
minimizes MSE between
restoration and truth
requires psf and noise model
min{|f - f|
2
}
original
imgoutwnr imgoutwnr2
Regularized Restoration: deconvreg
minimize mean square error
|g - (f * h + n)|
2
+ |f|
2
regularization penalizes noise dominated
results in favor of smoothness
imgblurn imgoutreg imgoutreg2 imgoutreg3
Blind deconvolution: deconvblind
no knowledge of PSF needed (sensitive to initial size but not
shape)
iterates on PSF and uses Lucy-Richardson algorithm
orig
10
150
deconvblind(image,psf,# iter)
Blind deconvolution (cont.)
original
blind deconv
3 x 3
blind deconv
7 x 7
Web Resources
Matlab online resources are among the most extensive of
all software packages:
1. Matlab website: http://www.mathworks.com
Latest release notes, detailed documentation, user-
supplied m-files and application packages.
2. Newsgroup: comp.soft-sys.matlab
Forum for users and developers to discuss matlab
usage, installation, platform specific issues, future
refinements, etc.