matlab中未定义函数optimite,MATLAB程式设计进阶篇一般数学函数处理和分析

《MATLAB程式设计进阶篇一般数学函数处理和分析》由会员分享,可在线阅读,更多相关《MATLAB程式设计进阶篇一般数学函数处理和分析(59页珍藏版)》请在技术文库上搜索。

1、mple 2 (2/3) nObjective function ndist2points.m ntion methods MATLAB 程式設計進階篇:一般數學函數的處理與分析 DSS: Exa exists nNumerical solution nAll kinds of optimizaized. nSolution nAnalytic solution nNot sure if ithat the total distance to a set of points is minim (1/3) nProblem definition nFind a point P such t(3/。

2、3) MATLAB 程式設計進階篇:一般數學函數的處理與分析 DSS: Example 2oints MATLAB 程式設計進階篇:一般數學函數的處理與分析 DSS: Example 1 Main program: ngoMinDist2points01.m function sumDich(dist2ABC, p0); Bad idea to hardwire the data p(x-B)+norm(x-C); p0=5 5;% Initial guess p=fminsear C A=0 0; B=3 0; C=0 4; sumDistance=norm(x-A)+normBC(x) %。

3、 dist2ABC: The distance sum to points A, B,gram: ngoMinDist2ABC.m function sumDistance=dist2A1 (2/3) nObjective function ndist2ABC.m nMain proerties! MATLAB 程式設計進階篇:一般數學函數的處理與分析 DSS: Example tion nAll kinds of optimization methods Quiz! Proption nP satisfies AFB=BFC=CFA=120o nNumerical soluat PA+PB+。

4、PC is minimized. nSolution nAnalytic solustance=dist2points(x, points) % dist2points: The s (1/3) nFermat point nA point P in a plane such thyFun() MATLAB 程式設計進階篇:一般數學函數的處理與分析 DSS: Example 1f DSS Old usage Extra parameters To be passed to m; nx=fminsearch(myFun, x0, , prm); More Examples ort the sea。

5、rch nx=fminsearch(x) myFun(x, prm), x0); nVariants nObjective function myFun(x, prm) nStathe search via fminsearch nx=fminsearch(myFun, x0) function myFun(x) nSet initial guess “x0” nStart own-hill Simplex search (DSS) nDefine an objectiveimum)的機會 MATLAB 程式設計進階篇:一般數學函數的處理與分析 nSteps for Dum of distan。

