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()
?
Least-squares parameter estimation can be applied to underdetermined just as to overdetermined linear problems. In fact, underdetermined prediction problems are generally more common than overdetermined filtering and adjustment problems.
An underdetermined linear system has either no solution or infinitely many solutions.
The overdetermined case occurs when the system has been overconstrained — that is, when the equations outnumber the unknowns. In contrast, the underdetermined case occurs when the system has been underconstrained — that is, when the number of equations is fewer than the number of unknowns.
If a set of vectors is linearly independent, then it corresponds to a system which is overdetermined or which has the same number of variables as equations. If a system is underdetermined, then it corresponds to a set of vectors which are linearly dependent.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With