Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // fit.cpp: Hauptprojektdatei.
- #include "stdafx.h"
- #include <cstdlib>
- #include <iostream>
- #include <stdlib.h>
- #include <iomanip>
- using namespace System;
- using namespace std;
- double squ(double i){return i*i;}
- int main(array<System::String ^> ^args)
- {
- int calN=4;
- system("pause");
- double x[4][2];
- double y[4];
- double c[3];
- double step[3];
- x[0][0]=32.13086414254335;
- x[0][1]=128;
- y[0]=130.9045354801;
- x[1][0]=46.534422894583;
- x[1][1]=126;
- y[1]=135.9066659041;
- x[2][0]=47.31386306240475;
- x[2][1]=132;
- y[2]=123.9053434;
- x[3][0]=49.49982274742053;
- x[3][1]=128;
- y[3]=131.9036065;
- c[0]=0.08254360199;c[1]=4660.984039;c[2]=0.912878418;
- step[0]=0.01;step[1]=1000;step[2]=0.1;
- double minStep=1e-7, minstdev=1e-6;
- int maxiter=1e5;
- double zwiC[3];
- double minLocal=1e200, minGlobal=1e200;
- double stdev=0;
- int iter=0,i=0;
- double ia,ib,it0;
- for(iter=0;iter<maxiter;iter++){
- minLocal=1e200;
- for(ia=-step[0]*2;ia<=step[0]*2;ia+=step[0]){
- for(it0=-step[1]*2;it0<=step[1]*2;it0+=step[1]){
- for(ib=-step[2]*2;ib<=step[2]*2;ib+=step[2]){
- if(ia==0&&ib==0&&it0==0)continue;
- stdev=0;
- for(i=0;i<calN;i++){
- stdev+=squ( (c[0]+ia)*squ(x[i][0]+c[1]+it0)/squ(1+(c[2]+ib)*x[i][1])- y[i]);
- }
- if(minLocal>=stdev){
- minLocal=stdev;
- zwiC[0]=c[0]+ia;
- zwiC[1]=c[1]+it0;
- zwiC[2]=c[2]+ib;
- }
- if(step[2]==0)break;
- }
- if(step[1]==0)break;
- }
- if(step[0]==0)break;
- }
- if(minLocal<=minGlobal){
- minGlobal=minLocal;
- c[0]=zwiC[0];
- c[1]=zwiC[1];
- c[2]=zwiC[2];
- }else{
- step[0]/=2.0;
- step[1]/=2.0;
- step[2]/=2.0;
- }
- if(step[0]<minStep*c[0]&&step[1]<minStep*c[1]&&step[2]<minStep*c[2]|| minGlobal<minstdev)
- break;
- }
- cout<<"parameter: a="<<setprecision(10)<<c[0]<<" , t0="<<setprecision(10)<<c[1]<<" , b="<<setprecision(10)<<c[2]<<std::endl;
- for(i=0;i<calN;i++)
- 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;
- cout<<"Fit-State:";
- if(step[0]<minStep*c[0]&&step[1]<minStep*c[1]&&step[2]<minStep*c[2])cout<<"relativ parameter gradient below "<<minStep;
- if(minGlobal<minstdev)cout<<" standard deviation below"<<minstdev;
- if(iter==maxiter)cout<<"maximum iteration reached: "<<maxiter;
- cout<<endl;
- system("pause");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement