Advanced MATLAB programming Morris Law Jan 19, 2013 1 Outline Advance programming in MATLAB MATLAB toolboxes Error analysis using Hilbert matrix Solving non-linear equations Function approximation using Taylor's expansion Solving ordinary differential equations Simulink Image processing MATLAB GUI MATLAB for fun 2 Error analysis using Hilbert matrix Hilbert matrix is an NxN matrix with element (1/i+j-1). It is a typical example of ill conditioned matrix. hilb(5) in MATLAB will give 5x5 Hilbert matrix 1 1 2 13 1 4 1 5 1 2 1 3 1 4 1 3 1 4 1 5 1 4 1 5 1 6 1 5 1 6 1 7 1 6 1 7 1 8 1 6 1 7 1 8 1 9 1 5 3 Error analysis using Hilbert matrix Try the following MATLAB code (hilbtest.m) n = 5; H = hilb(n); x = [1:n]’; b = H * x; xx = H \ b ; error = x – xx condH = cond(H) Change n to 10, 15, 20 to test the error 4 Solving non-linear equations The following non-linear equations are considered. y sin( x) *sin( x) 2 x x 2 y e 2 x cos( x) x 2 y 1/ (1 x ) 0.5 2 Above equations are stored in testfun.m. Consider one equation for each test. 5 Solving non-linear equations You may write your own solver using bisection method. Refer to nonlineq.m Or you may solve it simply for matlab function fzero. Refer to solvefun.m in the following function x0 = solvefunc % solve nonlinear function x = [-5:0.02:5]; y = testfun(x); hold on; plot(x,y); plot(x,zeros(size(x)),'r'); hold off x0=fzero('testfun',1); % solve with initial guess 1 6 Function approximations using Taylor’s expansion The Taylor series of f(x) xa ( x a)2 '' f ( x) f (a ) f (a ) f (a) 1! 2! ' ( x a)n f (a) n! n sin(x) can be approximated by expanding in Taylor series with f(x)=sin(x) at a=0 x3 x5 sin( x) x 3! 5! 2 n 1 x (1)n (2n 1)! 7 Function approximations using Taylor’s expansion The MATLAB code for sinappx.m can be written as, %approximate sin by taylor polynomial function y=sinappx(x) term=x; y=term; x2=x*x; n=1; %initialization while abs(term)>eps %have we summed enough yet? term=-term*x2/((n+1)*(n+2)); %update term y=y+term; %update sum n=n+2; %update n end Similarly you can modify the above to write cosappx.m 8 Solving ordinary differential equations Ordinary differential equation can be solved in MATLAB using functions like ode23 or ode45. For first order ODE, simply prepare a function y’ = f(x) and name it as yprime.m, then solve by [t,y]=ode23(@yprime, tspan,y0) Plot the graph to show the solution. For higher order ODE, rewrite it into a system of first order ODE and solve similarly. 9 Solving ordinary differential equations To solve y’ - y – t = 0, the matlab code yprime.m should be like, function yp = yprime(y,t) yp = y + t; Solve it by tspan=[0,10]; y0 = 0; [t,y] = ode23(@yprime,tspan,y0) Plot the graph t vs y to show the solution plot(t,y) 10 Solving ordinary differential equations Another example to solve a 2nd order ODE, y'' + y' + y + t = 0, the matlab code yprime1.m should be like, function yp=yprime1(y,t) yp(1) = y(2); yp(2) = -y(2) - y(1) -t; Solve it by tspan=[0,10]; y0 = 0; [t,y] = ode23(@yprime1,tspan,y0) Plot the graph t vs the first and second column of y to show the solution y and y’ plot(t,y(*,1),y(*,2)) Refer to forfun/orbit.m to solve an orbit trajectory using ode23. 11 MATLAB Toolboxes Toolboxes add more functions and feature into MATLAB. You may also write own toolboxes. OEE501 has another MATLAB classroom license with the following toolboxes, Simulink Control System Toolbox Image Processing Toolbox Signal Processing Toolbox Simulink control design Toolbox Statistics Toolbox Science Faculty has subscribed the following toolboxes, Simulink Control System Toolbox Image Processing Toolbox MATLAB Compiler Neural Network Toolbox Optimization Toolbox Partial Differential Equation Toolbox Signal Processing Toolbox Simulink Control Design Spline Toolbox Statistics Toolbox Symbolic Math Toolbox System Identification Toolbox Wavelet Toolbox 12 Simulink Toolbox • Provide a simulation environment for common discrete/continuous system • Invoke by typing ‘simulink’ in command windows 13 Image Processing Toolbox Functions specialised for image processing such as imread, imshow, imadjust, imhist, histeq, …… Support almost all image format input and output. RGB vs index vs BW images 14 Examples Counting grain from microscopic images % % Demo for functions in image processing toolbox % imgdemo.m % I=imread('rice.png'); %J=imread('demo.jpg'); %I=rgb2gray(J); imshow(I) background = imopen(I,strel('disk',15)); figure, surf(double(background(1:8:end,1:8:end))),zlim([0 255]); set(gca,'ydir','reverse'); I2 = I - background; imshow(I2) I3 = imadjust(I2); imshow(I3); level = graythresh(I3); bw = im2bw(I3,level); bw = bwareaopen(bw, 50); imshow(bw); cc = bwconncomp(bw, 4); grain = false(size(bw)); grain(cc.PixelIdxList{8}) = true; figure;imshow(grain) 15 MATLAB GUIDE Development Environment for Graphical User Interface Invoke with ‘guide’ in command window Plenty of user interface like button, textbox, scrollbar to develop screen interfaces. Callback functions can be written for each graphics object. Save and load the GUI as figures 16 MATLAB for fun Check machine constants machine.m Plot circle using parametric equation circle.m, drawpattern.m Plot graph by loading data from file dataplot.m, grid.plt tic-tac-toe game written in MATLAB play.m 17 Thank you! For enquiry: send e-mail to morris@hkbu.edu.hk 18