6、ce sum to points sumDistance=0; for i=有很大的關聯,一 個良好的起始點 n加快最佳化收斂的速度 n提高找到全域最佳值(Global Opt分析 提示 n最佳化並非一蹴可及,通常一再重覆,最後才能 收斂到最佳點 n最佳化的結果和起始點的選定n終止搜尋的自變數值容忍度,預設為 10-4 MATLAB 程式設計進階篇:一般數學函數的處理與預設為 10-4 n此選項只被 fminsearch 用到,fminbnd 並不使用 nTolX 一般數學函數的處理與分析 最佳化選項說明 (2) nTolFun n決定終止搜尋的函數值容忍度 nn對 fminsearch 的預。

7、設值是 200 乘上 x0 的長度 MATLAB 程式設計進階篇: 乘上 x0 的長度 nMaxIter n最大疊代次數 n對 fminbnd 的預設值是 500 的最高次數 n對 fminbnd 的預設值是 500 n對 fminsearch 的預設值是 200過程的中間結果 nMaxFunEvals n函數求值運算(Function Evaluation) (1) nDisplay n若為 0 (預設值),不顯示中間運算結果 n若不為 0,則顯示運算1:size(points,2) sumDistance=sumDistance+norm(x-poValCheck: off MATLAB 。

8、程式設計進階篇:一般數學函數的處理與分析 最佳化選項說明ables nTolFun: 1.0000e-004 nTolX: 1.0000e-004 nFun 200*numberofvariables nMaxIter: 200*numberofvarich) n顯示非空矩陣的最佳化選項: nDisplay: notify nMaxFunEvals:理與分析 設定最佳化選項 (4) n若輸入 options = optimset(fminsear00e-004 nFunValCheck: off MATLAB 程式設計進階篇:一般數學函數的處notify nMaxFunEvals: 500 n。

9、MaxIter: 500 nTolX: 1.00ions = optimset(fminbnd) n顯示非空矩陣的最佳化選項: nDisplay: ptions 共有五十多個欄位 n如果欄位值顯示是空矩陣, n使用此欄位的預設值來進行運算 opt MATLAB 程式設計進階篇:一般數學函數的處理與分析 設定最佳化選項 (3) noints(:,i); end points=-1 -1; 3 0; 0 4; 1 1; 2 5; PlotFcns: ActiveConstrTol: 000 FunValCheck: OutputFcn: r: TolFun: TolX: 0.1play: iter。

10、 MaxFunEvals: MaxIte= optimset(Disp, iter, TolX, 0.1) Dis的處理與分析 設定最佳化選項 (2) n印出最佳化步驟的中間結果,並放鬆誤差範圍 options 位名稱 nvalue1、value2 :對應的欄位值 MATLAB 程式設計進階篇:一般數學函數et(prop1, value1, prop2, value2, ) nprop1、prop2 :欄選項 (1) n如何設定最佳化選項 n用 optimset 指令 options = optimsxtra parameters MATLAB 程式設計進階篇:一般數學函數的處理與分析 設定最。

11、佳化4 2; p0=5 5;% Initial guess p=fminsearch(x) dist2 options n此結構變數可代表各種最佳化的選項(或參數) Extra parameters E ) x = fminsearch(x)objFun(x, a), x0, options ) n x2, options) 或 x = fminsearch(objFun, x0, options x1, x2, options) x = fminbnd(x)objFun(x, a), x1,來進入 fminbnd 或 fminsearch n使用語法 x = fminbnd(objFun,分。

12、析 最佳化選項 nMATLAB 最佳化的選項 n經由另一個輸入引數(Input Argument) 1.0000 2.0000 3.0000 MATLAB 程式設計進階篇:一般數學函數的處理與ive(x) y = (x(1)-1)2 +(x(2)-2)2 + (x(3)-3)2; x = x = fminsearch(objective, x0) function y = objectective.m n若起始點為 n範例:fminsearch01.m x0 = 0, 0, 0;points(x, points), p0); Its Better to accept the 處理與分析 多變數。

13、函數的極小值範例 n若目標函數為 我們必須先產生一個 MATLAB 的函數 objpped in local minima Quiz! MATLAB 程式設計進階篇:一般數學函數的od for continuous objective function nCould be traient or derivative needed nWeakness nSlow nOnly goStraightforward concept nEasy programming nNo grad分析 Downhill Simplex Search nAbout DSS nStrengths non nContra。

14、ction nShrink MATLAB 程式設計進階篇:一般數學函數的處理與unction, with the operations: nReflection nExpansiic Steps nUse a simplex to explore the objective flex Search nDSS in Wikipedia nMany variations nBasdata points externally Data matrix of 2xn MATLAB method MATLAB 程式設計進階篇:一般數學函數的處理與分析 Downhill Simpplex Search (D。

15、SS), aka nNelder-Mead method nAmoebaivative free Less efficient nMethod: Downhill Sim始點,讓 fminsearch 求出在起始點 附近的局部最小值(Local Minima) nDer小值: fminsearch nfminsearch 指令 n求多變數函數的極小值 n必須指定一個起ue = 11.2693 MATLAB 程式設計進階篇:一般數學函數的處理與分析 多變數函數的極 OPTIONS.TolX of 1.000000e-001 x = 0.6444 minValcurrent x satisfies。

16、 the termination criteria usingabolic Optimization terminated successfully: the 4646 parabolic 6 0.677749 11.7353 par程式設計進階篇:一般數學函數的處理與分析 DSS: Example 2 (3/3) MATLAB44416 11.2693 parabolic 5 0.611083 11.den 3 0.465248 25.1714 golden 4 0.62.9098 initial 2 0.732624 13.7746 golunt x f(x) Procedure 1 0.。

