确定的线性方程组在python中的求解 [英] solving under determined system of linear equation in python

查看:115
本文介绍了确定的线性方程组在python中的求解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下问题:

查找: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

Find: x_1,x_2,x_3 > 0 such that 67.5=60*x_1+90*x_2+120*x_3 and 60=30*x_1+120*x_2+90*x_3

有没有办法在Python中解决这个问题? (对于MATLAB,已经提出了类似的问题)

Is there a way to solve this equation in Python? (Similar question has already been asked for MATLAB)

有人可以举一个例子,说明如何在python中将scipy.nnls()用于任何欠定的方程组

Could anybody give an example of how to use scipy.nnls() in python for any underdetermined system of equation

推荐答案

使用sympy象征性地求解方程组

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}

使用 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

尽管如此,这仅承诺提供一个参数为x>=0的解决方案,因此您可以像本例中那样获得零.

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

这篇关于确定的线性方程组在python中的求解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