1

我需要在 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

我不知道如何在 matlab 中实现这一点。帮助?

4

3 回答 3

1

在 Matlab 中,您可以将函数定义为:

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

    %function definition to compute outputs using the input variables.

在您的情况下,如果您希望 x4(即 root)成为您的输出,您可以:

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
于 2012-09-16T21:21:40.660 回答
0

一些可能有帮助的主要概念:

  • 函数句柄(您需要以这种格式提供 f)
  • 有效的变量名(定义中的许多变量名无效)
于 2012-09-17T08:28:29.340 回答
0

我在 Matlab 文件交换上编写了 Ridder 方法的 Matlab 实现:提交 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
于 2016-07-16T14:47:17.587 回答