17、567376 1 MATLAB 程式設計進階篇:一般數學函數的處理與分析 放鬆誤差管制 (2) Func-co, 0.1); x, minValue = fminbnd(humps, 0.3, 1, opt)例8-16:fminbnd03.m opt = optimset( disp, iter, TolXbnd 提早傳回計算結果 n由 optimset 達成 n下例將 x 的誤差範圍提高為 0.1 n範AB 程式設計進階篇:一般數學函數的處理與分析 放鬆誤差管制 (1) n放鬆誤差管制 n使 fmin000000e-004 x = 0.6370 minValue = 11.2528 MATL。

18、 程式設計進階篇:一般數學函數的處理與分析 DSS: Example 3 (1/2) nObject the termination criteria using OPTIONS.TolX of 1. terminated successfully: the current x satisfies 9 0.637052 11.2528 parabolic Optimizationparabolic 8 0.637019 11.2528 parabolic 11.2529 parabolic 7 0.636985 11.2528 0.6413 11.2583 parabolic 6 0.6376。

19、18 den 4 0.644416 11.2693 parabolic 5 13.7746 golden 3 0.465248 25.1714 gol 0.567376 12.9098 initial 2 0.732624 10-4 Func-count x f(x) Procedure 1 Search) n拋物線內插法 (Parabolic Interpolation) nx 值誤差小於ive function nminDist2points02.m nMain program: ngf(x) n最後一欄位列出求極 小值的方法,包含 n黃金分割搜尋 (Golden Section :一般數。

20、學函數的處理與分析 尋求最小值的中間過程 (2) n左表列出x值的變化 及相對的函數值 alue = fminbnd(humps, 0.3, 1, opt) MATLAB 程式設計進階篇2.m opt = optimset(disp, iter);% 顯示每個步驟的結果 x, minV示選項 n再將 optimset 傳回結構變數送入 fminbnd n範例8-15:fminbnd0與分析 尋求最小值的中間過程 (1) n尋求最小值的中間過程 n使用 optimset 指令來設定顯370 minValue = 11.2528 MATLAB 程式設計進階篇:一般數學函數的處理528 x, min。

21、Value = fminbnd(humps, 0.3, 1) x = 0.6中的最小值 n範例:fminbnd01.m n最小值發生在 x = 0.637,且最小值為 11.2函數的處理與分析 單變函數的最小化 n fminbnd 指令 n尋求 humps 在 0.3, 1 oMinDist2points02.m function p=minDist2points02(po 最佳化工具箱(Optimization Toolbox) MATLAB 程式設計進階篇:一般數學數函數的最小化: fminsearch n設定最佳化的選項 n若讀者有興趣使用較複雜的方法,可以使用供了數個基本指令來進行數學。

22、函數 的優化,本節將介紹: n單變數函數的最小化: fminbnd n多變.8698 MATLAB 程式設計進階篇:一般數學函數的處理與分析 函數的優化 nMATLAB 提ad(integrand, xMin, xMax, yMin, yMax) result = -9; xMax = 2*pi; yMin = 0; yMax = pi; result = dblquax, yMin, yMax, quadl) result = -9.8696 xMin = piquadl,可執行下列指令 result = dblquad(integrand, xMin, xMquad01.m n一般的情況下。

23、dblquad 會呼叫 quad 計算定積分。若須呼 叫更為精確的 的上界值 MATLAB 程式設計進階篇:一般數學函數的處理與分析 雙重積分 (3) n範例:dblints, x0, showPlot) % minDist2points: Compute the 積分的下界值 nxMax:內迴圈積分的上界值 nyMin:外迴圈積分的下界值 nyMax:外迴圈積分( integrand, xMin, xMax, yMin, yMax) n其中 nxMin:內迴圈進階篇:一般數學函數的處理與分析 雙重積分 (2) n計算雙重積分 result = dblquadrand(x, y) out = 。

24、y*sin(x) + x*cos(y); MATLAB 程式設計integrand.m type integarnd.m function out = integ重積分 (1) ndblquad 指令 n用來計算雙重積分 n欲計算 n其中 n先建立被積分的函數 *pi) len = 17.2220 MATLAB 程式設計進階篇:一般數學函數的處理與分析 雙(t).2+1); n曲線長度可計算如下 len = quad(curveLength, 0, 3n out = curveLength(t) out = sqrt(4*cos(2*t).2+sin) n先定義函數 curveLength.m。

25、 type curveLength.m functiox, y, -o); MATLAB 程式設計進階篇:一般數學函數的處理與分析 產生更密的 X 座標點 (1) n若欲產生更密的 x 座標點,可在 fplot 指令加入 另一個輸入引數,已指定相對容忍n函數變化劇烈處,產生 緊密的點 x, y = fplot(humps, -1,2); plot(度(Tolerance) n範例:fplot04.m subplot (2,1,1); fplot(x)sin(1./x), 0.01,0.1); subplot (2,1,2); fplot(x示 fplot 所產生的 x 座標點 n範例:fplo。

26、t03.m n函數變化平緩處,產生 稀疏的點 )sin(1./x), 0.01,0.1, 0.0001); MATLAB 程式設計進階篇:一般數學函數的處理與分析 產生更密的 X 座標點 (2) n在第一圖中,fplot 指令使用預設相對容忍度lot 可進行描點作圖,類似 plot(x, y),但x 座標點的密度根據函數值的變化決定 n我們顯,其值 為 0.002。 n在第二圖中,相對容忍度被設為 0.0001,可得到更 準確的圖形,但也要花更多計算及作圖時間。 MATLAB 程式設計進階篇:一般數學函數的處理與分析 ezplot 指令xible! MATLAB 程式設計進階篇:一般數學函數的。

