Reputation: 547
I am trying to optimize a function to get it as close to zero as possible.
The function is:
def goal_seek_func(x: float) -> float:
lcos_list_temp = [energy_output[i] * x for i in range(life)]
npv_lcos_temp = npv(cost_capital, lcos_list_temp)
total = sum([cost_energy_capacity,
cost_power_conversion,
balance_of_plant,
cost_construction_commissioning,
npv_o_m,
npv_eol,
npv_cost_charging,
npv_lcos_temp,
])
return total
All the variables calculated previously in the code.
It is a linear equation, where as x
gets smaller, so does total
.
I am trying to find the value of x
where total
is as close to 0
as possible.
I have tried to use:
scipy.optimize.minimize_scalar(goal_seek_func)
but this clearly minimizes the equation to -inf
. I have read the docs, but cannot see where to define a target output of the function. Where can I define this, or is there a better method?
Upvotes: 0
Views: 644
Reputation: 7157
I am trying to find the value of x where total is as close to 0 as possible.
Then you want to solve the equation goal_seek_func(x) = 0
instead of minimizing goal_seek_func(x)
. See here for an explanation of why these two things are not the same. That being said, you can easily solve the equation by minimizing some vector norm of your objective function:
res = scipy.optimize.minimize_scalar(lambda x: goal_seek_func(x)**2)
If the objective value res.fun
is zero, res.x
solves your equation. Otherwise, res.x
is at least the best possible value.
Upvotes: 2