2012-09-16 31 views
1

我需要在Matlab中編寫正確的Ridder方法。我必須定義功能Matlab Ridder的方法代碼

function [x sol, f at x sol, N iterations] = Ridders(f, x1, x2, eps f, eps x) 

我被給出的解釋是:

  1. 托架根部(X1,X2)

  2. 評估的中點(X1 + X2)/ 2

  3. 找到新的近似根

    x4 = x3 + sign(f1 - f2) [f3/((f3)^2 - f1f2)^1/2)](x3 - x1)

  4. 檢查x4是否滿足收斂條件。如果是的話,停下來。如果不是......

  5. 使用x4和X1,X2,X3或取其更接近根

  6. 循環回1

rebracket根我不知道如何在matlab中實現這個。幫幫我?

+1

提供您要實施的方法的詳細信息或公式。 –

+0

如果您發佈迄今爲止的代碼,您將獲得更多回復。不要擔心,如果它不完美。 – bdecaf

回答

1

在Matlab中,你將定義你的函數爲:

function [list of outputs] = myfunc(list of input variables) 

    %function definition to compute outputs using the input variables. 

在你的情況,如果你想X4(即根)是你的輸出,你會怎麼做:

function root = riddler(func, x1, x2, xaccuracy, N) 


    xl = x1; 
    xh = x2; 
    fl=func(x1) 
    fh=func(x2) 

    for i = 1:N 
     xm = 0.5*(xl+xh); 
     fm = func(xm); 
     s = sqrt(fm*fm - fl*fh) 
     if s == 0 
     return; 
     end 
     xnew = xm + (xm - xl)*sign(fl - fh)*fm/s %update formula 
     . 
     . 
     . % extra code to check convergence and assign final answer for root 
     . % code to update xl, xh, fl, fh, etc. (i.e rebracket) 
     . 

    end % end for 
0

一些這可能有助於主要概念:

  • function handles(你需要這種格式提供F)
  • 有效的變量名(許多變量在定義名稱是無效的)
0

我寫了一個Matlab實現裏德的Matlab的文件交換方法:submission 54458。我已將以下代碼複製以供參考:

function xZero = rootSolve(func,xLow,xUpp) 
% XZERO = ROOTSOLVE(FUNC, XLOW, XUPP) 
% 
% FUNCTION: This function uses Ridder's Method to return a root, xZero, 
%  of func on the interval [xLow,xUpp] 
% 
% INPUTS: 
% func = a function for a SISO function: y = f(x) 
% xLow = the lower search bound 
% xUpp = the upper search bound 
% 
% OUTPUTS: 
% xZero = the root of the function on the domain [xLow, xUpp] 
% 
% NOTES: 
% 1) The function must be smooth 
% 2) sign(f(xLow)) ~= sign(f(xUpp)) 
% 3) This function will return a root if one exists, and the function is 
% not crazy. If there are multiple roots, it will return the first one 
% that it finds. 

maxIter = 50; 
fLow = feval(func,xLow); 
fUpp = feval(func,xUpp); 
xZero = []; 

tol = 10*eps; 

if (fLow > 0.0 && fUpp < 0.0) || (fLow < 0.0 && fUpp > 0.0) 
    for i=1:maxIter 
     xMid = 0.5*(xLow+xUpp); 
     fMid = feval(func,xMid); 
     s = sqrt(fMid*fMid - fLow*fUpp); 
     if s==0.0, break; end 
     xTmp = (xMid-xLow)*fMid/s; 
     if fLow >= fUpp 
      xNew = xMid + xTmp; 
     else 
      xNew = xMid - xTmp; 
     end 
     xZero = xNew; 
     fNew = feval(func,xZero); 
     if abs(fNew)<tol, break; end 

     %Update 
     if sign(fMid) ~= sign(fNew) 
      xLow = xMid; 
      fLow = fMid; 
      xUpp = xZero; 
      fUpp = fNew; 
     elseif sign(fLow) ~= sign(fNew) 
      xUpp = xZero; 
      fUpp = fNew; 
     elseif sign(fUpp) ~= sign(fNew) 
      xLow = xZero; 
      fLow = fNew; 
     else 
      error('Something bad happened in riddersMethod!'); 
     end 

    end 
else 
    if fLow == 0.0 
     xZero = xLow; 
    elseif fUpp == 0.0 
     xZero = xUpp; 
    else 
     error('Root must be bracketed in Ridder''s Method!'); 
    end 
end