JuliaNLSolvers / NLsolve.jl

Julia solvers for systems of nonlinear equations and mixed complementarity problems
Other
324 stars 66 forks source link

No support for non-square problems? #270

Open lieskjur opened 3 years ago

lieskjur commented 3 years ago

Hi, does NLsolve not support non-square problems? I ran into an error when trying to solve a problem with more equations than than unknown variables which I replicated on a small testing script.

using NLsolve
using Rotations

j = [0,1,0]

function f(ϕ,u)
    RotX(ϕ[1])*RotY(ϕ[2])*RotY(ϕ[3])*j - u
end

function g(ϕ,u)
    RotX(ϕ[1])*RotY(ϕ[2])*j - u
end

v = [0,cos(pi/3),sin(pi/3)]

f([pi/3;0;0],v)
nlsolve((x)->f(x,v),[pi/4;0;0])

g([pi/3;0],v)
nlsolve((x)->g(x,v),[pi/4;0])

with console output

julia> nlsolve((x)->g(x,v),[pi/4;0])                                                                 
ERROR: BoundsError: attempt to access 2-element Array{Float64,1} at index [1, 2, 3]

I am sure that the newton method is applicable to such problems so I do not see a reason why NLsolve would not handle it.

skleinbo commented 3 years ago

Similarly, the solver might not report convergence on problems with input dimension larger than output dimension.

F(x) = sin(x[1])*sin(x[2])/sqrt(1+x[1]^2+x[2]^2)
res = nlsolve(F, rand(2), autodiff=:forward, store_trace=true, extended_trace=true)

Results of Nonlinear Solver Algorithm
 * Algorithm: Trust-region with dogleg and autoscaling
 * Starting Point: [0.08081342223001275, 0.42525217505251844]
 * Zero: [4.175125734506499e-9, 0.4132526768794085]
 * Inf-norm of residuals: 0.425252
 * Iterations: 1000
 * Convergence: false
   * |x - x'| < 0.0e+00: false
   * |f(x)| < 1.0e-08: false
 * Function Calls (f): 4
 * Jacobian Calls (df/dx): 3

res.trace.states[end]
  1000     4.252522e-01     0.000000e+00
 * f(x): [0.0, 0.42525217505251844]
 * g(x): [0.3711470323262712 2.9868402029688e-9; 0.0 0.0]
 * x: [4.175125734506499e-9, 0.4132526768794085]
 * delta: 0.000877443722023774
 * rho: NaN
kungfugo commented 2 years ago

Hi, facing the same issue here. Is the package really restricted to square problems, or is there a workaround known?