27、處理與分析 產生 X、Y 座標點 nfp nezplot指令和fplot指令類似,可進行描點作圖 ,但使用更為簡便,預設的作圖範圍為 n範例8-7:ezplot01.m ezplot(x)x3-x2+x); MATLAB 程式設計進階篇:一般數學函數的處理與分析 平面中的參數式曲線 nezplot 也可畫出平面中的參數式曲線 n範例b*x), sin(c*x), 0, 10) Function handle is more fle8-8:ezplot02.m n參數式函數的參數預設範圍仍是 ezplot(t)sin(3*t), (t)cos(5*t); 利薩如圖形 (Lissajous Fig。

28、ures) MATLA以便指定自變數 a=1; b=1.1; c=1.2; fplot(x)sin(a*x), sin(B 程式設計進階篇:一般數學函數的處理與分析 空間中的參數式曲線 nezplot3 可畫出空間中的參數式曲線 n範例8-8:ezplot021.m n參數式函數的參數預設範圍仍是 ezplot3(有參數的函數 n匿名函式也可以帶有參數 n範例:fplot023.m n此時 “(x)” 不可省略,(t)sin(3*t), (t)cos(5*t), (t)t) 3D利薩如圖形 MATLAB 程式設計進階篇:一般數學函數的處理與分析 隱函數作圖 nezplot 指令可用於隱函數作圖。

29、 n下列範例可x), exp(-x), 0, 10) MATLAB 程式設計進階篇:一般數學函數的處理與分析 帶以畫出 n範例8-9:ezplot03.m ezplot(x,y)x3+2*x2-3*x-y2+15); MATLAB 程式設計進階篇:一般數學函數的處理與分析 函數的求根 nfzero 指令 n用於單變數函數的求根 n語法 x = fzero(fun, x0) nfun 是欲求根的函數(以字串, exp(-x) 是二個行向量 n每個行向量代表一個函數(即一條曲線) fplot(x)sin(或函數握把來表示 ) nx0 是一個起始點或起始區間 MATLAB 程式設計進階篇:一般數學函。

30、數的處理與分析 X0 對 fzero 的影響 nfzero 指令根據 x0 不同而執行下列動作 n若 x個行向量來表示 n範例:fplot022.m nx 是行向量(MATLAB 預設值) nsin(x)0 為一個起始點 n fzero 會自動找出附近包含零點(即根,或函數 變號點)的區間 n逐步縮小此區間以找出零點 n若 fzero 無法找出此區間,傳回 NaN n若已知使函數值不同號的兩點 n由 一般數學函數的處理與分析 對多個函數作圖 nfplot 也可同時對多個函數作圖,其中每個函數 須以一x0 直接指定尋根的區間 n fzero 更快速找到位於此區間內的根 MATLAB 程式設計進階。

31、nterpolation 13 -0.131618 -9.76996e-015 in interpolation 12 -0.131618 8.88178e-016 i interpolation 11 -0.131618 -2.7935e-011 terpolation Zero found in the interval: -1, 1. a =2 interpolation 10 -0.131618 1.88371e-007 26907 interpolation 9 -0.131666 -0.001149-0.423446 interpolation 8 -0.132562 -0.02 。

32、-0.1316 MATLAB 程式設計進階篇:一般數學函數的處理與分析 數值積分 nMATLA115287 0.414441 bisection 7 -0.150214 -0.473635 -3.83767 interpolation 6 -0.B 可用於計算單變函數定積分 nquad:適應式 Simpson 積分法(Adaptive Simption 4 0.243062 71.6382 bisection 5 initial 3 -0.513876 -4.02235 interpola-1 -5.13779 initial 2 1 16 Ison Quadrature) nquadl:適應。

33、式 Lobatto 積分法(Adaptive Lo Func-count x f(x) Procedure 1 ction) n內插法 (Interpolation) n可由doc fzero找到 所使用的演算法batto Quadrature) MATLAB 程式設計進階篇:一般數學函數的處理與分析 定積分零點過程中,找 下一點的兩個方法 顯示在第四個欄位 (Procedure 欄位) n二分法(Bise MATLAB 程式設計進階篇:一般數學函數的處理與分析 顯示求解過程的中間結果 (2) n求 顯示每個 iteration 的結果 a = fzero(humps, -1, 1, opt)。

34、 n計算 humps 在 0, 1 的定積分 q = quad(humps, 0, 1) q = 定最佳化的選項,下一節會有比 較完整的介紹 opt = optimset(disp, iter);%et 傳回結構變數送入 fzero n範例8-13:fzero04.m noptimset 常用於設 29.8583 nquad 及 quad8 都應用遞迴程序 n若遞迴次數達 10 次,兩種方法均會AB 可以顯示求解過程的中間結果 n使用 optimset 指令來設定顯示選項 n再將 optims MATLAB 程式設計進階篇:一般數學函數的處理與分析 顯示求解過程的中間結果 (1) nMATL傳。

35、回 Inf n表示所計算之定積分可能不存在 nquad 及 quad8第四個引數用來指定積分的相對or, r); line(z2, humps(z2), marker, o, color, r); (humps, -1, 1); line(z1, humps(z1), marker, o, col-1, 2); grid on z1 = fzero(humps, 1.5); z2 = fzero誤 差容忍度 MATLAB 程式設計進階篇:一般數學函數的處理與分析 曲線的長度 (1) nquan若要畫出以上兩個零點,請見下列範例 n範例8-12:fzero03.m fplot(humps, = 。

36、0.000000 MATLAB 程式設計進階篇:一般數學函數的處理與分析 求根範例 (3) d 及 quadl 計算曲線的長度 n一曲線是由下列參數化的方程式來表示 n t 的範圍為 0, 3 fprintf(humps(%f) = %fn, x , y); humps(-0.131618)humps, -1, 1);% 求落於區間 -1, 1 的根 y = humps(x);% 帶入求值點 n範例8-11:fzero02.m n此時 fzero 找到的是另一個零點 x = fzero(*pi n範例:plotCurve.m t = 0:0.1:3*pi; plot3(sin(2*t) 在 x。

37、 = -1 及 1 間為異號 n令 x0 = -1, 1 為起始區間來找出 humps 的零0 MATLAB 程式設計進階篇:一般數學函數的處理與分析 求根範例 (2) n若已知 humps, cos(t), t); MATLAB 程式設計進階篇:一般數學函數的處理與分析 曲線的長度 (humps(%f) = %fn, x , y); humps(1.299550) = 0.00000% 求靠近 1.5 附近的根 y = humps(x); % 帶入求值 fprintf(2) n此曲線的長度等於 MATLAB 程式設計進階篇:一般數學函數的處理與分析 曲線的長度 (3及 1.6697),然後再。

38、找出 humps 的零 點 x = fzero(humps, 1.5); n範例8-10:fzero01.m nfzero 先找到在 1.5 附近變號的兩點(即 1.26 篇:一般數學函數的處理與分析 求根範例 (1) n找出humps在 x = 1.5 附近的根,並驗算point that has the min total if nargin2|isempty*x)+cos(x), -10, 10)% 使用函式握把來指定函式 MATLAB 程式設計進階篇: -10, 10)% 使用字串指定函式 subplot(2,1,2); fplot(x)sin(2(x0), x0=mean(point。

39、s, 2); end % Initial guess p=:fplot021.m subplot(2,1,1); fplot(sin(2*x)+cos(x),計進階篇:一般數學函數的處理與分析 匿名函式 nfplot 也接受匿名函式(當場指定的函式) n範例lot(humps, 0, 1, 5, 25); grid on% 畫出格線 MATLAB 程式設fminsearch(x)dist2points(x, points), x0); points和 y 的區間 n範例:fplot02.m nx 的區間為0, 1 ny 的區間為5, 25 fpTLAB 程式設計進階篇:一般數學函數的處理與分析。

40、 同時改變 x、y 的區間 n我們可同時改變 x =-1 -1; 3 0; 0 4; 1 1; 2 5; 4 2; p=minDist2points0fplot(humps, 0 2);% 使用函式握把來指定函式 Less flexible! MA,1); fplot(humps, 0,2);% 使用字串指定函式 subplot(2,1,2); 圖 n畫出 humps 函數在 0,2 間的曲線 n範例:fplot01.m subplot(2,12(points, , 1); fprintf(p=%sn, mat2str(p); Data mumps.m n字串:使用 humps 來代表 hum。

41、ps.m n用 fplot 指令進行數學函數作般數學函數的處理與分析 數學函數的作圖 n表示函數的方式 n函數握把:使用 humps 來代表 hatrix of 2xn Initial guess MATLAB 程式設計進階篇:一般數學函數的age We use 函數 to represent both. MATLAB 程式設計進階篇:一來表示subroutines or functions in a programming langu處理與分析 DSS: Example 3 (2/2) MATLAB 程式設計進階篇:一般數學函數的若要正名,可區分如下: n函數:通常用來表示mathemati。

42、c functions n函式:通常用mps:單輸入函數 npeaks:雙輸入函數 n函式和函數都代表functions,兩 者常會混用,TLAB 程式設計進階篇:一般數學函數的處理與分析 提示 nMATLAB 常被用到的測試函數 nhu處理與分析 本章指令彙整 類別指令名稱功能 作圖ezplot fplot 簡便的函數作圖 一般n欲顯示此檔案的位置 which humps n欲顯示此檔案的內容 type humps MA 一個函數 n例如,內建於MATLAB目錄的 humps.m 可用來計算 下列函數: n更多資訊: 的函數作圖 求根fzero單變數函數的求根 最佳化 fminbnd fmi。

43、nsearch 單變數函:一般數學函數的處理與分析 一維數學函數的範例 nMATLAB 是以 M 檔案(副檔名為 m)來表示 function) Functions of Functions! MATLAB 程式設計進階篇的函數? n字串 n函數握把 (function handles) n匿名函數 (anonymous數的最小值 多變數函數的最小值 數值積分(Quadrature) quad quad8 dblquan作圖 n求根 n優化:求函數的極大 或極小值 n數值積分 n求解微分方程式 n如何表示此種被分析 :一般數學函數的處理與分析 函數的函數 nMATLAB 可對數學函 數進行各種運算與分 析,例如: d 低精度的數值積分 高精度的數值積分 雙重(二維)積分 p:/mirlab.org/jang 台大資工系 多媒體檢索實驗室 MATLAB 程式設計進階篇MATLAB 程式設計進階篇 一般數學函數的處理與分析 張智星 jangmirlab.org htt。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值