[1] C#:使用Math.NET库解决方程组
// test solver in Math.NET
var A = Matrix<double>.Build.DenseOfArray(new double[,] {
{1, 1, 3},
{2, 0, 4},
{-1, 6, -1}
});
Console.WriteLine(A);
var b = Vector<double>.Build.Dense(new double[] { 2, 19, 8 });
Console.WriteLine(b);
var x = A.Solve(b);//Math.NET
Console.WriteLine("Test Solver in Math.NET: " + x);
>> Test Solver in Math.NET: DenseVector 3-Double
34.5
5
-12.5
Press any key to continue . . .
[2] MATLAB中相同输入的结果:
A = [1 1 3; 2 0 4; -1 6 -1]
B = [2 19 8]
x = B/A
A =
1 1 3
2 0 4
-1 6 -1
B =
2 19 8
x =
1.0000e+00 2.0000e+00 3.0000e+00
[3]在Python中获取相同的输入并在numpy.linalg的帮助下:
In[10]:
import numpy as np
# matrix A
A = np.matrix ([[1, 1, 3],[2, 0, 4],[-1, 6, -1]])
# vector b
b = np.array([2, 19, 8])
b.shape = (3,1)
# attempt to solve Ax=b
z = np.linalg.solve(A,b)
z
Out[10]:
array([[ 34.5],
[ 5. ],
[-12.5]])
[4] C#(Math.NET)和Python的结果似乎相同,而MATLAB的结果大不相同,为什么会这样呢?
答案 0 :(得分:1)
C#和Python示例解决了等式A*x=b
,而MATLAB示例解决了x*A=b
。
通过转置B并使用A*x=b
代替\
,可以更改MATLAB示例以解决/
。
可以通过转置A来改变Math.NET(和Python)示例来解决x*A=b
,即A.Transpose().Solve(b)
而不是A.Solve(b)
。