【发布时间】:2012-09-19 16:14:55
【问题描述】:
我有一段代码,它基本上使用数值逼近法解决了一个由 2 个非线性方程组成的系统。
代码:
l1 = 8
l2 = 10
x2 = 12.66
y2 = 11.928
maxError = 1e-30
maxIterations = 100
theta = 1: 0, 2: 0
theta1 = 0
theta2 = 0
i = 0
loop # Block 1
i++
theta1 = Math.acos (x2 - l2 * Math.cos theta2) / l1
theta2 = Math.asin (y2 - l1 * Math.sin theta1) / l2
break if Math.sqrt(Math.pow(theta[1] - theta1, 2) + Math.pow(theta[2] - theta2, 2)) <= maxError or i is maxIterations
theta = 1: theta1, 2: theta2
console.log "Converged to first solution {theta1: #{theta1 * 180 / Math.PI}, theta2: #{theta2 * 180 / Math.PI}} in #{i} iterations."
theta = 1: 0, 2: 0
theta1 = 0
theta2 = 0
i = 0
loop # Block 2
i++
theta2 = Math.acos (x2 - l1 * Math.cos theta1) / l2
theta1 = Math.asin (y2 - l2 * Math.sin theta2) / l1
break if Math.sqrt(Math.pow(theta[1] - theta1, 2) + Math.pow(theta[2] - theta2, 2)) <= maxError or i is maxIterations
theta = 1: theta1, 2: theta2
console.log "Converged to second solution {theta1: #{theta1 * 180 / Math.PI}, theta2: #{theta2 * 180 / Math.PI}} in #{i} iterations."
输出:
Converged to first solution {theta1: 60.004606260047474, theta2: 29.99652810779697} in 34 iterations.
Converged to second solution {theta1: 26.584939314539064, theta2: 56.593017466789554} in 35 iterations.
这两个方程是:
8cos(θ₁) + 10cos(θ₂) = 12.66
8sin(θ₁) + 10sin(θ₂) = 11.928
在两个块(Block 1 和 Block 2)中,θ₁ 和 θ₂ 最初都设置为 0。然后将一个 θ 代入其中一个方程以找到另一个 θ 的值。然后将第二个 θ 代入另一个方程以找到第一个 θ 的值。这是递归完成的,收敛于θ₁ 和θ₂。
在第一个块中,我首先将θ₂ 替换为0 并为θ₁ 找到一个值。在第二个区块中反之亦然。
现在我的问题是,当块之间的唯一区别是起始变量时,为什么我最终会得到 2 种不同的解决方案?
PS:我知道对于给定的方程组会有 2 种不同的解。我不明白的是,仅仅因为我使用了不同的起始变量,就得出了两种不同的解决方案。
PPS:我确实尝试从 θ₁ 和 θ₂ 的不同初始值开始,而不是 0。这并没有改变任何东西。
【问题讨论】:
标签: math coffeescript numerical-methods numerical-analysis