Consider the following problem:
Find:
x_1, x_2, x_3 > 0
such that67.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()
?
Consider the following problem:
Find:
x_1, x_2, x_3 > 0
such that67.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()
?
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.