nan*_*itv 6 python linear-algebra
考虑以下问题:
查找:x_1,x_2,x_3> 0,使得67.5 = 60*x_1 + 90*x_2 + 120*x_3和60 = 30*x_1 + 120*x_2 + 90*x_3
有没有办法在Python中解决这个等式?(类似的问题已经被要求用于MATLAB)
任何人都可以举例说明如何在python中使用scipy.nnls()来获得任何未确定的方程组
用sympy符号解方程组
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}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
尽管这仅承诺了一个解决方案,其中参数是x>=0这样你可以获得零,就像你在这个例子中所做的那样。
| 归档时间: |
|
| 查看次数: |
3669 次 |
| 最近记录: |