1

I have seen this amazing example. But I need to solve system with boundaries on X and F, for example:

    f1 = x+y^2 = 0
    f2 = e^x+ xy = 0
-5.5< x <0.18
2.1< y < 10.6
   # 0.15< f1 <20.5 - not useful for this example
   # -10.5< f2 < -0.16 - not useful for this example

How could I set this boundary constrains to fsolve() of scipy? Or may be there is some other method? Would You give me a Simple code example?

Community
  • 1
  • 1
Fruitty
  • 57
  • 2
  • 10
  • 1
    How can you solve for `f1 = 0` when there is a constraint `f1 > 0.15`? – cfh May 21 '15 at 17:40
  • Google: Scipy & constraint nonlinear optimization. Use e.g scipy.optimize.minimize – Moritz May 21 '15 at 18:35
  • Dear @Moritz, of course, I have searched for my question in Google before I asked You. But there were no useful solution for me, which I would be able to understood. Therefore I asked You for simple example. – Fruitty May 21 '15 at 21:08
  • @Moritz, And, for sure, I looked at the page of [scipy.optimize](http://docs.scipy.org/doc/scipy/reference/optimize.html), but What is the difference between .minimize and .fsolve? It looks, like .minimize is for problem like F=(f1,f2,..)->min (that is more seems to be (df/dx=0) problem), not for equation systems, as represented above. What is way to use .minimize for Non-LAE systems? – Fruitty May 21 '15 at 21:21
  • @cfh, thank you, I just was thinking about my own system-problem, and do not thought, what this simple example do not need any constraints to satisfy because it's f(x)=const problem. – Fruitty May 21 '15 at 21:25

2 Answers2

4

I hope this will serve you as a starter. It was all there.

import numpy as np
from scipy.optimize import minimize

def my_fun(z):
    x = z[0]
    y = z[1]

    f = np.zeros(2)
    f[0] = x + y ** 2
    f[1] = np.exp(x) + x * y
    return np.dot(f,f)

def my_cons(z):
    x = z[0]
    y = z[1]
    f = np.zeros(4)
    f[0] = x + 5.5
    f[1] = 0.18 - x
    f[2] = y - 2.1
    f[3] = 10.6 - y
    return f

cons = {'type' : 'ineq', 'fun': my_cons}
res = minimize(my_fun, (2, 0), method='SLSQP',\
           constraints=cons)
res

status: 0 success: True njev: 7 nfev: 29 fun: 14.514193585986144 x: array([-0.86901099, 2.1 ]) message: 'Optimization terminated successfully.' jac: array([ -2.47001648e-04, 3.21871972e+01, 0.00000000e+00]) nit: 7

EDIT: As a response to the comments: If your function values f1 and f2 are not zero you just have to rewrite the equations e.g:

f1 = -6 and f2 = 3

Your function to minimize will be:

def my_fun(z):
    x = z[0]
    y = z[1]

    f = np.zeros(2)
    f[0] = x + y ** 2 + 6
    f[1] = np.exp(x) + x * y -3
    return np.dot(f,f)
Moritz
  • 5,130
  • 10
  • 40
  • 81
  • But this does minimization, OP wants to solve a system of equation. How does this help? – cfh May 22 '15 at 16:49
  • So what ? Maybe I do not understand the concept of solving system of equations but he would like to find values for x and y which do fulfill the constraints and the system clearly should be minimized `f[0] = 0 and f[1] = 0`. You can write every system of equation in a way that the residual should be zero. – Moritz May 22 '15 at 18:47
  • Why do you think that (0,0) is the minimum of the system? The values f1 and f2 might get negative. – cfh May 22 '15 at 18:48
  • He wrote `f1=0` and `f2=0` – Moritz May 22 '15 at 18:49
  • You don't gain anything from rewriting it as a constrained optimization problem: if the optimization reaches 0 (which it doesn't in your case), then you have found a solution, but the constraints aren't active and you could have simply solved the equation without constraints anyway. If you don't reach 0, you haven't found a solution. – cfh May 22 '15 at 19:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78570/discussion-between-moritz-and-cfh). – Moritz May 22 '15 at 19:13
1

It depends on the system, but here you can simply check the constraints afterwards.

First solve your nonlinear system to get one/none/several solutions of the form (x,y). Then check which, if any, of these solutions, satisfy the constraints.

cfh
  • 4,576
  • 1
  • 24
  • 34