0

二分法の数値解のグラフをプロットし、実際の解にどのように近づくかを示したいと思います。私のコード:

% f=input('please enter the function:');
%  xL=input('please enter the limits .from:');
%  XR=input('to:');
% eps=input('please enter epsilon:');
f=@(x)x.^2-1;
XR=2;
xL=-2;
XL=xL ;
eps=0.001;
subplot(2,1,1);
title('graph 1');
    ezplot(f);
    hold on ;
    % line([0 0],40);
    % line(-40,[0 0]);
    plot(XR,f(XR),'r*');
    plot(xL,f(xL),'r*');
    disp( 'the answers is : ');
    for df=xL:0.15:XR
        if f(xL)*f(df)<= 0
        xR=df;
        Roots = BisectionM(f,xR,xL,eps);
        plot(Roots,f(Roots),'gs');
        disp(Roots);
        xL=df;
        xR=XR;
        end
    end
subplot(2,1,2);
title('graph 2');
x0=fzero(f,xL);
sol = BisectionM(f,xR,xL,eps);
plot(1:1:100,ones(1,100)*x0);
hold on ;
plot(1:1:100,sol);

関数 :

function[sol,Roots] = BisectionM(f,xR,xL,eps)
      while abs(xR - xL) > eps
             xM = (xR + xL) / 2;
             if (f(xL))*(f(xM)) > 0
                 xL = xM;
                 sol=xM;
                 plot(xL,f(xL),'.');
             else
                 xR = xM;
                 plot(xR,f(xR),'.');
                  sol=xM;
             end
             Roots = xM;

        end
    end

これをプロットする方法がわからないので、解に近づきます (最後の青い線)。誰でも?

4

1 に答える 1