Gautham Badhrinathan
Gautham Badhrinathan

Reputation: 2437

Converging to 2 different values

I have a piece of code that basically solves a system of 2 non-linear equations using a numeric approximation method.

Code:

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."

Output:

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.

The 2 equations are:

8cos(θ₁) + 10cos(θ₂) = 12.66
8sin(θ₁) + 10sin(θ₂) = 11.928

In both the blocks (Block 1 and Block 2), θ₁ and θ₂ both are set to 0 initially. Then one θ is substituted in one of the equations to find a value for the other θ. This 2nd θ is then substituted in the other equation to find a value for the first θ. This is done recursively, converging at θ₁ and θ₂.

In the first block, I start with substituting θ₂ as 0 and finding a value for θ₁. Vice-versa in the second block.

Now my question is, Why do I end up with 2 different solutions when the only difference between the blocks is the starting variable?

PS: I do understand that there would be 2 different solutions for the given set of equations. What I don't understand is the reason for arriving at the 2 different solutions just because I'm using different starting variables.

PPS: I did try starting with different initial values for θ₁ and θ₂ instead of 0. That didn't change anything.

Upvotes: 1

Views: 144

Answers (2)

Bruno Kim
Bruno Kim

Reputation: 2360

You can analyze your methods as a kind of fixed-point method. A fixed-point method is one such that

v_{n+1} = f(v_{n})

In your case

v = (θ₁,θ₂)

and you rearranged your equations such that

f(v) = (acos(x₂ - l₂*cos(θ₂))/l₁, acos(y₂ - l₁*cos(θ₁))/l₂)

...more or less. As you use the already updated variable at the second calculation, it is the same as if you started with another v0, one where the second calculated variable is "one step ahead" the other. In the first case, your starting position is (0,acos(y₂ - l₁)/l₂), and in the second in the starting position (acos(x₂ - l₂)/l₁, 0). Despite what you said in your post-post-scriptum, it is a case of converging to different roots with different initial values.

It's hard to state why this happens. The basin of attraction of a root may have a weird boundary, as shown in the Newton-Raphson page in Wikipedia. You could try plotting the basins, selecting lots of initial starting points in your (θ₁,θ₂) domain and painting pixels of different colors depending to where they converge to.

Upvotes: 0

HYang
HYang

Reputation: 31

if the following 2 expressions are not same,
instead of acos (x2 - l2 * Math.cos theta2) / l1 , acos ((x2 - l2 * Math.cos theta2) / l1) looks right.

applies to other 3 expressions also.

Upvotes: 3

Related Questions