# How to solve these equations?

**URL:** <https://discourse.julialang.org/t/how-to-solve-these-equations/92788>\
**Category:** General Usage\
**Tags:** sympy\
**Created:** [January 11, 2023, 2:22am UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788 "2023-01-11T02:22:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 2:22am UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/1 "2023-01-11T02:22:02Z")

</div>

```julia
using SymPy
@syms x y z e₁ e₂ c₁ c₂ c₃ c₅ s₁ s₂ s₃ g₁ g₂
fx = x*(x - 1)*(c₁ + c₂*y*z - c₂*y - c₂*z - e₁ - e₂*y*z + e₂*y + 
e₂*z - g₁*y - g₂*z - s₁*y)
fy = y*(y - 1)*(c₃ + g₁*x - g₁ + s₁*x)
fz = -z*(z - 1)*(-c₅ + s₂ + s₃)
solve([fx, fy, fz], [x, y, z])

```

It runs long long time, but no results.

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 4:24am UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/2 "2023-01-11T04:24:15Z")

</div>

Anyone can help me to solve this problem?

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 1:09pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/3 "2023-01-11T13:09:44Z")

</div>

help

---

<div class="post-metadata">

**Author:** ![abraunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraunst/32/6880_2.png) [@abraunst](https://discourse.julialang.org/u/abraunst)\
**Post date:** [January 11, 2023, 1:25pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/4 "2023-01-11T13:25:56Z")

</div>

You have at least the 8 solutions with x,y,z\in\{0,1\}. There are other solutions with x=\frac{c₃ - g₁}{ s₁ +g₁}…

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 11, 2023, 2:15pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/5 "2023-01-11T14:15:51Z")

</div>

Note please that this is really not a Julia question per se, since all SymPy.jl does is call out to Python.

I let it run in the background and forgot all about it, but the solve did finish. **Edit:** But it took twenty minutes.

```julia
julia> @time solve([eq1,eq2,eq3],[x,y,z], domain=SymPy.sympy.S.Reals)
1193.533067 seconds (29.31 M allocations: 333.714 GiB, 0.06% gc time)
10-element Vector{Tuple{Sym, Sym, Sym}}:
 (0, 0, 0)
 (0, 0, 1)
 (0, 1, 0)
 (0, 1, 1)
 (1, 0, 0)
 (1, 0, 1)
 (1, 1, 0)
 (1, 1, 1)
 (-(c₃ - g₁)/(g₁ + s₁), (c₁ - e₁)/(c₂ - e₂ + g₁ + s₁), 0)
 (-(c₃ - g₁)/(g₁ + s₁), (c₁ - c₂ - e₁ + e₂ - g₂)/(g₁ + s₁), 1)

```

Your `fx,...` are called `eq1,...` here, and I asked for real solutions. Although that probably won’t make a difference, because no assumptions about the other variables are made.

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 2:27pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/6 "2023-01-11T14:27:42Z")

</div>

Thanks, This result is what I want. But I can not get this result. The solve can not finish. I run for one day, but it did not finish.

 ![截屏2023-01-11 22.27.17](https://global.discourse-cdn.com/julialang/original/3X/d/d/ddb23d5de4b144ebbc05eb50f8dc310106663dd1.png)

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 2:29pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/7 "2023-01-11T14:29:17Z")

</div>

Can I use other package to solve this problem?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 11, 2023, 2:33pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/8 "2023-01-11T14:33:46Z")

</div>

Why are solutions calculated only for z=0 and z=1?  
It would seem that there are many other (infinite?) solutions.

PS  
I just tried to do a quick calculation by hand.

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 11, 2023, 2:35pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/9 "2023-01-11T14:35:25Z")

</div>

It seems easier to solve them by hand to be honest.

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 2:51pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/10 "2023-01-11T14:51:53Z")

</div>

In matlab, it can solve them very quickly. But why Julia can’t?

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 2:55pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/11 "2023-01-11T14:55:37Z")

</div>

> [@skleinbo](#):
>
> `, domain=SymPy.sympy.S.Reals`

I have run for 1 hour, it still not finish.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 11, 2023, 2:59pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/12 "2023-01-11T14:59:16Z")

</div>

Again, you are not really using Julia here, you are calling SymPy which is a Python library:

[https://www.sympy.org/en/index.html](https://www.sympy.org/en/index.html)

You might have more luck asking about SymPy performance issues on a SymPy forum.

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 3:00pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/13 "2023-01-11T15:00:11Z")

</div>

Thanks , but How can I use Julia to solve this problem?

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 3:00pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/14 "2023-01-11T15:00:54Z")

</div>

