我想解決以下特殊情況下的等式,其中l = b = 0;它應該返回完美平方函數的平方根,例如SQRT((X-d)^ 2)。這可以有兩個解,(x-d)或(d-x)。我想獲得(x-d)作爲我的最終解決方案,但程序默認返回(d-x)解決方案。我試圖改變d和x的位置,但似乎沒有任何工作。下面是我的程序:從函數的平方根返回一個首選根
float y(float x) {
float l=0., b=0., d=8.5, r_0=3., z_0=0.1;
return exp(-pow(x*x*cos(b*PI/180.)*cos(b*PI/180.)+d*d-2*d*x*cos(b*PI/180.)*cos(l*PI/180.), 0.5)/r_0)*exp(-x*pow(1-cos(b*PI/180.)*cos(b*PI/180.),0.5)/z_0) ;
}
int main(){
FILE* fp =NULL;
float x0,xn,step,s,int_val, tau; /* s = distance to the star from the sun*/
int i,n, j ;
scanf("%f%f%d",&x0,&xn,&n);
step = (xn-x0)/n;
s = y(x0) + y(xn);
fp = fopen("trap.txt", "w");
for(i = 1; i < n; i++) {
s += 2*y(x0+i*step);
fprintf(fp,"%e\n",s*step/2);
}
fclose(fp);
是否有一些代碼缺失? – abiessu
該指數具有至少一個可能的優化,即,'1-cos^2(x)= sin^2(x)'等。另外,任何你不能僅僅採用負分支的理由? – abiessu