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

Matlab Tutorial 3

Embed Size (px)

Citation preview

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 restorationEigenvalues: eig(A) and eigs(A)Eigenvectors returned as columns of VEigenvalues returned as main diagonal elements of a diagonal matrix Deigs(A,m,option) returns m eigenvalues of smallest/largest magnitude as specified by option.Singular Value Decomposition (SVD)12 2 20 ...0 0 ..., ,mA U V where A Av v AA u u | | | |= = = = | | |\ .# %matrix of singular values svds(A,m,options) finds m smallest/largest singular valuesOther standard matrix decompositions available in the basic Matlab package arelu(A) LU decompositionqr(A) QR decompositionchol(A) Cholesky factorizationschur(A) Schur decompositionRootfinding 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 x2Linear 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 mthorder. 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 1Least-Square Parameter Estimation for Non-linear ModelsUseful 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-Mentonmaxmin iinn i inxeRRc xy = + ++dataexplanatory varparametersparametersnoiseNonlinear curve fitting 2min max22maxmin, , ,arg min nii i nnR R c n i i iR xe y Rc x | | = + | `+ \ . ) Cost Function (SSE = Sum of Square Errors)Algorithm IngredientsA vector of indep. var. x = [x1, x2, ...]A vector of obsd. var. y = [y1, y2, ...]A vector of initial guesses p0 = [R0min, R0max, c0, n0]A vector of parameters p = [Rmin, Rmax, 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.datamodelNonlinear curve fitting 3Algorithm IngredientsA vector of independent var. x = [x1, x2, ...]A vector of observed. var. y = [y1, y2, ...]A vector of initial values p0 = [R0min, R0max, c0, n0]A vector of parameters p = [Rmin, Rmax, c, n]Systems of 1storder ODEsImplementing ODE solverCreate a myfunction.m function file with the RHS of the ODE.function dydt = myfunction(t,y,params)dydt(1) = F1(y)dydt(2) = F2(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 integrationy0 = [.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 routinePrimary 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 Routineshelp funfun >>Van der Pohl oscillator Function file:( )21 0 y y y y + = Calling routine:(VanDerPohl_nonstiff.m):Screen output:Van der Pohl integration (non-stiff)ODE45: 12.5 sec integration31950 timestepsODE15s: 24 sec integration29725 timestepsAdaptive single-step, Runge-Kutta algorithm (ODE45) is superior, even though more timesteps were taken.Note that smaller timesteps are required near regions of sharp variationVan der Pohl integration (stiff)ODE45: > 1 hr integrationABORTED!ODE15s: .65 sec integration592 timestepsODE23s: 1 sec integration743 timestepsAdaptive single-step, Runge-Kutta 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 equationsconvective roll patternLorenz equations: Matlab CodeFunction fileMain codeNote: Parameters passed as array argument this timenargin determines number of input argumentsLorenz attractor>> subplot(2,1,1)>> plot3(x,y,z);>> subplot(2,1,2)>> plot3(x,y,z);>> view(0,90);090Azimuthal angleElevation angle ==Default view is (AZ,EL) = (-37.5,30) Lorenz attractor: movie of viewsmpeg movie created using a matlab scriptmpgwrite.mwhich may be downloaded at the MathWorks websiteLorenz 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 pointsPartial Differential Equations( ) ( )22( , , ), ,u ua b c u du f x y u x yt 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 formAllows user to define finite element grids to solve problems on irregular domainsGraphical user interfaces (GUIs) facilitate the construction of a computational meshExample finite element grid MEX filesMatlab 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 functionMex 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 MatlabConsider 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 hitOutput: vector C (length A) with counts of A(i) in BExample: 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 fileMatlab for-loop Matlab with Mex file slow fast!Vectorized Codemediumvectorized codeFast Fourier Transform MATLAB calculates discrete Fourier transformsusing 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 powersof 2 but can handle the primes, 3-13 withsome 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.)padded fftshiftImage RestorationLimitations of digital imaging systems noise limitations (Poisson process) resolution limited by finite bandpass of imaging systemGoal of restoration is to improve the image resolution beyond systems bandwidth improve the signal-to-noise ratio (SNR)without a priori knowledge of the sceneHow 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 + nFTG = F x H + Nobject: f image: g inverse: fF ~ G / HLinear Systems with Noiseblurred + gaussian noise (mean = 0, var = 0.001)inversionCan we do better?YesImprovements include linear algorithms: Wiener filtering and Tikhonovregularization non-linear iterative algorithms: Lucy-Richardson (capable of hyperresolution) basic a priori assumptions of typical noise and PSF/OTF propertiesWiener filter: deconvwnr(img,psf,nsr) linear space-invariant minimizes MSE between restoration and truth requires psf and noise modelmin{|f - f|2}originalimgoutwnr imgoutwnr2Regularized Restoration: deconvreg minimize mean square error|g - (f * h + n)|2+ |f|2 regularization penalizes noise dominated results in favor of smoothnessimgblurn imgoutreg imgoutreg2 imgoutreg3Blind deconvolution: deconvblind no knowledge of PSF needed (sensitive to initial size but not shape) iterates on PSF and uses Lucy-Richardson algorithmorig10150deconvblind(image,psf,# iter)Blind deconvolution (cont.)originalblind deconv3 x 3blind deconv7 x 7Web ResourcesMatlab online resources are among the most extensive of all software packages:1. Matlab website: http://www.mathworks.comLatest release notes, detailed documentation, user-supplied m-files and application packages.2. Newsgroup: comp.soft-sys.matlabForum for users and developers to discuss matlab usage, installation, platform specific issues, future refinements, etc.3. Deja news: http://groups.google.comAccess matlab newsgroup archives, search all newsgroups for matlab discussion and interfacing with other software (Maple, Excel,)