# SymPy: solving a system of equation and replacing the values in the original expression

**URL:** https://discourse.julialang.org/t/sympy-solving-a-system-of-equation-and-replacing-the-values-in-the-original-expression/93653
**Category:** General Usage
**Tags:** symbolics, sympy
**Created:** [January 27, 2023, 5:59pm UTC](https://discourse.julialang.org/t/sympy-solving-a-system-of-equation-and-replacing-the-values-in-the-original-expression/93653 "2023-01-27T17:59:05Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [January 27, 2023, 5:59pm UTC](https://discourse.julialang.org/t/sympy-solving-a-system-of-equation-and-replacing-the-values-in-the-original-expression/93653/1 "2023-01-27T17:59:05Z")

</div>

Hello, I am trying to replicate this (old) simple example on symbolically finding the resolution of a system of equations and replacing an expression with the founded numerical solution:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/d/3d91c83a9ecc3c387467969b1ec4ee9d5d3682bf.png)

I arrived to it by manually recreating the dictionary, but is it not possible to have the symbol names from the output of `solve` as before ?

```julia
julia> using SymPy

julia> @vars x y w l1 l2
(x, y, w, l1, l2)

julia> lagr = 2x + 4y + y * w + l1 * (6 - 2x * w) + l2 * (9 - x - y * w)
l₁⋅(-2⋅w⋅x + 6) + l₂⋅(-w⋅y - x + 9) + w⋅y + 2⋅x + 4⋅y

julia> dlx = diff(lagr,x)
-2⋅l₁⋅w - l₂ + 2

julia> dly = diff(lagr,y)
-l₂⋅w + w + 4

julia> dlw = diff(lagr,w)
-2⋅l₁⋅x - l₂⋅y + y

julia> dll1 = diff(lagr,l1)
-2⋅w⋅x + 6

julia> dll2 = diff(lagr,l2)
-w⋅y - x + 9

julia> sola = solve([dlx, dly, dlw, dll1, dll2],[x, y, w, l1, l2])
1-element Vector{NTuple{5, Sym}}:
 (39/8, 429/64, 8/13, -143/32, 15/2)

julia> solb = nonlinsolve((dlx, dly, dlw, dll1, dll2),(x, y, w, l1, l2))
⎧⎛ 429 -143 ⎞⎫
⎨⎜39/8, ───, 8/13, ─────, 15/2⎟⎬
⎩⎝ 64 32 ⎠⎭

julia> sol = Dict([k=>v for (k,v) in zip([x,y,w,l1,l2],sola[1])])
Dict{Sym, Sym} with 5 entries:
  w => 8/13
  l1 => -143/32
  y => 429/64
  x => 39/8
  l2 => 15/2

julia> z = lagr.evalf(subs=sol)
40.6875000000000

```

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [January 27, 2023, 6:06pm UTC](https://discourse.julialang.org/t/sympy-solving-a-system-of-equation-and-replacing-the-values-in-the-original-expression/93653/2 "2023-01-27T18:06:45Z")

</div>

ok, I have found this that is a bit more elegant:

```julia
julia> using SymPy

julia> @vars x y w l1 l2
(x, y, w, l1, l2)

julia> lagr = 2x + 4y + y * w + l1 * (6 - 2x * w) + l2 * (9 - x - y * w)
l₁⋅(-2⋅w⋅x + 6) + l₂⋅(-w⋅y - x + 9) + w⋅y + 2⋅x + 4⋅y

julia> varlist = [x, y,w, l1, l2]
5-element Vector{Sym}:
  x
  y
  w
 l₁
 l₂

julia> derlist = diff.(lagr,varlist)
5-element Vector{Sym}:
   -2⋅l₁⋅w - l₂ + 2
      -l₂⋅w + w + 4
 -2⋅l₁⋅x - l₂⋅y + y
         -2⋅w⋅x + 6
       -w⋅y - x + 9

julia> sol = Dict([k=>v for (k,v) in zip(varlist,solve(derlist,varlist)[1])])
Dict{Sym, Sym} with 5 entries:
  w => 8/13
  l1 => -143/32
  y => 429/64
  x => 39/8
  l2 => 15/2

julia> z = lagr.evalf(subs=sol)
40.6875000000000

```
