【问题标题】:Until convergence using for loop? (Newton's)直到使用for循环收敛? (牛顿)
【发布时间】:2016-03-18 08:08:59
【问题描述】:

很明显,while 循环很容易用于运行直到收敛(牛顿),但是是否可以使用 for 循环?

while循环供参考:

maxIteration = 1000
tol = % some small number

while (n>0)
  % do something

  if n<tol % converges
    return;
  end;

  if n>maxIteration % diverges
    break;
  end;
end;

(不要介意语法)

【问题讨论】:

  • 我猜 return 和 break 也适用于 for 循环。如果有人有其他建议,请发表
  • 不,你明白了。如果您知道如何使用breakif 语句,您应该能够弄清楚如何将for 循环视为while 循环(反之亦然)。虽然我会说我多年来没有使用带有迭代方法的for 循环;我认为while 循环在语义上是优越的。

标签: matlab newtons-method


【解决方案1】:

你可以做一些事情

for n = 1:maxIteration
    % Do something

    % Converges
    if error < tol
        return;
    end
end

【讨论】:

  • 感谢您的回答。顺便说一句,您是否知道评估多个变量的语法?像 f(x,y) f 是一些向量,例如 [x^2 y^3] values = [1 2] 并且应该返回 ans = [1 8]
  • 你可以直接用其他相同大小的向量来评估向量,试试吧。即 a = [5 5] b = [4 6] a>b % 返回向量 [1 0] (true, false)
猜你喜欢
  • 2021-12-07
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
  • 1970-01-01
  • 2019-05-03
  • 1970-01-01
相关资源
最近更新 更多