我在Matlab代码中找不到错误。它只打印Hello World

时间:2015-12-11 16:14:45

标签: matlab

我需要使用割线方法找到函数-x ^ 3 + 3x ^ 2 - 4 * p的根,然后绘制它们。

function roots=secantRoot (funName,x0,x1,tol,counterMax)

%Basic Secant method for finding the root
% funName is name of a string refering to a function
% x0 and x1 are points near the area of the root
%tol is the maximum error
%counterMaX is the maximum number of iterations

    for i=1:counterMax
      funx0=feval(funName,x0);
      funx1=feval(funName,x1);
      xi=x1-funx1*(x1-x0)/(funx1-funx0);

      if abs((xi - x1)/x1)<tol
        roots =xi;
        break
      end

      x0=x1;
      x1=xi;
    end

  if i= counterMax
    fprintf('No solution found.")
    fun = @(x)(-x^3+3x^2 - 4*p);
    data[];
    depth=secantRoot (fun,0,1,0.001,100)
    data'= [data; p data];
  end

plot ( data[:1], data[:2])

1 个答案:

答案 0 :(得分:0)

我猜:

if i= counterMax
    fprintf('No solution found.")
    fun = @(x)(-x^3+3x^2 - 4*p);
    data[];
    depth=secantRoot (fun,0,1,0.001,100)
    data'= [data; p data];
  end

应该是:

if i == counterMax
    fprintf('No solution found.")
    fun = @(x)(-x^3+3x^2 - 4*p);
    data[];
    depth=secantRoot (fun,0,1,0.001,100)
    data'= [data; p data];
  end