6

Consider the following problem:

Find: x_1, x_2, x_3 > 0 such that

67.5 = 60*x_1 +  90*x_2 + 120*x_3  
60   = 30*x_1 + 120*x_2 +  90*x_3

Is there a way to solve this equation in Python? Perhaps with scipy.nnls()?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
nantitv
  • 3,539
  • 4
  • 38
  • 61
  • I think this is the non negative least square problem. Please giv ur valuable comments – nantitv Feb 23 '14 at 16:53
  • You've got three variables (x_1, x_2, and x_3) but only two equations. As it stands, their solution set will be equivalent to the "line" formed by the intersection of the planes of the two equations. In other words, there are infinitely many solutions, currently. – voithos Feb 23 '14 at 17:06
  • 1
    @voithos yes it have infinitely many solution. But I heard that still find we can find the solution of this under determined system using least-square fitting.In my case I require nonnegative least square solution. pls see http://stackoverflow.com/questions/16365723/find-positive-solutions-to-underdetermined-linear-system-of-equations for ref. Thnx for ur comment – nantitv Feb 23 '14 at 17:27

1 Answers1

6

Using sympy to solve the equation set symbolically

from sympy import * 

x_1, x_2, x_3 = symbols('x_1 x_2 x_3')

res = solve([Eq(60*x_1+90*x_2+120*x_3, 67.5),
             Eq(30*x_1+120*x_2+90*x_3, 60)],
             [x_1, x_2, x_3])
print res
#{x_1: -1.4*x_3 + 0.6, x_2: -0.4*x_3 + 0.35}

using scipy.optimize.nnls

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import nnls 

A = np.array([[60, 90, 120], 
              [30, 120, 90]])

b = np.array([67.5, 60])

x, rnorm = nnls(A,b)

print x
#[ 0.          0.17857143  0.42857143]
print rnorm
#0.0

Altough this only promises a solution where the parameters are x>=0 so you can get zeros, as you did for this example.

M4rtini
  • 13,186
  • 4
  • 35
  • 42
  • 1
    Thnx for the sugestion. But here I have to analyze output to satisfy the non-negative constraint. But I require the next advance version of this which will give me the one of the possible answer. Once again thnxs for ur help – nantitv Feb 23 '14 at 17:43
  • Could anybody give an example of how to use scipy.nnls() in python for any underdetermined system of equation – nantitv Feb 23 '14 at 18:55
  • 1
    @nantitv updated with solution using scipy.nnls, but it might not work for you since it can give 0 values. and you wanted >0. – M4rtini Feb 23 '14 at 19:53
  • great man. I owe you one. I could not find a good documentation about nnls. Could u suggest one? what is rnorm? at what condition solution becoming zero?Is there a property that a,b should follow? – nantitv Feb 24 '14 at 06:20
  • I don't know of any other documentation other than the one linked in the answer. It says it's a wrapper for some fortran solver, so you might be able to find documentation for that. – M4rtini Feb 24 '14 at 10:42