TriposG
TriposG

Reputation: 113

Passing a Sympy expression to Scipy.optimize.minimize

I am trying to optimize the following function

fun=-300.49858410695*C_0 - 301*C_1 - 60.2000000000003*C_2

But when I pass this expression to the scipy.optimize.minimize function I am getting errors.

TypeError: 'Add' object is not callable

Here is the whole code that I have tried

x0=[1,1,1]
scipy.optimize.minimize(fun,x0,method="Nelder-Mead")

I understand that the problem lies with passing sympy expressions as function but is there any way to tackle this problem?

Upvotes: 0

Views: 544

Answers (1)

Learning is a mess
Learning is a mess

Reputation: 8277

You need to create a function out of your expression by lambdifying it.

from sympy import lambdify
fun = lambdify( (C_0,C_1,C_2), fun))
...

Upvotes: 1

Related Questions