python - Solving linear equation with four variables
问题描述
Problem: I need to solve these equations with Python.
a + 3b + 2c + 2d = 1
2a + b + c + 2d = 0
3a + b + 2c + d = 1
2a + c + 3d = 0
So I can get the value for a, b, c and d. Is there a way that I can show them in a fraction?
My code:
import numpy as np
A = np.array([[1,3,2,2],[2,1,1,2]])
B = np.array([1,0,1,0])
X2 = np.linalg.solve(A,B)
Error:
LinAlgError: Last 2 dimensions of the array must be square
解决方案
You didn't add the last two equations of your problem to the A
matrix:
import numpy as np
A = np.array([[1,3,2,2],[2,1,1,2],[3,1,2,1],[2,0,1,3]])
B = np.array([1,0,1,0])
X2 = np.linalg.solve(A,B)
Gives:
array([-0.27272727, -0.18181818, 1.09090909, -0.18181818])
This should work.
推荐阅读
- html - 响应式按钮从水平布局变为垂直布局时如何添加空间?
- regex - Perl - 从一个字符串返回多个“相同”匹配
- android - Legend.setPosition() is not a valid method?
- python - AttributeError:“NoneType”对象没有属性“create_rectangle”
- mysql - 如何使用 Sequelize 使用我的时区存储正确的日期时间
- python - 将列表中的一个元素与列表中的所有其他元素相除
- javascript - while 循环中的 Promise 不会运行 - 导致无限循环
- java - 从 Oracle 数据库返回大型数据集的 Spring Boot 服务
- c# - 在哪里存储 .NET Core 3.0 应用程序所需的字符串?
- tensorflow - 如何将 tfrecord 数据集加载到 keras 模型中