为代码添加边界,matlab

为代码添加边界,matlab,matlab,matlab-guide,numerical-analysis,numerical-computing,Matlab,Matlab Guide,Numerical Analysis,Numerical Computing,我正试图为代码添加边界,但却很难找到将它们放在哪里。方程式如下:f(x)=e^(6x)+1.441e^(2x)− 2.079e^(4x)− 0.333=0,-1>=x我将在牛顿步之后添加检查。这并不能防止有人输入-1或1作为初始猜测,但这可以在您进行输入验证时完成。我采纳了安德·比古里的建议,将c改为x: function x = newton(x0, delta) x = x0; fx = f(x0); fprintf('initial

我正试图为代码添加边界,但却很难找到将它们放在哪里。方程式如下:f(x)=e^(6x)+1.441e^(2x)− 2.079e^(4x)− 0.333=0,-1>=x我将在牛顿步之后添加检查。这并不能防止有人输入-1或1作为初始猜测,但这可以在您进行输入验证时完成。我采纳了安德·比古里的建议,将
c
改为
x

function x = newton(x0, delta)
  x = x0;    
  fx = f(x0);                   
  fprintf('initial guess:  x=%f, fx=%f\n',x,fx)

  if abs(fx) <= delta             % check to see if initial guess satisfies
        return;                       % convergence criterion.
  end;

  while abs(fx) > delta,
    fpx = fprime(x);

    if fpx==0,                    % if fprime is 0, abort.
      error('fprime is 0')        % the error function prints message and exits
    end;

    x = x - fx/fpx;               % Newton step
    if( x > 1 || x < -1 )
      error('x out of bounds!');
    end

    fx = f(x);
    fprintf('   x=%f, fx=%f\n',x,fx)
  end
end

function fx = f(x)
  fx = exp(6*x)+1.441*exp(2*x)-2.079*exp(4*x)-0.333;         % Enter your function here.
end

function fprimex = fprime(x)
  fprimex = 6*exp(6*x)+6*exp(2*x)*(log(2))^2-4*exp(4*x)*log(8); % Enter the derivative of function
end

如果将变量名
c
更改为
x
,您可能会更容易理解!安德的建议:干得好,谢谢!您在命令窗口中输入什么来获取输出,由于某种原因,当我插入initila点等等时,我一直得到一个错误。也许我打错了。
function x = newton(x0, delta)
  x = x0;    
  fx = f(x0);                   
  fprintf('initial guess:  x=%f, fx=%f\n',x,fx)

  if abs(fx) <= delta             % check to see if initial guess satisfies
        return;                       % convergence criterion.
  end;

  while abs(fx) > delta,
    fpx = fprime(x);

    if fpx==0,                    % if fprime is 0, abort.
      error('fprime is 0')        % the error function prints message and exits
    end;

    x = x - fx/fpx;               % Newton step
    if( x > 1 || x < -1 )
      error('x out of bounds!');
    end

    fx = f(x);
    fprintf('   x=%f, fx=%f\n',x,fx)
  end
end

function fx = f(x)
  fx = exp(6*x)+1.441*exp(2*x)-2.079*exp(4*x)-0.333;         % Enter your function here.
end

function fprimex = fprime(x)
  fprimex = 6*exp(6*x)+6*exp(2*x)*(log(2))^2-4*exp(4*x)*log(8); % Enter the derivative of function
end
>> x = newton(0.5, 0.5*1e-4)
initial guess:  x=0.500000, fx=8.307733
   x=0.375798, fx=2.908518
   x=0.263566, fx=1.003444
   x=0.165026, fx=0.340291
   x=0.081315, fx=0.113210
   x=0.012704, fx=0.036909
   x=-0.041514, fx=0.011793
   x=-0.082894, fx=0.003695
   x=-0.113515, fx=0.001136
   x=-0.135581, fx=0.000341
   x=-0.151084, fx=0.000098
   x=-0.161526, fx=0.000025

x =

   -0.1615