Hi guys, i am currently working on jacobi transformation program. my program is supposed to find max value of 2 dimension matrix and transformed it using sin & cos matrix transformation. it will produce new matix with new max value on it. the process is repeated again until all non-diagonal element close to zero.
i wrote my program like this (u can skip to read the whole progam. i ask my queation in the end of this post ) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
#include <iostream>
#include <algorithm>
#include <cmath>
#include <iterator>
#include <ctime>
#include <iomanip>
#define phi 3.14
#define tol 0.001
using namespace std;
int const dimensi = 3;
double matrik[dimensi][dimensi]= {{12,6,-6},{6,16,2},{-6,2,16}};
double k, jacobi[dimensi][dimensi], jacobi_baru[dimensi][dimensi],data[dimensi][dimensi],rotasi[dimensi][dimensi],rotasi_in[dimensi][dimensi];
double maks;
double nilai,nilai_abs,bar,kol,teta,sudut,s,c,t,penyebut,delta,coteta;
int main ()
{
for (int k=0;k<3;k++)
{
maks = 0;
cout <<endl<< "matriks : " << endl;
// matriks
for (int i =0;i<dimensi;i++)
{
for (int j=0;j<dimensi;j++)
{
cout << matrik[i][j] << " ";
}
cout << endl;
}
cout << endl;
//finding max value
for (int i=0;i<dimensi;i++)
{
for (int j=0;j<dimensi;j++)
{
if (i!=j)
{
nilai_abs = sqrt (matrik[i][j]*matrik[i][j] ) ;
cout << "index= " << i<< "," <<j<<" value : " << nilai_abs;
if (nilai_abs > maks)
{
maks = matrik[i][j];
}
else {
maks=maks;
}
cout << " maks value: " << maks<<endl;
}
}
}
nilai = maks;
cout <<" max value: " << nilai<<endl<<endl;
for (int a=0;a<dimensi;a++)
{
for (int b=0;b<dimensi;b++)
{
if ( matrik[a][b] == nilai)
{
bar =a;
kol = b;
}
}
}
//jacobi transformation
int a=bar,b=kol;
cout << bar << " and " << kol << endl;
cout << matrik[a][b] <<endl<<endl;
penyebut = matrik[a][a]-matrik[b][b];
delta = penyebut/(2.0*(matrik[b][a]));
if (delta >= 0 )
{
t = 1 / ( delta + sqrt( (delta*delta) +1 ) );
}
else
{
t = 1 / ( delta + sqrt( (delta*delta) +1 ) );
}
c = 1 / ( sqrt( 1 + (t*t) ) );
s = c*t;
cout << " delta = " << delta << " angle value= " << sudut << endl;
cout << " nilai t= " << t << " sin= " << s << " cos= " <<c<< endl;
rotasi[b][b] = c;
rotasi[a][a] = c;
rotasi[b][a] = s;
rotasi[a][b] = -s;
rotasi_in[b][b] = c;
rotasi_in[a][a] = c;
rotasi_in[b][a] = -s;
rotasi_in[a][b] = s;
for (int i=0;i<dimensi;i++)
{
for (int j=0;j<dimensi;j++)
{
if(i==j)
{
if(i!=a && i!=b)
{
rotasi[i][j] = 1;
}
}
}
}
for (int i=0;i<dimensi;i++)
{
for (int j=0;j<dimensi;j++)
{
if(i==j)
{
if(i!=a && i!=b)
{
rotasi_in[i][j] = 1;
}
}
}
}
cout << "Matrik rotasi: " << endl;
for (int i=0;i<dimensi;i++)
{
for (int j=0;j<dimensi;j++)
{
cout << fixed;
cout << setprecision(2) << rotasi[i][j] << setw(4) << " ";
}
cout << endl;
}
cout << endl<<endl<<"Matrik rotasi inverse: " << endl;
for (int i=0;i<dimensi;i++)
{
for (int j=0;j<dimensi;j++)
{
cout << fixed;
cout << setprecision(2) << rotasi_in[i][j] << setw(4) << " ";
}
cout << endl;
}
cout<<endl << "jacobi : " << endl;
for (int i=0;i<dimensi;i++)
{
for (int j=0;j<dimensi;j++)
{
for (int k=0;k<dimensi;k++)
{
jacobi[i][j] += rotasi_in[i][k]*matrik[k][j];
}
cout <<fixed;
cout << setprecision(2) << jacobi[i][j] << setw(4) << " ";
}
cout << endl;
}
//print out jacobi
cout<<endl << " new jacobi : " << endl;
for (int i=0;i<dimensi;i++)
{
for (int j=0;j<dimensi;j++)
{
for (int k=0;k<dimensi;k++)
{
jacobi_baru[i][j] += jacobi[i][k]*rotasi[k][j];
}
cout <<fixed;
cout << setprecision(2) << jacobi_baru[i][j] << setw(4) << " ";
}
cout << endl;
}
//change the old matrix with new jacobi's matrix
cout<<endl << "data diagonalnya adalah: " << endl;
for (int i=0;i<dimensi;i++)
{
for (int j=0;j<dimensi;j++)
{
data[i][j] = jacobi_baru[i][j];
cout << fixed;
cout << setprecision(2) << data[i][j] << setw(4) << " ";
matrik [i][j] = data[i][j];
}
cout << endl;
}
}
}
|
in the first loop it works as it supposed to be. but the problem appear in the second loops when i try to find max value of matrix.
1. in the first loops :
matrix : 12 6 -6
6 16 2
-6 2 16
max value of each element ()
index : 0,1 value(in abs form) : 6 maks value :6
index : 0,2 value : 6 maks value :6
index : 1,0 value : 6 maks value :6
index : 1,2 value : 2 maks value :6
index : 2,0 value : 6 maks value :6
index : 2,1 value : 2 maks value :6
max value = 6
2.in the second loops :
matrix : 7.68 0 -6.04
0 20.32 -1.89
-6.04 -1.89 16.00
max value of each element :
index : 0,1 value : 0.00 maks value :-0.00
index : 0,2 value : 6.04 maks value :-6.04
index : 1,0 value : 0.00 maks value :-0.00
index : 1,2 value : 1.89 maks value :-1.89
index : 2,0 value : 6.04 maks value :-6.04
index : 2,1 value : 1.89 maks value :-1.89
max value = -1.89
on the second looping, the max value indicate the last value (not a max value anymore) in looping.
i dont understand, It work well in the first process but not in the second looping and so on.
Is there something i have missed?
Thanks,
P.S : i am pretty sure my problem not in the jocobi trans method. but in the finding max value in looping matrix.