Julia equivalent for python scipy.optimize.fsolve

There a several options, I think, but the NLsolve.jl package is one possibility:

julia> using NLsolve

julia> function F!(F, x)
         F[1] = 1 - x[1] - x[2]
         F[2] = 8 - x[1] - 3x[2]
       end

julia> result = nlsolve(F!, [1.0,1.0], autodiff=:forward)
Results of Nonlinear Solver Algorithm
 * Algorithm: Trust-region with dogleg and autoscaling
 * Starting Point: [1.0, 1.0]
 * Zero: [-2.5, 3.5]
 * Inf-norm of residuals: 0.000000
 * Iterations: 2
 * Convergence: true
   * |x - x'| < 0.0e+00: false
   * |f(x)| < 1.0e-08: true
 * Function Calls (f): 3
 * Jacobian Calls (df/dx): 3

julia> result.zero
2-element Vector{Float64}:
 -2.5
  3.5

Of course, if you know that your equations are linear, you should really put them into some kind of matrix and use \:

julia> [1 1; 1 3] \ [1, 8]
2-element Vector{Float64}:
 -2.5
  3.5

but perhaps your real problem is nonlinear and you intended this F as a toy problem to figure out the library?

6 Likes