240 likes | 822 Views
Advanced MATLAB programming. Morris Law Jan 19, 2013. Outline. Advance programming in MATLAB Error analysis using Hilbert matrix Solving non-linear equations Function approximation using Taylor's expansion Solving ordinary differential equations MATLAB toolboxes Simulink
E N D
Advanced MATLAB programming Morris Law Jan 19, 2013
Outline • Advance programming in MATLAB • Error analysis using Hilbert matrix • Solving non-linear equations • Function approximation using Taylor's expansion • Solving ordinary differential equations • MATLAB toolboxes • Simulink • Image processing • MATLAB GUI • MATLAB for fun
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
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
Solving non-linear equations • The following non-linear equations are considered. • Above equations are stored in testfun.m. • Consider one equation for each test.
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
Function approximations using Taylor’s expansion • sin(x) can be approximated by expanding in Taylor series with f(x)=sin(x) at a=0 • The Taylor series of f(x)
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
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.
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)
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.
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
Simulink Toolbox • Provide a simulation environment for common discrete/continuous system • Invoke by typing ‘simulink’ in command windows
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
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)
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
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
Thank you! For enquiry: send e-mail to morris@hkbu.edu.hk