#include <stdio.h>
#include <math.h>
double g(double y,double mu, double sig){
double g1,g2,h,x,x1,x2,x3,f;
int n,t;
double pi;
pi = 3.1415;
g1=0;
h=0.001;
t=floor(y/h);
for (n=-2000;n<t;n++){
x=n*h;
x1=-pow(x - mu,2) / (2*pow(sig,2));
x2=exp(x1);
x3=sig*pow(2*pi,0.5);
g2=x2/x3;
g1=g1+g2*h;
}
return g1;
}
int main(void)
{
double y1,y2,y3,g1,g2,p,mu,sig;
double th[101];
double maxwel,wel,maxtl;
int n,t,s;
double tl,tr3,tr1,tr2,tr,l1,c1,w1,d1,d2;
mu=0;
sig=0.39;
for (s=1;s<101;s++){
p=-0.005+0.01*s;
y1=-0.4;
y2=0.3;
g1=g(y1,mu,sig);
t=0;
while(t<100){
g2=g(y2,mu,sig);
y3=y2+(p-g2)*(y2-y1)/(g2-g1);
y1=y2;
y2=y3;
g1=g2;
if (pow(p-g2,2)<0.0001)t=1000;
t=t+1;
}
th[s]=exp(y2);
}
maxwel=-999;
for (n=10;n<=40;n++){
tl=0.01*n;
tr1=0.05;
tr2=0.03;
d1=0;
for (s=1;s<=100;s++){
w1=(1-tl)*th[s];
l1=(w1-tr1)/(2*w1);
if (l1<0) l1=0;
y1=th[s]*l1;
c1=w1*l1+tr1;
d1=d1+y1-c1;
}
t=0;
while(t<100){
d2=0;
for (s=1;s<=100;s++){
w1=(1-tl)*th[s];
l1=(w1-tr2)/(2*w1);
if (l1<0) l1=0;
y1=th[s]*l1;
c1=w1*l1+tr2;
d2=d2+y1-c1;
}
tr3=tr2-d2*(tr2-tr1)/(d2-d1);
tr1=tr2;
tr2=tr3;
d1=d2;
if (d2*d2<0.01) t=1000;
t=t+1;
}
tr=tr2;
wel=0;
for (s=1;s<=100;s++){
w1=(1-tl)*th[s];
l1=(w1-tr)/(2*w1);
if (l1<0) l1=0;
c1=w1*l1+tr;
wel=wel+log(c1)+log(1-l1);
}
if (wel>maxwel) maxtl=tl;
if (wel>maxwel) maxwel=wel;
}
printf("%f",maxtl);
return 0;
}
最終更新:2009年12月14日 11:26