最小二次方拟合matlab,一般线性模型的最小二次方拟合方法

#include#includestaticlongdoublegammln (constlongdouble&xx )

{longdoublex,

tmp,

ser;staticlongdoublecof[6]={76.18009173,-86.50532033,24.01409822,-1.231739516,0.120858003e-2,-0.536382e-5};intj;

x=xx-1.0;

tmp=x+5.5;

tmp-=( x+0.5)*log ( tmp );

ser=1.0;for( j=0; j<=5; j++)

{

x+=1.0;

ser+=cof[j]/x;

}return-tmp+log (2.50662827465*ser );

}staticvoidgcf (longdouble*gammcf,constlongdouble&a,constlongdouble&x,longdouble*gln )

{constunsignedintITMAX=100;constlongdoubleEPS=3.0e-7;

unsignedintn=0;longdoublegold=0.0,

g,

fac=1.0,

b1=1.0;longdoubleb0=0.0,

anf,

ana,

an,

a1,

a0=1.0;*gln=gammln ( a );

a1=x;for( n=1; n<=ITMAX; n++)

{

an=(float) n;

ana=an-a;

a0=( a1+a0*ana )*fac;

b0=( b1+b0*ana )*fac;

anf=an*fac;

a1=x*a0+anf*a1;

b1=x*b0+anf*b1;if( a1 )

{

fac=1.0/a1;

g=b1*fac;if( fabs ( ( g-gold )/g )

{*gammcf=exp (-x+a*log ( x )-(*gln ) )*g;return;

}

gold=g;

}

}if("a too large, ITMAX too small in routine GCF")

exit ( EXIT_FAILURE );

}staticvoidgser (longdouble*gamser,constlongdouble&a,constlongdouble&x,longdouble*gln )

{constunsignedintITMAX=100;constlongdoubleEPS=3.0e-7;

unsignedintn=0;longdoublesum,

del,

ap;*gln=gammln ( a );if( x<=0.0)

{if( x<0.0)if("x less than 0 in routine GSER")

exit ( EXIT_FAILURE );*gamser=0.0;return;

}else{

ap=a;

del=sum=1.0/a;for( n=1; n<=ITMAX; n++)

{

ap+=1.0;

del*=x/ap;

sum+=del;if( fabs ( del )

{*gamser=sum*exp (-x+a*log ( x )-(*gln ) );return;

}

}if("a too large, ITMAX too small in routine GSER")

exit ( EXIT_FAILURE );return;

}

}longdoublegammq (constlongdouble&a,constlongdouble&x )

{longdoublegamser,

gammcf,

gln;if( x<0.0||a<=0.0)if("Invalid arguments in routine gammq.")

exit ( EXIT_FAILURE );if( x

{

gser (&gamser, a, x,&gln );return1.0-gamser;

}else{

gcf (&gammcf, a, x,&gln );returngammcf;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值