Advertisement
Dediggefedde

fit iterative-scan algorithmus

Jan 31st, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. // fit.cpp: Hauptprojektdatei.
  2.  
  3. #include "stdafx.h"
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <stdlib.h>
  7. #include <iomanip>
  8.  
  9. using namespace System;
  10. using namespace std;
  11.  
  12. double squ(double i){return i*i;}
  13. int main(array<System::String ^> ^args)
  14. {  
  15.     int calN=4;
  16.     system("pause");
  17.  
  18.     double x[4][2];
  19.     double y[4];
  20.     double c[3];
  21.     double step[3];
  22.    
  23.     x[0][0]=32.13086414254335;
  24.     x[0][1]=128;
  25.     y[0]=130.9045354801;
  26.     x[1][0]=46.534422894583;
  27.     x[1][1]=126;
  28.     y[1]=135.9066659041;
  29.     x[2][0]=47.31386306240475;
  30.     x[2][1]=132;
  31.     y[2]=123.9053434;
  32.     x[3][0]=49.49982274742053;
  33.     x[3][1]=128;
  34.     y[3]=131.9036065;
  35.    
  36.     c[0]=0.08254360199;c[1]=4660.984039;c[2]=0.912878418;
  37.     step[0]=0.01;step[1]=1000;step[2]=0.1;
  38.  
  39.     double minStep=1e-7, minstdev=1e-6;
  40.     int maxiter=1e5;
  41.    
  42.  
  43.     double zwiC[3];
  44.     double minLocal=1e200, minGlobal=1e200;
  45.     double stdev=0;
  46.     int iter=0,i=0;
  47.     double ia,ib,it0;
  48.  
  49.     for(iter=0;iter<maxiter;iter++){   
  50.         minLocal=1e200;
  51.         for(ia=-step[0]*2;ia<=step[0]*2;ia+=step[0]){
  52.             for(it0=-step[1]*2;it0<=step[1]*2;it0+=step[1]){
  53.                 for(ib=-step[2]*2;ib<=step[2]*2;ib+=step[2]){
  54.                     if(ia==0&&ib==0&&it0==0)continue;
  55.                     stdev=0;
  56.                     for(i=0;i<calN;i++){
  57.                         stdev+=squ( (c[0]+ia)*squ(x[i][0]+c[1]+it0)/squ(1+(c[2]+ib)*x[i][1])- y[i]);
  58.                     }
  59.                     if(minLocal>=stdev){
  60.                         minLocal=stdev;                
  61.                         zwiC[0]=c[0]+ia;
  62.                         zwiC[1]=c[1]+it0;
  63.                         zwiC[2]=c[2]+ib;   
  64.                     }
  65.                     if(step[2]==0)break;
  66.                 }
  67.                 if(step[1]==0)break;
  68.             }
  69.             if(step[0]==0)break;
  70.         }
  71.         if(minLocal<=minGlobal){
  72.             minGlobal=minLocal;
  73.             c[0]=zwiC[0];      
  74.             c[1]=zwiC[1];      
  75.             c[2]=zwiC[2];  
  76.         }else{
  77.             step[0]/=2.0;
  78.             step[1]/=2.0;
  79.             step[2]/=2.0;
  80.         }
  81.         if(step[0]<minStep*c[0]&&step[1]<minStep*c[1]&&step[2]<minStep*c[2]|| minGlobal<minstdev)
  82.             break;
  83.        
  84.     }
  85.  
  86.     cout<<"parameter: a="<<setprecision(10)<<c[0]<<" , t0="<<setprecision(10)<<c[1]<<" , b="<<setprecision(10)<<c[2]<<std::endl;
  87.    
  88.     for(i=0;i<calN;i++)
  89.         cout<<"m:"<<setprecision(10)<<y[i]<<" , fit:"<<setprecision(10)<<(c[0]*squ(x[i][0]+c[1])/squ(1+c[2]*x[i][1]))<<" , div:"<<setprecision(10)<<(c[0]*squ(x[i][0]+c[1])/squ(1+c[2]*x[i][1]))-y[i]<<std::endl;
  90.    
  91.     cout<<"Fit-State:";
  92.     if(step[0]<minStep*c[0]&&step[1]<minStep*c[1]&&step[2]<minStep*c[2])cout<<"relativ parameter gradient below "<<minStep;
  93.     if(minGlobal<minstdev)cout<<" standard deviation below"<<minstdev;
  94.     if(iter==maxiter)cout<<"maximum iteration reached: "<<maxiter;
  95.     cout<<endl;
  96.  
  97.     system("pause");
  98.     return EXIT_SUCCESS;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement