-1

I'm only new to processing and it's confusing me far more than Java or Javascript ever did! I have to solve simultaneous equations for a college assignment. (It's a class where they don't really explain to us what they're doing in the code.) I know how they figure out the code with two equations in the code below, however they now want us to do it with 3 equations. Does anyone know how I would do this? I imagined I would just have to add the extra bits into each matrix but it is obviously more complicated than that. The 3 equations I have are:

x+y+z=9 x+2y+3z=23 x+5y-3z=-7

The code for two equations is the following:

// 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); 
}
Jessie
  • 47
  • 7

1 Answers1

0

You will solve it the same way you solve a system of 2 equations, just add the third variable. Also in practice you almost never want to take the inverse of a matrix, there are better methods like LU decomposition to solve Ax=B. Since you are using Jama, you can try the following snippet

   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

Jama docs
http://math.nist.gov/javanumerics/jama/doc/

StaticBeagle
  • 5,070
  • 2
  • 23
  • 34