Arimaa Forum (http://arimaa.com/arimaa/forum/cgi/YaBB.cgi)
Arimaa >> Off Topic Discussion >> Re: My hobby (second season)
(Message started by: hanzack on Feb 1st, 2011, 12:43am)

Title: Re: My hobby (second season)
Post by hanzack on Feb 1st, 2011, 12:43am
I made it! Thanks arimaa babies!



#include <stdio.h>
#include <math.h>

#define N 5
double a[N][N], b[N], x[N];

#define M 250
#define OMEGA (M_PI * 2)
double y[M], c[M][N];

void read_data(char *file)
{
 FILE *in;
 int k;
 double t, w,p[M];

 in = fopen(file, "r");
 if (in == NULL) { fprintf(stderr, "Read error!\n"); }
 for (k = 0; k < M; k++) {

   fscanf(in,"%lf %lf",&p[k], &w);
   y[k]=w;


}
 fclose(in);
}

void print_matrix_a(void)
{
 int i, j;

 printf("--- matrix a ---\n");
 for (i = 0; i < N; i++) {
   for (j = 0; j < N; j++) {
     printf("%11.6f", a[i][j]);
   }
   printf("\n");
 }
}

void print_vector_b(void)
{
 int i;

 printf("--- vector b ---\n");
 for (i = 0; i < N; i++) {
   printf("%10.6f ", b[i]);
 }
 printf("\n");
}

void print_vector_x(void)
{
 int i;

 printf("--- vector x ---\n");
 for (i = 0; i < N; i++) {
   printf("%10.6f ", x[i]);
 }
 printf("\n");
}

void solve(void)
{
 int i, j, k;
 double m, sum;

 
 for (k = 0; k < N; k++) {
   for (i = k + 1; i < N; i++) {
     m = a[i][k] / a[k][k];
     for(j=k+1;j<N;j++){
     a[i][j]=a[i][j]-m*a[k][j];

     }b[i]=b[i]-m*b[k];  
   }
 }

 
 for (k = N - 1; k >= 0; k--) {
   sum=0;
for(j=k+1;j<N;j++){
  sum=sum+a[k][j]*x[j];
}
   x[k] = (b[k] - sum) / a[k][k];
 }
}

void make_matrix(void)
{int i,j,k;
 double t[M];
 a[0][0]=0,b[0]=0;

for(k=0;k<M;k++){

 t[k]=0.02*k;

 c[k][0]=1;

 c[k][1]=cos(OMEGA*t[k]);

c[k][2]=sin(OMEGA*t[k]);

 c[k][3]=cos(2*OMEGA*t[k]);

 c[k][4]=sin(2*OMEGA*t[k]);

}


for(i=0;i<N;i++){
 for(k=0;k<M;k++){
b[i] +=y[k]*c[k][i];
 }
}
for(i=0;i<N;i++){
for(j=0;j<N;j++){
 for(k=0;k<M;k++){
 a[i][j] +=c[k][j]*c[k][i];

]}

}

void print_rmse(void)
{int j,k;
double suma,sumb,sumc,sumd, RMSE;

suma=0,sumb=0,sumc=0;
for(k=0;k<M;k++){
for(j=0;j<N;j++){
suma +=(c[k][j]*x[j])*(c[k][j]*x[j]);
sumb +=2*y[k]*(c[k][j]*x[j]);

}

}

for(k=0;k<M;k++){

sumc +=y[k]*y[k];

}
sumd = sumc-sumb+suma;

RMSE=sqrt(sumd/M);

printf("RMSE=%10.6f\n ", RMSE);
}

int main(void)
{
 read_data("data.txt");            
 make_matrix();                  
 print_matrix_a();                  
 print_vector_b();                  
 solve();                        
 print_vector_x();                  
 print_rmse();                        
}



Title: Re: My hobby (second season)
Post by hanzack on Feb 1st, 2011, 12:45am
this is the "data.txt"

0.000000   0.148949
 0.020000   0.872669
 0.040000   0.787320
 0.060000   0.542697
 0.080000   1.607640
 0.100000   0.792586
 0.120000   1.213314
 0.140000   1.258806
 0.160000   1.096778
 0.180000   1.413962
 0.200000   0.701885
 0.220000   0.692180
 0.240000   0.467869
 0.260000   0.383277
 0.280000   0.194057
 0.300000  -0.349572
 0.320000   0.103754
 0.340000  -0.475095
 0.360000  -0.762549
 0.380000  -1.263494
 0.400000  -1.032502
 0.420000  -1.409862
 0.440000  -1.510348
 0.460000  -1.438014
 0.480000  -1.941349
 0.500000  -1.753632
 0.520000  -0.915306
 0.540000  -0.872972
 0.560000  -0.993482
 0.580000  -0.275689
 0.600000  -0.731151
 0.620000  -0.253896
 0.640000   0.034278
 0.660000   0.302336
 0.680000   0.320775
 0.700000   0.453335
 0.720000   0.696979
 0.740000   0.955246
 0.760000   0.996679
 0.780000   0.999264
 0.800000   0.563160
 0.820000   0.954200
 0.840000   0.083208
 0.860000   0.629852
 0.880000   0.206202
 0.900000   0.466498
 0.920000  -0.175060
 0.940000   0.226739
 0.960000   0.277326
 0.980000   0.694517
 1.000000   0.899604
 1.020000   0.426493
 1.040000   0.638867
 1.060000   0.833356
 1.080000   1.434481
 1.100000   1.057000
 1.120000   1.268745
 1.140000   1.462938
 1.160000   0.820981
 1.180000   1.560411
 1.200000   1.094464
 1.220000   0.613029
 1.240000   0.459632
 1.260000   0.944514
 1.280000   0.443002
 1.300000   0.082302
 1.320000  -0.454459
 1.340000  -0.790384
 1.360000  -0.894862
 1.380000  -1.321015
 1.400000  -1.523030
 1.420000  -1.882842
 1.440000  -1.703207
 1.460000  -1.544685
 1.480000  -1.127327
 1.500000  -1.159030
 1.520000  -1.561615
 1.540000  -0.653725
 1.560000  -1.207297
 1.580000  -0.543838
 1.600000  -0.264824
 1.620000   0.282943
 1.640000   0.319953
 1.660000   0.620210
 1.680000   0.148273
 1.700000   0.565851
 1.720000   0.279266
 1.740000   0.838175
 1.760000   0.313327
 1.780000   0.305755
 1.800000   0.592538
 1.820000   0.663427
 1.840000   0.310548
 1.860000   0.650993
 1.880000   0.476666
 1.900000  -0.057217
 1.920000   0.277954
 1.940000  -0.061010
 1.960000   0.438323
 1.980000   0.015218
 2.000000   0.554333
 2.020000   1.096962
 2.040000   0.486589
 2.060000   1.295227
 2.080000   0.998280
 2.100000   1.718743
 2.120000   1.325217
 2.140000   1.380426
 2.160000   1.701971
 2.180000   1.403068
 2.200000   0.743805
 2.220000   0.960345
 2.240000   0.839129
 2.260000   0.879530
 2.280000   0.108192
 2.300000   0.289297
 2.320000  -0.406927
 2.340000  -0.542907
 2.360000  -0.804938
 2.380000  -0.956835
 2.400000  -0.969062
 2.420000  -1.763540
 2.440000  -1.629800
 2.460000  -1.763376
 2.480000  -1.986885
 2.500000  -1.815159
 2.520000  -1.304021
 2.540000  -1.060269
 2.560000  -1.151175
 2.580000  -0.125246
 2.600000  -0.350666
 2.620000  -0.006205
 2.640000   0.409014
 2.660000   0.382090
 2.680000   0.320996
 2.700000   0.218711
 2.720000   0.702889
 2.740000   1.067371
 2.760000   0.883676
 2.780000   0.610368
 2.800000   0.664391
 2.820000   0.883116
 2.840000  -0.037523
 2.860000   0.102343
 2.880000   0.631371
 2.900000   0.259901
 2.920000  -0.063701
 2.940000   0.141226
 2.960000   0.002918
 2.980000   0.763487
 3.000000   0.120750
 3.020000   1.215525
 3.040000   1.354160
 3.060000   0.935050
 3.080000   0.898151
 3.100000   1.726756
 3.120000   1.308912
 3.140000   1.537892
 3.160000   1.303440
 3.180000   1.442885
 3.200000   1.319862
 3.220000   0.475973
 3.240000   0.589797
 3.260000   0.544649
 3.280000   0.385700
 3.300000   0.212688
 3.320000  -0.088949
 3.340000  -0.841775
 3.360000  -0.652351
 3.380000  -1.068508
 3.400000  -0.963318
 3.420000  -1.539100
 3.440000  -1.521784
 3.460000  -1.105703
 3.480000  -1.311095
 3.500000  -1.552438
 3.520000  -1.329230
 3.540000  -0.726134
 3.560000  -0.686218
 3.580000  -0.585860
 3.600000  -0.268263
 3.620000   0.025169
 3.640000   0.066963
 3.660000   0.332064
 3.680000  -0.007807
 3.700000   0.776530
 3.720000   0.660876
 3.740000   0.722263
 3.760000   0.598961
 3.780000   0.169825
 3.800000   0.359099
 3.820000   0.174459
 3.840000   0.037562
 3.860000   0.547720
 3.880000   0.587833
 3.900000   0.612495
 3.920000   0.305067
 3.940000   0.415666
 3.960000   0.056643
 3.980000   0.284842
 4.000000   0.283517
 4.020000   0.274994
 4.040000   1.099955
 4.060000   1.205833
 4.080000   1.615293
 4.100000   1.148340
 4.120000   0.842416
 4.140000   1.229825
 4.160000   1.059159
 4.180000   1.441346
 4.200000   1.551181
 4.220000   1.314096
 4.240000   0.619632
 4.260000   0.433915
 4.280000   0.173796
 4.300000  -0.086280
 4.320000  -0.641863
 4.340000  -1.095692
 4.360000  -1.296427
 4.380000  -0.906136
 4.400000  -1.657777
 4.420000  -1.488468
 4.440000  -1.068069
 4.460000  -1.725078
 4.480000  -1.815086
 4.500000  -1.142262
 4.520000  -1.596012
 4.540000  -0.861357
 4.560000  -1.001601
 4.580000  -0.798917
 4.600000  -0.882130
 4.620000  -0.127448
 4.640000  -0.086625
 4.660000   0.463992
 4.680000   0.110359
 4.700000   0.340084
 4.720000   0.214388
 4.740000   0.373932
 4.760000   0.854448
 4.780000   0.479055
 4.800000   1.009229
 4.820000   0.661264
 4.840000   0.184915
 4.860000   0.227685
 4.880000  -0.036095
 4.900000   0.547944
 4.920000   0.686064
 4.940000   0.238824
 4.960000   0.738175
 4.980000   0.021762

Title: Re: My hobby (second season)
Post by hanzack on Feb 1st, 2011, 12:48am
here is the function to fit

y(t) = x1 + x2 cos(OMEGA*t) + x3 sin(OMEGA*t) + x4 cos(2*OMEGA*t) + x5 sin(2*OMEGA*t)

yay  :-*



Arimaa Forum » Powered by YaBB 1 Gold - SP 1.3.1!
YaBB © 2000-2003. All Rights Reserved.