[問題] 請教一個funtion裡面想再呼叫一個function出來解的問題已回收

看板MATLAB作者時間16年前 (2009/03/24 01:44), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串1/1
請教一個funtion裡面想再呼叫一個function出來解的問題 主要是參考一本書上的code利用fixed point去求實數解 而如果要求解,只需呼叫code1 (附在最底下)的m file就可以 我試過可以求出-((x-1).^2-3)/2 gb=inline('-((x-1).^2-3)/2','x'); [x,err,xx]=fixpt(gb,1,1e-4,50); 可以解得其收斂在1.414 沒有問題~ 但是如果我想把"-((x-1).^2-3)/2"這個要解的函數寫再另外一個m file變成一個 function再呼叫code1去解可以嗎>"< 我有試過把inline那邊改成我要呼叫的function名稱,但是不行>"< 因為如果可以另外寫在一個m file我要改一些東西比較方便~ 不知道是否有人可以給我一點指點>"< 謝謝~~ code1: function [x,err,xx] = fixpt(g,x0,TolX,MaxIter) % solve x = g(x) starting from x0 by fixed-point iteration. %input : g,x0 = the function and the initial guess % TolX = upperbound of incremental difference |x(n + 1) - x(n)| % MaxIter = maximum # of iterations %output: x = point which the algorithm has reached % err = last value |x(k) - x(k - 1)| achieved % xx = history of x if nargin < 4, MaxIter = 100; end if nargin < 3, TolX = 1e-6; end xx(1) = x0; for k = 2:MaxIter xx(k) = feval(g,xx(k - 1)); %Eq.(4.1.3) err = abs(xx(k) - xx(k - 1)); if err < TolX, break; end end x = xx(k); if k == MaxIter fprintf('Do not rely on me, though best in %d iterations\n',MaxIter) end -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.162.108

03/24 21:48, , 1F
找到了 寫的函式前加個@就可以了XD
03/24 21:48, 1F
文章代碼(AID): #19nygAFb (MATLAB)
文章代碼(AID): #19nygAFb (MATLAB)