Created
July 12, 2014 04:20
-
-
Save Liooo/2825ab810004fdc99936 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<cmath> | |
#include<cstdlib> | |
#define N 10 | |
#define E 0.001 | |
using namespace std; | |
double getRand(){ | |
int sign; | |
if( (rand() % 10) < 5){ | |
sign = 1; | |
}else{ | |
sign = -1; | |
} | |
//cout << rand() % 10 * sign << "\n"; | |
return static_cast <double> (rand()) / static_cast <double> (RAND_MAX); | |
//return rand() % 10; | |
} | |
double getSum(double arr[]){ | |
double sum=0; | |
for(long i=0;i<N;i++){ | |
sum+=arr[i]; | |
} | |
return sum; | |
} | |
void printMatrix(double arr[N][N]){ | |
long i=0, j=0; | |
cout << "======\n" ; | |
for(i=0;i<N;i++) { | |
for(j=0;j<N;j++) { | |
cout << arr[i][j] << " "; | |
} | |
cout << "\n"; | |
} | |
cout << "======\n "; | |
} | |
int main() { | |
long i,j,m,n,l; | |
static double x[N],a[N][N],b[N],c; | |
double nU, nD, first; | |
srand((unsigned) time(NULL)); | |
for(i=0;i<N;i++) { | |
b[i] = getRand(); | |
} | |
for(i=0;i<N;i++) { | |
x[i]=0; | |
for(j=0;j<N;j++) { | |
a[i][j] = getRand(); | |
} | |
} | |
for(i=0;i<N;i++) { | |
a[i][i] = getSum(a[i]) + getRand() + 1; | |
} | |
printMatrix(a); | |
while(true){ | |
nU = nD = 0; | |
for(i=0;i<N;i++) { | |
c=b[i]; | |
for(j=0;j<N;j++) { | |
if(i!=j) { | |
c-=a[i][j]*x[j]; | |
} | |
} | |
x[i]=c/a[i][i]; | |
nU += fabs(c) * fabs(c); | |
nD += fabs(b[i]) * fabs(b[i]); | |
} | |
if (sqrt(nU) / sqrt(nD) < E){ | |
break; | |
} | |
} | |
cout<<"\n===== answer: \n"; | |
for(i=0;i<N;i++) { | |
cout<<"\nx("<<i<<") = "<<x[i]<<"\n"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<cmath> | |
#include<cstdlib> | |
#define N 300 | |
#define E 0.001 | |
using namespace std; | |
float getRand(){ | |
int sign; | |
if( (rand() % 10) < 5){ | |
sign = 1; | |
}else{ | |
sign = -1; | |
} | |
return static_cast <float> (rand()) / static_cast <float> (RAND_MAX); | |
} | |
float getSum(float arr[]){ | |
float sum=0; | |
for(long i=0;i<N;i++){ | |
sum+=arr[i]; | |
} | |
return sum; | |
} | |
void printMatrix(float arr[N][N]){ | |
long i=0, j=0; | |
cout << "======\n" ; | |
for(i=0;i<N;i++) { | |
for(j=0;j<N;j++) { | |
cout << arr[i][j] << " "; | |
} | |
cout << "\n"; | |
} | |
cout << "======\n "; | |
} | |
int main() { | |
long i,j,m,n,l; | |
static float xNew[N], xOld[N], a[N][N], b[N], c; | |
double nU, nD, first; | |
srand((unsigned) time(NULL)); | |
for(i=0;i<N;i++) { | |
b[i] = getRand(); | |
} | |
for(i=0;i<N;i++) { | |
xOld[i]=0; | |
for(j=0;j<N;j++) { | |
a[i][j] = getRand(); | |
} | |
} | |
for(i=0;i<N;i++) { | |
a[i][i] = getSum(a[i]) + getRand() + 1; | |
} | |
while(true){ | |
nU = nD = 0; | |
for(i=0;i<N;i++) { | |
c=b[i]; | |
for(j=0;j<N;j++) { | |
if(i!=j) { | |
c-=a[i][j]*xOld[j]; | |
} | |
} | |
xNew[i]=c/a[i][i]; | |
xOld[i] = xNew[i]; | |
nU += fabs(c) * fabs(c); | |
nD += fabs(b[i]) * fabs(b[i]); | |
} | |
if (sqrt(nU) / sqrt(nD) < E){ | |
break; | |
} | |
} | |
cout<<"\n===== answer: \n"; | |
for(i=0;i<N;i++) { | |
cout<<"\nx("<<i<<") = "<<xNew[i]<<"\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment