Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB 程式設計進階篇 一般數學函數的處理與分析

Similar presentations


Presentation on theme: "MATLAB 程式設計進階篇 一般數學函數的處理與分析"— Presentation transcript:

1 MATLAB 程式設計進階篇 一般數學函數的處理與分析
張智星 台大資工系 多媒體檢索實驗室

2 函數的函數 MATLAB 可對數學函數進行各種運算與分析,例如: 如何表示此種被分析的函數? 字串 作圖
求根 優化:求函數的極大或極小值 數值積分 求解微分方程式 如何表示此種被分析的函數? 字串 函數握把 (function handles) 匿名函數 (anonymous function) Functions of Functions!

3 一維數學函數的範例 MATLAB 是以 M 檔案(副檔名為 m)來表示一個函數
例如,內建於MATLAB目錄的 humps.m 可用來計算下列函數: 更多資訊: 欲顯示此檔案的位置  which humps 欲顯示此檔案的內容  type humps

4 We use 函數 to represent both.
提示 MATLAB 常被用到的測試函數 humps:單輸入函數 peaks:雙輸入函數 「函式」和「函數」都代表「functions」,兩者常會混用,若要正名,可區分如下: 函數:通常用來表示「mathematic functions」 函式:通常用來表示「subroutines or functions in a programming language」 We use 函數 to represent both.

5 數學函數的作圖 表示函數的方式 用 fplot 指令進行數學函數作圖 函數握把:使用 @humps 來代表 humps.m
範例:fplot01.m Less flexible! subplot(2,1,1); fplot('humps', [0,2]); % 使用字串指定函式 subplot(2,1,2); [0 2]); % 使用函式握把來指定函式

6 同時改變 x、y 的區間 我們可同時改變 x 和 y 的區間 範例:fplot02.m x 的區間為[0, 1] y 的區間為[5, 25]
fplot('humps', [0, 1, 5, 25]); grid on % 畫出格線

7 匿名函式 fplot 也接受匿名函式(當場指定的函式) 範例:fplot021.m subplot(2,1,1);
fplot('sin(2*x)+cos(x)', [-10, 10]) % 使用字串指定函式 subplot(2,1,2); [-10, 10]) % 使用函式握把來指定函式

8 對多個函數作圖 fplot 也可同時對多個函數作圖,其中每個函數須以一個行向量來表示 範例:fplot022.m
x 是行向量(MATLAB 預設值) [sin(x), exp(-x)] 是二個行向量 每個行向量代表一個函數(即一條曲線) exp(-x)], [0, 10])

9 Function handle is more flexible!
帶有參數的函數 匿名函式也可以帶有參數 範例:fplot023.m 此時 不可省略,以便指定自變數 Function handle is more flexible! a=1; b=1.1; c=1.2; sin(b*x), sin(c*x)], [0, 10])

10 產生 X、Y 座標點 fplot 可進行描點作圖,類似 plot(x, y),但x 座標點的密度根據函數值的變化決定
我們顯示 fplot 所產生的 x 座標點 範例:fplot03.m 函數變化平緩處,產生 稀疏的點 函數變化劇烈處,產生 緊密的點 [x, y] = [-1,2]); plot(x, y, '-o');

11 產生更密的 X 座標點 (1) 若欲產生更密的 x 座標點,可在 fplot 指令加入另一個輸入引數,已指定相對容忍度(Tolerance)
範例:fplot04.m subplot (2,1,1); [0.01,0.1]); subplot (2,1,2); [0.01,0.1], );

12 產生更密的 X 座標點 (2) 在第一圖中,fplot 指令使用預設相對容忍度,其值為 0.002。
在第二圖中,相對容忍度被設為 ,可得到更準確的圖形,但也要花更多計算及作圖時間。

13 ezplot 指令 ezplot指令和fplot指令類似,可進行描點作圖,但使用更為簡便,預設的作圖範圍為 範例8-7:ezplot01.m

14 平面中的參數式曲線 ezplot 也可畫出平面中的參數式曲線 範例8-8:ezplot02.m 參數式函數的參數預設範圍仍是 利薩如圖形
@(t)cos(5*t)); 利薩如圖形 (Lissajous Figures)

15 空間中的參數式曲線 ezplot3 可畫出空間中的參數式曲線 範例8-8:ezplot021.m 參數式函數的參數預設範圍仍是
3D利薩如圖形

16 隱函數作圖 ezplot 指令可用於隱函數作圖 下列範例可以畫出 範例8-9:ezplot03.m

17 函數的求根 fzero 指令 用於單變數函數的求根 語法 x = fzero(fun, x0)

18 X0 對 fzero 的影響 fzero 指令根據 x0 不同而執行下列動作 若 x0 為一個起始點 若已知使函數值不同號的兩點
逐步縮小此區間以找出零點 若 fzero 無法找出此區間,傳回 NaN 若已知使函數值不同號的兩點 由 x0 直接指定尋根的區間 fzero 更快速找到位於此區間內的根

19 求根範例 (1) 找出humps在 x = 1.5 附近的根,並驗算 範例8-10:fzero01.m
fzero 先找到在 1.5 附近變號的兩點(即 1.26 及 ),然後再找出 humps 的零點 x = 1.5); % 求靠近 1.5 附近的根 y = humps(x); % 帶入求值 fprintf('humps(%f) = %f\n', x , y); humps( ) =

20 求根範例 (2) 若已知 humps 在 x = -1 及 1 間為異號 令 x0 = [-1, 1] 為起始區間來找出 humps 的零點
範例8-11:fzero02.m 此時 fzero 找到的是另一個零點 x = [-1, 1]); % 求落於區間 [-1, 1] 的根 y = humps(x); % 帶入求值 fprintf('humps(%f) = %f\n', x , y); humps( ) =

21 求根範例 (3) 若要畫出以上兩個零點,請見下列範例 範例8-12:fzero03.m
[-1, 2]); grid on z1 = 1.5); z2 = [-1, 1]); line(z1, humps(z1), 'marker', 'o', 'color', 'r'); line(z2, humps(z2), 'marker', 'o', 'color', 'r');

22 顯示求解過程的中間結果 (1) MATLAB 可以顯示求解過程的中間結果 使用 optimset 指令來設定顯示選項
再將 optimset 傳回結構變數送入 fzero 範例8-13:fzero04.m optimset 常用於設定最佳化的選項,下一節會有比較完整的介紹 opt = optimset('disp', 'iter'); % 顯示每個 iteration 的結果 a = [-1, 1], opt)

23 顯示求解過程的中間結果 (2) 求零點過程中,找下一點的兩個方法顯示在第四個欄位(Procedure 欄位)
Func-count x f(x) Procedure initial I initial interpolation bisection interpolation bisection interpolation interpolation interpolation e interpolation e interpolation e interpolation e interpolation Zero found in the interval: [-1, 1]. a = 求零點過程中,找下一點的兩個方法顯示在第四個欄位(Procedure 欄位) 二分法(Bisection) 內插法(Interpolation) 可由doc fzero找到所使用的演算法

24 數值積分 MATLAB 可用於計算單變函數定積分
quad:適應式 Simpson 積分法(Adaptive Simpson Quadrature) quadl:適應式 Lobatto 積分法(Adaptive Lobatto Quadrature)

25 定積分 計算 humps 在 [0, 1] 的定積分 quad 及 quad8 都應用遞迴程序
>> q = 0, 1) q = quad 及 quad8 都應用遞迴程序 若遞迴次數達 10 次,兩種方法均會傳回 Inf 表示所計算之定積分可能不存在 quad 及 quad8第四個引數用來指定積分的相對誤差容忍度

26 曲線的長度 (1) quad 及 quadl 計算曲線的長度 一曲線是由下列參數化的方程式來表示 t 的範圍為 [0, 3*pi]
範例:plotCurve.m t = 0:0.1:3*pi; plot3(sin(2*t), cos(t), t);

27 曲線的長度 (2) 此曲線的長度等於

28 曲線的長度 (3) 先定義函數 curveLength.m 曲線長度可計算如下 >> type curveLength.m
function out = curveLength(t) out = sqrt(4*cos(2*t).^2+sin(t).^2+1); 曲線長度可計算如下 >> len = 0, 3*pi) len =

29 雙重積分 (1) dblquad 指令 用來計算雙重積分 欲計算 其中 先建立被積分的函數 integrand.m
>> type integarnd.m function out = integrand(x, y) out = y*sin(x) + x*cos(y);

30 雙重積分 (2) 計算雙重積分 其中 xMin:內迴圈積分的下界值 xMax:內迴圈積分的上界值 yMin:外迴圈積分的下界值
result = dblquad( 'integrand', xMin, xMax, yMin, yMax) 其中 xMin:內迴圈積分的下界值 xMax:內迴圈積分的上界值 yMin:外迴圈積分的下界值 yMax:外迴圈積分的上界值

31 雙重積分 (3) 範例:dblquad01.m 一般的情況下dblquad 會呼叫 quad 計算定積分。若須呼叫更為精確的 quadl,可執行下列指令 >>result = xMin, xMax, yMin, yMax, 'quadl') result = xMin = pi; xMax = 2*pi; yMin = 0; yMax = pi; result = xMin, xMax, yMin, yMax) result =

32 函數的優化 MATLAB 提供了數個基本指令來進行數學函數的優化,本節將介紹:
單變數函數的最小化: fminbnd 多變數函數的最小化: fminsearch 設定最佳化的選項 若讀者有興趣使用較複雜的方法,可以使用「最佳化工具箱」(Optimization Toolbox)

33 單變函數的最小化 fminbnd 指令 尋求 humps 在 [0.3, 1] 中的最小值 範例:fminbnd01.m
最小值發生在 x = 0.637,且最小值為 [x, minValue] = 0.3, 1) x = 0.6370 minValue =

34 尋求最小值的中間過程 (1) 尋求最小值的中間過程 使用 optimset 指令來設定顯示選項
再將 optimset 傳回結構變數送入 fminbnd 範例8-15:fminbnd02.m opt = optimset('disp', 'iter'); % 顯示每個步驟的結果 [x, minValue] = 0.3, 1, opt)

35 尋求最小值的中間過程 (2) 左表列出x值的變化及相對的函數值 f(x) 最後一欄位列出求極小值的方法,包含 x 值誤差小於 10^-4
Func-count x f(x) Procedure initial golden golden parabolic parabolic parabolic parabolic parabolic parabolic Optimization terminated successfully: the current x satisfies the termination criteria using OPTIONS.TolX of e-004 x = minValue = 左表列出x值的變化及相對的函數值 f(x) 最後一欄位列出求極小值的方法,包含 黃金分割搜尋 (Golden Section Search) 拋物線內插法 (Parabolic Interpolation) x 值誤差小於 10^-4

36 放鬆誤差管制 (1) 放鬆誤差管制 使 fminbnd 提早傳回計算結果 由 optimset 達成 下例將 x 的誤差範圍提高為 0.1
範例8-16:fminbnd03.m opt = optimset( 'disp', 'iter', 'TolX', 0.1); [x, minValue] = 0.3, 1, opt)

37 放鬆誤差管制 (2) Func-count x f(x) Procedure 1 0.567376 12.9098 initial
golden golden parabolic parabolic parabolic  Optimization terminated successfully: the current x satisfies the termination criteria using OPTIONS.TolX of e-001  x = minValue =

38 多變數函數的極小值: fminsearch
求多變數函數的極小值 必須指定一個起始點,讓 fminsearch 求出在起始點附近的局部最小值(Local Minima) Derivative free  Less efficient Method: Downhill Simplex Search (DSS), aka Nelder-Mead method Amoeba method

39 Downhill Simplex Search
DSS in Wikipedia Many variations Basic Steps Use a simplex to explore the objective function, with the operations: Reflection Expansion Contraction Shrink

40 Downhill Simplex Search
About DSS Strengths Straightforward concept Easy programming No gradient or derivative needed Weakness Slow Only good for continuous objective function Could be trapped in local minima Quiz!

41 多變數函數的極小值範例 若目標函數為 若起始點為 我們必須先產生一個 MATLAB 的函數 objective.m
範例:fminsearch01.m function y = objective(x) y = (x(1)-1)^2 +(x(2)-2)^2 + (x(3)-3)^2; x0 = [0, 0, 0]; x = x0) x =

42 最佳化選項 MATLAB 最佳化的選項 經由另一個輸入引數(Input Argument)來進入 fminbnd 或 fminsearch
使用語法 x = x1, x2, options) x = a), x1, x2, options) x = x0, options ) x = a), x0, options ) options 此結構變數可代表各種最佳化的選項(或參數) Extra parameters Extra parameters

43 設定最佳化選項 (1) 如何設定最佳化選項 用 optimset 指令
options = optimset(prop1, value1, prop2, value2, …) prop1、prop2 :欄位名稱 value1、value2 :對應的欄位值

44 設定最佳化選項 (2) 印出最佳化步驟的中間結果,並放鬆誤差範圍
>> options = optimset('Disp', 'iter', 'TolX', 0.1) Display: 'iter' MaxFunEvals: [] MaxIter: [] TolFun: [] TolX: FunValCheck: [] OutputFcn: [] PlotFcns: [] ActiveConstrTol: []

45 設定最佳化選項 (3) options 共有五十多個欄位 如果欄位值顯示是空矩陣, 使用此欄位的預設值來進行運算 顯示非空矩陣的最佳化選項:
>> options = optimset('fminbnd') 顯示非空矩陣的最佳化選項: Display: 'notify MaxFunEvals: 500 MaxIter: 500 TolX: e-004 FunValCheck: 'off'

46 設定最佳化選項 (4) 若輸入 顯示非空矩陣的最佳化選項: Display: 'notify‘
>> options = optimset('fminsearch') 顯示非空矩陣的最佳化選項: Display: 'notify‘ MaxFunEvals: '200*numberofvariables' MaxIter: '200*numberofvariables‘ TolFun: e-004 TolX: e-004 FunValCheck: 'off'

47 最佳化選項說明 (1) Display MaxFunEvals MaxIter 若為 0 (預設值),不顯示中間運算結果
若不為 0,則顯示運算過程的中間結果 MaxFunEvals 函數求值運算(Function Evaluation)的最高次數 對 fminbnd 的預設值是 500 對 fminsearch 的預設值是 200 乘上 x0 的長度 MaxIter 最大疊代次數

48 最佳化選項說明 (2) TolFun TolX 決定終止搜尋的函數值容忍度 預設為 10^-4
此選項只被 fminsearch 用到,fminbnd 並不使用 TolX 終止搜尋的自變數值容忍度,預設為 10-4

49 提示 最佳化並非一蹴可及,通常一再重覆,最後才能收斂到最佳點 最佳化的結果和起始點的選定有很大的關聯,一個良好的起始點 加快最佳化收斂的速度
提高找到全域最佳值(Global Optimum)的機會

50 DSS: Example 1 (1/3) Fermat point Solution
A point P in a plane such that PA+PB+PC is minimized. Solution Analytic solution P satisfies AFB=BFC=CFA=120o Numerical solution All kinds of optimization methods Quiz! Properties!

51 hardwire the data points
DSS: Example 1 (2/3) Objective function dist2ABC.m Main program: goMinDist2ABC.m function sumDistance=dist2ABC(x) % dist2ABC: The distance sum to points A, B, C A=[0 0]’; B=[3 0]’; C=[0 4]’; sumDistance=norm(x-A)+norm(x-B)+norm(x-C); Bad idea to hardwire the data points p0=[5 5]’; % Initial guess p0);

52 DSS: Example 1 (3/3)

53 DSS: Example 2 (1/3) Problem definition Solution
Find a point P such that the total distance to a set of points is minimized. Solution Analytic solution Not sure if it exists… Numerical solution All kinds of optimization methods

54 the data points externally
DSS: Example 2 (2/3) Objective function dist2points.m Main program: goMinDist2points01.m Data matrix of 2xn It’s Better to accept the data points externally function sumDistance=dist2points(x, points) % dist2points: The sum of distance sum to points sumDistance=0; for i=1:size(points,2) sumDistance=sumDistance+norm(x-points(:,i)); end points=[-1 -1; 3 0; 0 4; 1 1; 2 5; 4 2]’; p0=[5 5]’; % Initial guess dist2points(x, points), p0);

55 DSS: Example 2 (3/3)

56 DSS: Example 3 (1/2) Objective function Main program:
minDist2points02.m Main program: goMinDist2points02.m Data matrix of 2xn Initial guess function p=minDist2points02(points, x0, showPlot) % minDist2points: Compute the point that has the min total … if nargin<2||isempty(x0), x0=mean(points, 2); end % Initial guess points), x0); points=[-1 -1; 3 0; 0 4; 1 1; 2 5; 4 2]’; p=minDist2points02(points, [], 1); fprintf('p=%s\n', mat2str(p));

57 DSS: Example 3 (2/2)

58 本章指令彙整 類別 指令名稱 功能 作圖 ezplot fplot 簡便的函數作圖 一般的函數作圖 求根 fzero 單變數函數的求根
最佳化 fminbnd fminsearch 單變數函數的最小值多變數函數的最小值 數值積分(Quadrature) quad quad8 dblquad 低精度的數值積分 高精度的數值積分 雙重(二維)積分


Download ppt "MATLAB 程式設計進階篇 一般數學函數的處理與分析"

Similar presentations


Ads by Google