线形最小二乘法拟合。用一个一般的X的幂级数来拟合。

源代码在线查看: least square fitting .cpp

软件大小: 2 K
上传用户: IsabellaJ
关键词: 最小二乘法拟合
下载地址: 免注册下载 普通下载 VIP

相关代码

				#include "iostream.h"
				#include "math.h"
				#include "stdlib.h"
				
				void funcs(double x, double afunc[], int ma)
				{
					int i;
				    afunc[1] = 1.0;
				    for (i = 2; i					{
				        afunc[i] = x * afunc[i - 1];
				    }
				}
				
				void main()
				{
				    int i,j,npt = 100;
				    double chisq,spread = 0.1;
				    int mfit,nterm = 3;
				    double x[101], y[101], sig[101], a[4], covar[10];
					int lista[4];
				    long idum = -911;
				    for (i = 1; i					{
				        x[i] = 0.1 * i;
				        y[i] = nterm;
				        for (j = nterm - 1; j>=1; j--)
						{
				            y[i] = j + y[i] * x[i];
				        }
				        y[i] = y[i] + spread * gasdev(idum);
				        sig[i] = spread;
				    }
				    mfit = nterm;
				    for (i = 1; i					{
				        lista[i] = i;
				    }
				    lfit(x, y, sig, npt, a, nterm, lista, mfit, covar, nterm, chisq);
				    cout					cout.setf(ios::scientific|ios::left);
					cout.precision(4);
				    for (i = 1; i					{
				        cout						cout.width(18);
						cout						cout.width(18);
				        cout						cout				    }
				    cout				    cout				    cout				    cout				    for (i = 1; i					{
				        for (j = 1; j						{
							cout.width(18);
				            cout				        }
						cout				    }
				    //now test the lista feature
				    for (i = 1; i					{
				        lista[i] = nterm + 1 - i;
				    }
				    lfit(x, y, sig, npt, a, nterm, lista, mfit, covar, nterm, chisq);
				    cout				    cout				    for (i = 1; i					{
				        cout						cout.width(18);
						cout						cout.width(18);
						cout				    }
				    cout				    cout				    cout				    cout				    for (i = 1; i					{
				        for (j = 1; j						{
							cout.width(18);
				            cout				        }
						cout				    }
				    //now check results of restricting fit parameters
				    int ii = 1;
					int aaa;
				    for (i = 1; i					{
				        aaa = i - int(i / 2) * 2;
				        if (aaa == 1)
						{
				            lista[ii] = i;
				            ii = ii + 1;
				        }
				    }
				    mfit = ii - 1;
				    lfit(x, y, sig, npt, a, nterm, lista, mfit, covar, nterm, chisq);
				    cout				    cout				    for (i = 1; i					{
				        cout						cout.width(18);
						cout						cout.width(18);
						cout				    }
				    cout				    cout				    cout				    cout				    for (i = 1; i					{
				        for (j = 1; j						{
							cout.width(18);
				            cout				        }
						cout				    }
				}
							

相关资源