Download presentation

Presentation is loading. Please wait.

Published byGavyn Patchell Modified over 3 years ago

1
1 Advanced MATLAB programming Morris Law Jan 19, 2013

2
2 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

3
3 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

4
4 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

5
5 Solving non-linear equations The following non-linear equations are considered. Above equations are stored in testfun.m. Consider one equation for each test.

6
6 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

7
7 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)

8
8 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

9
9 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.

10
10 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)

11
11 Solving ordinary differential equations Another example to solve a 2 nd 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.

12
12 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

13
13 Simulink Toolbox Provide a simulation environment for common discrete/continuous system Invoke by typing ‘simulink’ in command windows

14
14 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

15
15 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)

16
16 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

17
17 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

18
18 Thank you! For enquiry: send e-mail to morris@hkbu.edu.hk

Similar presentations

Presentation is loading. Please wait....

OK

EE3417 – Introduction to Simulink

EE3417 – Introduction to Simulink

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on fast food business plan Ppt on air water and land pollution Ppt on vehicle to grid Ppt on business plan of a coffee shop Ppt on magnets and electromagnets Ppt on art of war audio Ppt on cross cultural communication barriers Ppt on magnetism and matter class 12 Ppt on conference call etiquette at the office Ppt on algebraic expressions and identities for class 8