【问题标题】:Secant Method in WhileWhile 中的正割法
【发布时间】:2019-01-23 17:03:12
【问题描述】:

我在 for 循环中有一个割线方法,试图将其更改为 while 循环,但问题只是显示了一次迭代,我想让它像 for 循环但在 while 循环中。

For循环的代码:

    clc;clear;
    x0=1;
    x1=0;
    f=@(x) 3*x+sin(x)-exp(x)
    if abs(x0) <abs(x1)
        t=x0;
        x0=x1;
        x1=t;
    end
    for i=1:5
        x2=x1-(f(x1)*((x0-x1)/(f(x0)-f(x1))))
        disp([x0,  x1,  x2,  f(x2)]);
        x0=x1;
        x1=x2;
    end

这是我的 while 循环代码:

clc; clear;
 x0=1;
 x1=0;
 error=1
 tol=0.00001;
 f=@(x) 3*x+sin(x)-exp(x)
 if abs(f(x0)) < abs(f(x1))
     t=x0;
     x0=x1;
     x1=t;
 end
 x2=x1-(f(x1)*((x0-x1)/(f(x0)-f(x1))))
 while(abs(f(x2))<tol)

         disp(x1);

         x0=x1;
         x1=x2;
 end

【问题讨论】:

    标签: for-loop while-loop


    【解决方案1】:

    首先,您没有在循环内修改x2,因此您的while 循环将不会运行或无限循环。正如 ViG 指出的那样,您的逻辑条件是向后的,因为您想在错误大于容差时循环。此外,最好在 while 条件不收敛的情况下将最大循环计数添加到它。最后你定义了error,但从未使用它。因此,我通过将代码添加到循环中以提高可读性,从而更清楚地说明了代码在做什么。

    x0=1;
    x1=0;
    error=1;
    tol=0.00001;
    f=@(x) 3*x+sin(x)-exp(x);
    loopCount = 0;
    maxNumLoops = 100;
    
    if abs(f(x0)) < abs(f(x1))
        t=x0;
        x0=x1;
        x1=t;    
    end
    while(abs(error)>tol) && loopCount < maxNumLoops  
        loopCount = loopCount +1;
        x2=x1-(f(x1)*((x0-x1)/(f(x0)-f(x1))));
        error = f(x2);
        x0=x1;
        x1=x2;    
    end
    if loopCount >= maxNumLoops  
       disp('Method did not converge!!') 
    end
    

    【讨论】:

    • 非常感谢,我有一个牛顿法可以使用,但我不知道如何使用它,你也可以帮我吗?
    • 您可以将它作为一个新问题发布,我会在有时间的时候看看。否则其他人可能会先得到它。但是,您可能想使用本网站的搜索功能。这里已经有几个 Matlab Newton Raphson 方法问题,可能包含您正在寻找的内容。前任。 stackoverflow.com/a/5640295/1106634
    【解决方案2】:

    您不加入 while 循环的原因是因为 abs(f(x2))&lt;tol 不满意。我猜你想改进你的结果,直到下一步小于tol。在这种情况下,您应该使用while(abs(f(x2))&gt;tol),因为这样,循环将不断重复直到abs(f(x2))&lt;=tol。另外别忘了把x2的函数放在while循环里面,否则x2不会更新,会无限循环。

    【讨论】:

      【解决方案3】:

      我有以下代码,也许对你有帮助:

      secant <- function(fn, x0, x1, tol, max.iter){
      xn.min1 <- x0
      xn <- x1
      iter <- 1
      while((abs(xn-xn.min1) > tol) && (iter < max.iter)){
        xn.plus1 <- xn - f(xn)*(xn-xn.min1)/(f(xn)-f(xn.min1))
        xn.min1 <- xn
        xn <- xn.plus1
        iter <- iter + 1
      }
      if (abs(xn.plus1 - xn) > tol){
        cat("failed to converge\n")
        return(NULL)
      } else {
        return(xn.plus1)
      }
      

      【讨论】:

        【解决方案4】:

        您的停止条件有问题。请参阅我的代码以获取工作示例。

        x.n.minus.1 <- 0.5
        x.n <- 6
        iter <- 1
        while ((abs(x.n-x.n.minus.1) > tol) && (iter < max.iter)) {
          x.n.plus.1 <- x.n - f.acc(x.n)*(x.n-x.n.minus.1)/(f.acc(x.n)-f.acc(x.n.minus.1))
          x.n.minus.1 <- x.n
          x.n <- x.n.plus.1
          iter <- iter + 1
          cat("At iteration", iter, "value of x.n is:", x.n, "\n")
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多