使用二等分找到第一类贝塞尔函数(J0(x))的第n个根

时间:2018-09-27 05:37:43

标签: matlab iteration bisection bessel-functions

首先,我想澄清一下这是学校的作业,因此我不寻求解决方案。我只是想朝着正确的方向前进。

现在,解决问题。

我们有使用二等分求多项式根的代码:

function [root, niter, rlist] = bisection2( func, xint, tol )
% BISECTION2: Bisection algorithm for solving a nonlinear equation
%             (non-recursive).
%
% Sample usage:
%   [root, niter, rlist] = bisection2( func, xint, tol )
% 
%  Input:
%     func    - function to be solved
%     xint    - interval [xleft,xright] bracketing the root
%     tol     - convergence tolerance (OPTIONAL, defaults to 1e-6)
%
%  Output:
%     root    - final estimate of the root
%     niter   - number of iterations needed  
%     rlist   - list of midpoint values obtained in each iteration.

% First, do some error checking on parameters.
if nargin < 2
  fprintf( 1, 'BISECTION2: must be called with at least two arguments' );
  error( 'Usage:  [root, niter, rlist] = bisection( func, xint, [tol])' );
end
if length(xint) ~= 2, error( 'Parameter ''xint'' must be a vector of length 2.' ), end  
if nargin < 3, tol = 1e-6; end

% fcnchk(...) allows a string function to be sent as a parameter, and
% coverts it to the correct type to allow evaluation by feval().
func = fcnchk( func );

done  = 0;
rlist = [xint(1); xint(2)];
niter = 0;

while ~done

 % The next line is a more accurate way of computing
 % xmid = (x(1) + x(2)) / 2 that avoids cancellation error.
 xmid = xint(1) + (xint(2) - xint(1)) / 2;
 fmid = feval(func,xmid);

 if fmid * feval(func,xint(1)) < 0
   xint(2) = xmid;
 else
   xint(1) = xmid;
 end

 rlist = [rlist; xmid];
 niter = niter + 1;

 if abs(xint(2)-xint(1)) < 2*tol || abs(fmid) < tol
   done = 1;
 end
end

root = xmid;
%END bisection2.

我们必须使用此代码找到第一种Bessel函数(J0(x))的第n个零。插入一个范围然后找到我们要查找的特定根是非常简单的。但是,我们必须绘制Xn与n的关系图,为此,我们需要能够计算与n有关的大量根。因此,我编写了以下代码:

bound = 1000;
x = linspace(0, bound, 1000);
for i=0:bound
    for j=1:bound
        y = bisection2(@(x) besselj(0,x), [i,j], 1e-6)
    end
end

我相信这会行得通,但是它提供的根源并没有井井有条,并且不断重复。我相信的问题是我打电话给bisection2时的范围。我知道[i,j]并不是最好的方法,希望有人可以引导我朝正确的方向解决此问题。

谢谢。

1 个答案:

答案 0 :(得分:1)

您的实施方向正确,但并不完全正确。

bound = 1000;
% x = linspace(0, bound, 1000);  No need of this line.
x_ini = 0; n =1; 
Root = zeros(bound+1,100);  % Assuming there are 100 roots in [1,1000] range

for i=0:bound                                            
  for j=1:bound                                          
    y = bisection2(@(x) besselj(i,x), [x_ini,j], 1e-6);   % besselj(i,x) = Ji(x); i = 0,1,2,3,...
    if abs(bessel(i,y)) < 1e-6
       x_ini = y;                                         % Finds nth root
       Root(i+1,n) = y;
       n = n+1;
    end
  end
  n = 1;
end

我用besselj(i,x)替换了代码中的besselj(0,x)。这样不仅可以为J0(x)提供根,还可以为J1(x),J2(x),J3(x)等提供根。 (i = 0,1,2,...)

我在您的代码中进行的另一项更改是将[i,j]替换为[x_ini,j]。最初x_ini = 0和j = 1。这会尝试在间隔[0,1]中找到根。由于J0的第一个根出现在2.4,所以二等分函数计算出的根(0.999)实际上不是第一个根。 if ..... end之间的行将检查由Bisection函数找到的根是否实际上是根。如果是,则x_ini将采用根的值,因为在x = x_ini(或y)之后将出现下一个根。

相关问题