why aren't the results the same?

i have to differnt sets of code but the only difference is how i create my A matrix

code 1

publicclass GaussEl2{

publicstaticvoid main(String[] args){

int N = 10;

double[][] A ={{ 12, -2, 1, 0, 0, 0, 0, 0, 0, 0},

{ -2, 12, -2, 1, 0, 0, 0, 0, 0, 0},

{ 1, -2, 12, -2, 1, 0, 0, 0, 0, 0},

{ 0, 1, -2, 12, -2, 1, 0, 0, 0, 0},

{ 0, 0, 1, -2, 12, -2, 1, 0, 0, 0},

{ 0, 0, 0, 1, -2, 12, -2, 1, 0, 0},

{ 0, 0, 0, 0, 1, -2, 12, -2, 1, 0},

{ 0, 0, 0, 0, 0, 1, -2, 12, -2, 1},

{ 0, 0, 0, 0, 0, 0, 1, -2, 12, -2},

{ 0, 0, 0, 0, 0, 0, 0, 1, -2, 12}

};

double[] b ={ 13.97, 5.93, -6.02, 8.32, -23.75, 28.45, -8.9, -10.5, 10.34, -38.74};

double [][] L =newdouble [10][10];

double [][] U =newdouble [10][10];

double [] b1 =newdouble [10];

// cpoying original A to U

for(int i = 0; i < 10; ++i)

for(int j = 0; j < 10; ++j)

U[i][j] = A[i][j];

// cpoying original b to b1

for(int i = 0; i < 10; ++i)

for(int j = 0; j < 1; ++j)

b1[i] = b[i];

// Gaussian elimination with partial pivoting

for (int p = 0; p < N; p++){

// find pivot row and swap

int max = p;

for (int i = p + 1; i < N; i++){

if (Math.abs(A[i][p]) > Math.abs(A[max][p])){

max = i;

}

}

double[] temp = U[p]; U[p] = U[max]; U[max] = temp;

doublet= b1[p]; b1[p] = b1[max]; b1[max] = t;

// pivot within A and b

for (int i = p + 1; i < N; i++){

L[i][p] = U[i][p] / U[p][p];

b1[i] -= L[i][p] * b1[p];

for (int j = p; j < N; j++){

U[i][j] -= L[i][p] * U[p][j];

}

}

}

// back substitution

double[] x =newdouble[N];

for (int i = N - 1; i >= 0; i--){

double sum = 0.0;

for (int j = i + 1; j < N; j++){

sum += U[i][j] * x[j];

}

x[i] = (b1[i] - sum) / U[i][i];

}

// print results

System.out.println("This is my A:\n");

for (int i = 0; i < N; i++){

for(int j = 0; j < N; ++j)

System.out.print(A[i][j]+"\t");

System.out.println();

}

System.out.println("\nThis is my b:\n");

for (int i = 0; i < N; i++){

System.out.println(b[i]);

System.out.println();

}

System.out.println("\nThis is my x:\n");

for (int i = 0; i < N; i++){

System.out.println(x[i]);

System.out.println();

}

System.out.println("\nThis is my L:\n");

for(int i = 0; i < 10; ++i){

for(int j = 0; j < 10; ++j)

System.out.print(Math.round((L[i][j])*10000)/10000.0+"\t");

System.out.println();

}

System.out.println("\nThis is my U:\n");

for(int i = 0; i < 10; ++i){

for(int j = 0; j < 10; ++j)

System.out.print(Math.round((U[i][j])*10000)/10000.0+"\t");

System.out.println();

}

}

}

code 2

