我只是处理新手而且比Java或Javascript更让我困惑! 我必须解决大学任务的联立方程。 (这是一个他们没有真正向我们解释他们在代码中做了什么的课程。) 我知道他们如何在下面的代码中用两个方程式来计算代码,但他们现在希望我们用3个方程来做。有谁知道我会怎么做?我想我只需要在每个矩阵中添加额外的位,但显然比这更复杂。 我有3个方程式:
X + Y + Z = 9 X + 2Y + 3Z = 23 X + 5Y-3Z = -7
两个方程的代码如下:
// import Jama.*;
// Solve 3x+2y=3
// -2x-y=-1
// AX=B
// X=InvA B
import java.io.StringWriter;
void setup()
{
size(150,110);
fill(0,0,0);
double [][] Aline12={{ 3, 2}, // Create a 2D array to store A
{-2,-1}};
Matrix A = new Matrix(Aline12); // Copy array to A Matrix data structure
double [][] B1ine12 = {{3}, // Create a 2D array to store B
{-1}};
Matrix B = new Matrix(B1ine12); // Copy array to B Matrix data structure
Matrix X=(A.inverse()).times(B); // Solve for X
text("A",10,12);
app_print(A,0,16);
text("B",110,12);
app_print(B,100,16);
text("X",10,65);
app_print(X,0,70);
}
// Method added to allow printing on applet screen at (x,y)
void app_print(Matrix P, int x, int y)
{
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);
P.print(writer,5,2);
text(stringWriter.toString(),x,y);
}
答案 0 :(得分:0)
你将以解决2个方程组的方式解决它,只需添加第三个变量。同样在实践中,你几乎从不想采用矩阵的逆,有更好的方法,如LU分解来解决Ax=B
。由于您使用的是Jama,您可以尝试以下代码段
double [][] Aline12={{1.0, 1.0, 1.0}, // Create a 2D array to store A
{1.0, 2.0. 3.0},
{1.0, 5.0, -3.0}};
Matrix A = new Matrix(Aline12); // Copy array to A Matrix data structure
double [][] B1ine12 = {{9}, // Create a 2D array to store B
{23},
{-7}};
Matrix B = new Matrix(B1ine12); // Copy array to B Matrix data structure
Matrix X = A.solve(B) // Solve for X. See Jama documentation on solve