Can Julia solve this problem ?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 11, 2023, 3:01pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/15 "2023-01-11T15:01:51Z")

</div>

I’ve never used anything but Wolfram tools for symbolic manipulation, but you can try

> **[GitHub - JuliaSymbolics/Symbolics.jl: Symbolic programming for the next...](https://github.com/JuliaSymbolics/Symbolics.jl)**
>
> Symbolic programming for the next generation of numerical software - GitHub - JuliaSymbolics/Symbolics.jl: Symbolic programming for the next generation of numerical software

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 3:05pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/16 "2023-01-11T15:05:34Z")

</div>

I try to use Symbolics.jl, but It seems that Symbolics can only solve linear equations.

```julia
julia> using Symbolics

julia> @syms x y z e₁ e₂ c₁ c₂ c₃ c₅ s₁ s₂ s₃ g₁ g₂
(x, y, z, e₁, e₂, c₁, c₂, c₃, c₅, s₁, s₂, s₃, g₁, g₂)

julia> fx = x*(x - 1)*(c₁ + c₂*y*z - c₂*y - c₂*z - e₁ - e₂*y*z + e₂*y +
       e₂*z - g₁*y - g₂*z - s₁*y)
x*(x - 1)*(c₁ + e₂*y + e₂*z + c₂*y*z - e₁ - c₂*y - g₁*y - s₁*y - c₂*z - g₂*z - e₂*y*z)

julia> fy = y*(y - 1)*(c₃ + g₁*x - g₁ + s₁*x)
y*(y - 1)*(c₃ + g₁*x + s₁*x - g₁)

julia> fz = -z*(z - 1)*(-c₅ + s₂ + s₃)
-z*(z - 1)*(s₂ + s₃ - c₅)

julia> Symbolics.solve_for([fx, fy, fz], [x, y, z])
ERROR: AssertionError: islinear
Stacktrace:
 [1] solve_for(eq::Vector{SymbolicUtils.Mul{Number, Int64, Dict{Any, Number}, Nothing}}, var::Vector{SymbolicUtils.Sym{Number, Nothing}}; simplify::Bool, check::Bool)
   @ Symbolics ~/.julia/packages/Symbolics/FGTCH/src/linear_algebra.jl:100
 [2] solve_for(eq::Vector{SymbolicUtils.Mul{Number, Int64, Dict{Any, Number}, Nothing}}, var::Vector{SymbolicUtils.Sym{Number, Nothing}})
   @ Symbolics ~/.julia/packages/Symbolics/FGTCH/src/linear_algebra.jl:96
 [3] top-level scope
   @ REPL[6]:1

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 11, 2023, 3:18pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/17 "2023-01-11T15:18:02Z")

</div>

> [@rocco\_sprmnt21](#):
>
> Why are solutions calculated only for z=0 and z=1?  
> It would seem that there are many other (infinite?) solutions.
> 
> PS  
> I just tried to do a quick calculation by hand.

okay. I looked better. everything’s clear

if -c5+s2+s3=/=0 it is ok.

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 3:28pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/18 "2023-01-11T15:28:07Z")

</div>

Can you help me ? how to use Julia to solve this problem. many thanks

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [January 11, 2023, 4:04pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/19 "2023-01-11T16:04:54Z")

</div>

How can I use Symbolics,jl to solve this problem?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 11, 2023, 4:29pm UTC](https://discourse.julialang.org/t/how-to-solve-these-equations/92788/20 "2023-01-11T16:29:29Z")

</div>

Try rewriting the equations like this.

```julia
julia> @syms x y z A B C D a b c
(x, y, z, A, B, C, D, a, b, c)

julia> eq1=x*(x-1)*(A*y+B*z+C*z*y+D)
x⋅(x - 1)⋅(A⋅y + B⋅z + C⋅y⋅z + D)

julia> eq2=y*(y-1)*(a*x+b)
y⋅(y - 1)⋅(a⋅x + b)

julia> eq3=z*(z-1)*c
c⋅z⋅(z - 1)

julia> @time solve([eq1,eq2,eq3],[x,y,z], domain=SymPy.sympy.S.Reals)
  1.450638 seconds (257 allocations: 8.133 KiB)
10-element Vector{Tuple{Sym, Sym, Sym}}:
 (0, 0, 0)
 (0, 0, 1)
 (0, 1, 0)
 (0, 1, 1)
 (1, 0, 0)
 (1, 0, 1)
 (1, 1, 0)
 (1, 1, 1)
 (-b/a, -D/A, 0)
 (-b/a, (-B - D)/(A + C), 1)

```

[Next page](https://discourse.julialang.org/t/how-to-solve-these-equations/92788.md?page=2)