publicclass GaussEl{

publicstaticvoid main(String[] args){

int N = 10;

double[][] A =newdouble [10][10];

double[] b ={ 13.97, 5.93, -6.02, 8.32, -23.75, 28.45, -8.9, -10.5, 10.34, -38.74};

double [][] L =newdouble [10][10];

double [][] U =newdouble [10][10];

double [] b1 =newdouble [10];

for (int i=0; i<=9; i++){

A[i][i] = 12;

}

for (int i=0; i<=8; i++){

A[i][i+1] = -2;

}

for (int i=1; i<=9; i++){

A[i][i-1] = -2;

}

for (int i=0; i<=7; i++){

A[i][i+1] = -1;

}

for (int i=2; i<=9; i++){

A[i][i-1] = -1;

}

// cpoying original A to U

for(int i = 0; i < 10; ++i)

for(int j = 0; j < 10; ++j)

U[i][j] = A[i][j];

// cpoying original b to b1

for(int i = 0; i < 10; ++i)

for(int j = 0; j < 1; ++j)

b1[i] = b[i];

// Gaussian elimination with partial pivoting

for (int p = 0; p < N; p++){

// find pivot row and swap

int max = p;

for (int i = p + 1; i < N; i++){

if (Math.abs(A[i][p]) > Math.abs(A[max][p])){

max = i;

}

}

double[] temp = U[p]; U[p] = U[max]; U[max] = temp;

doublet= b1[p]; b1[p] = b1[max]; b1[max] = t;

// pivot within A and b

for (int i = p + 1; i < N; i++){

L[i][p] = U[i][p] / U[p][p];

b1[i] -= L[i][p] * b1[p];

for (int j = p; j < N; j++){

U[i][j] -= L[i][p] * U[p][j];

}

}

}

// back substitution

double[] x =newdouble[N];

for (int i = N - 1; i >= 0; i--){

double sum = 0.0;

for (int j = i + 1; j < N; j++){

sum += U[i][j] * x[j];

}

x[i] = (b1[i] - sum) / U[i][i];

}

// print results

System.out.println("This is my A:\n");

for (int i = 0; i < N; i++){

for(int j = 0; j < N; ++j)

System.out.print(A[i][j]+"\t");

System.out.println();

}

System.out.println("\nThis is my b:\n");

for (int i = 0; i < N; i++){

System.out.println(b[i]);

System.out.println();

}

System.out.println("\nThis is my x:\n");

for (int i = 0; i < N; i++){

System.out.println(x[i]);

System.out.println();

}

System.out.println("\nThis is my L:\n");

for(int i = 0; i < 10; ++i){

for(int j = 0; j < 10; ++j)

System.out.print(Math.round((L[i][j])*10000)/10000.0+"\t");

System.out.println();

}

System.out.println("\nThis is my U:\n");

for(int i = 0; i < 10; ++i){

for(int j = 0; j < 10; ++j)

System.out.print(Math.round((U[i][j])*10000)/10000.0+"\t");

System.out.println();

}

}

}

[13490 byte] By [crislee11a] at [2007-11-27 0:45:45]
# 1
Are they supposed to be the same ?How are the results different ?error ? wrong numbers ? etc ?How does the matrix impact all the calculations ?
Aknibbsa at 2007-7-11 23:11:16 > top of Java-index,Java Essentials,Java Programming...
# 2
also, consider using variable names that are longer then 3 chars long, it helps with the readability.
mkoryaka at 2007-7-11 23:11:16 > top of Java-index,Java Essentials,Java Programming...
# 3
also, are you trying to copy someones homework and failing?
mkoryaka at 2007-7-11 23:11:16 > top of Java-index,Java Essentials,Java Programming...
# 4
my numbers are slightly or totaly off and i'm missing numbers in my matirces. the first code has correct results.
crislee11a at 2007-7-11 23:11:16 > top of Java-index,Java Essentials,Java Programming...
# 5
> also, are you trying to copy someone's homework and failing?no, i am not copying someone's homework. i want to my program to create my matrix for me instead of declaring my matrix in the beginning.
crislee11a at 2007-7-11 23:11:16 > top of Java-index,Java Essentials,Java Programming...
# 6

> > also, are you trying to copy someone's homework and

> failing?

>

> no, i am not copying someone's homework. i want to

> my program to create my matrix for me instead of

> declaring my matrix in the beginning.

So you are saying that 2 programs are the same except one of them is going to create the information that many calculations are based on where the other will be using static data - sounds pretty odd to me.

Aknibbsa at 2007-7-11 23:11:16 > top of Java-index,Java Essentials,Java Programming...
# 7
yes i know it's odd but i want to see if i can work that way as well.
crislee11a at 2007-7-11 23:11:16 > top of Java-index,Java Essentials,Java Programming...