Is there a reliable package for solving simple equation system?

Today I am working on finding the insections of a line and a infinite long cylinder. It is a simple math problem that can be solved by hand and very easy for Matlab. However, it looks like tough for Julia which is inspired by the so-called dissatisfactory Matlab. Given the equations of a 3-D line and a cylinder. I’ve tried several ways. Firstly, the Package HomotopyContinuation does not work for lines at some place and slope (e.g, a line from [47.8636, 0,0] to [49.3684,0,7.24951], and a 4 radius cylinder surrounding vector (0,1,0) and center on x=50, z=10). The Package NLsolver can only get one solution, while the proble definitely has two intersections. I searched a lot, but do not find a syntax to get all solutions . The SymPy is the third way I tried. There is still a obvious problem about the campability of SymPy on Julia. After I installed it, add or delete a space in code led to error. And for some very easy equations, it gave void solution (or no solution). Finally, I gave up and deduced the analytical solution by hand.

Welcome any helpful methods or suggestions for this basic and generic functionality.

  1. Please read: make it easier to help you. It will help you get answers in this forum if you read this and follow its recommendations. For example, post code for a minimum working example rather than just describing your problem.
  2. It’s too much to expect a nonlinear solver to find all solutions. There is no upper bound on the number, location, narrowness, degree, etc of the roots of arbitrary nonlinear functions. Instead solvers typically find a single solution near a given initial guess. Try running NLSolver with an initial guess near the other solution.
  3. Julia is sensitive to whitespace in a number of ways. You’ll need to provide more details (e.g. the code and where you changed whitespace) to get help on that.
2 Likes

Did you try BifurcationKit.jl? That’s the one that is designed to give multiple solutions?

https://bifurcationkit.github.io/BifurcationKitDocs.jl/dev/

https://bifurcationkit.github.io/BifurcationKitDocs.jl/dev/deflatedproblem/

1 Like

I tried using this and it seems to work, at least for this simple example.

using HomotopyContinuation 
@var x y z; 
f = System([x^2 + y^2 - 4, y+z, x+z]) 
julia> f = System([x^2 + y^2 - 4, y+z, x+z]) 
System of length 3
 3 variables: x, y, z

 -4 + x^2 + y^2
 y + z
 x + z

julia> result = solve(f)
Result with 2 solutions
=======================
• 2 paths tracked
• 2 non-singular solutions (2 real)
• random_seed: 0xa63443f0
• start_system: :polyhedral


julia> s=real(solutions(result))
2-element Vector{Vector{Float64}}:
 [-1.4142135623730951, -1.4142135623730951, 1.4142135623730951]
 [1.414213562373095, 1.414213562373095, -1.414213562373095]

julia> f = System([x^2 + y^2 - 8, y+z, x+z]) # construct system f
System of length 3
 3 variables: x, y, z

 -8 + x^2 + y^2
 y + z
 x + z

julia> result = solve(f)
Result with 2 solutions
=======================
• 2 paths tracked
• 2 non-singular solutions (2 real)
• random_seed: 0x077e0c91
• start_system: :polyhedral


julia> s=real(solutions(result))
2-element Vector{Vector{Float64}}:
 [2.0, 2.0, -2.0]
 [-2.0, -2.0, 2.0]

julia>        solutions(result)
2-element Vector{Vector{ComplexF64}}:
 [2.0 - 1.4349296274686127e-42im, 2.0 + 0.0im, -2.0 + 0.0im]
 [-2.0 + 0.0im, -2.0 + 0.0im, 2.0 + 0.0im]
2 Likes
julia> @var x y z; # declare the variables x, y and z

julia> A=[47.8636, 0,0]
3-element Vector{Float64}:
 47.8636
  0.0
  0.0

julia> B=[49.3684,0,7.24951]
3-element Vector{Float64}:
 49.3684
  0.0
  7.24951

julia> D=B.-A
3-element Vector{Float64}:
 1.504800000000003
 0.0
 7.24951

julia> f = System([(x-50)^2 + (z-10)^2 - 4,
       y, D[1]*z-D[3]*(x-A[1])])
System of length 3
 3 variables: x, y, z

 -4 + (-50 + x)^2 + (-10 + z)^2
 y
 1.5048*z - 7.24951*(-47.8636 + x)

julia> result = solve(f)
Result with 2 solutions
=======================
• 2 paths tracked
• 2 non-singular solutions (2 real)
• random_seed: 0xcdab3255
• start_system: :polyhedral


julia> real_solutions(result)
2-element Vector{Vector{Float64}}:
 [50.34813410654636, 0.0, 11.969467604165946]
 [49.53553132646704, 0.0, 8.054680270159523]

5 Likes