# Converting MultivariatePolynomials output as a function to be used in JuMP

The objective is to solve a large system of NL equations. Equations should be obtained via polynomial operations and passed on to JuMP as user-defined function. Below is the minuaturised code for eveluating polynomials’ variables as function’s arguments:
`using JuMP`
`using Ipopt`
`using MultivariatePolynomials`
`using DynamicPolynomials`
`function f(k_1,c_1)`
`c_1*k_1^2`
`end`
`@polyvar c_1 k_1 s`
`d=2*k_1^2+c_1-s`
`p5=subs(d,s=>f(k_1,c_1))`
`function y_2(c,k)`
`p5(c_1=>c,k_1=>k)`
`end`
`y_2(c,k)`
The error message is the following when I call for the evaluated function:
`UndefVarError: c not defined`

Please, what is wrong in the code? How differently can I convert MultivariatePolynomials output into user-defined functions usable into JuMP?

The variables `c` and `k` do not have any value in your code.
You get the same error if you do

``````function y_2(c, k)
c + y
end
y_2(c, k)
``````

What do you intend to do with the line `y_2(c, k)` ?

y_2(c,k) is going to be used as user-defined function in JuMP with `c` and `k` the unknowns of the non-linear system to be solved.

When I register `y_2(c,k)` as a user defined function in the real model, I receive the following error:

``````  `AssertionError: Variable c_1 was not assigned a value`

`Stacktrace:`

`_subsmap(::MultivariatePolynomials.Eval,::Array{DynamicPolynomials.PolyVar{true},1},::Tuple{Pair{DynamicPolynomials.PolyVar{true},Float64},`
`Pair{DynamicPolynomials.PolyVar{true},Float64},Pair{DynamicPolynomials.PolyVar{true},Float64},Pair{DynamicPolynomials.PolyVar{true},Float64},Pair{DynamicPolynomials.PolyVar{true},Float64},.....`
``````

It is difficult to tell you what is the issue without a minimal working example but it could be cause by calling `@polyvar` twice. Indeed, in DynamicPolynomials, the equality between variables is not based on the name, each variable is different. For instance,

``````using JuMP
using Ipopt
using MultivariatePolynomials
using DynamicPolynomials
function f(k_1,c_1)
c_1*k_1^2
end
@polyvar c_1 k_1 s
d=2*k_1^2+c_1-s
p5=subs(d,s=>f(k_1,c_1))
@polyvar c_1
function y_3(c,k)
p5(c_1=>c,k_1=>k)
end
m = Model()
@variable m c
@variable m k
y_3(c, k)
``````

throws the error `AssertionError: Variable c_1 was not assigned a value` because the `c_1` using in `p5(c_1=>c,k_1=>k)` is different than the `c_1` used in `d=2*k_1^2+c_1-s` and `p5=subs(d,s=>f(k_1,c_1))`.

I have just added a notebook detailing how to solve polynomial optimization here that might be helpful.

2 Likes

Thanks a lot! Going to have a look now.

I ran
`Pkg.build()`
and no error message related to the conversion of MultivariatePolynomials output to a function anymore.