【发布时间】:2018-08-30 14:32:54
【问题描述】:
以下是Bounding Phase 方法的代码,它返回限制的中点。但它没有将值返回给 main() 函数。 对出现问题的行进行了注释。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float func(float x)
{
float f = (pow(x,2) + (54 / x));
return f;
}
float bPhase(float x0, float delta)
{
float x1 , f0, f1, f2, a, b, mPt;
int k, p;
k = 0;
p = 0;
f0 = func(x0 - delta);
p++;
f1 = func(x0);
p++;
f2 = func(x0 + delta);
p++;
if(f0 <= f1 && f1 <= f2)
delta = -1 * delta;
else
delta = delta;
printf("%f %f %f\n", f0, f1, f2);
x1 = x0 + pow(2,k) * delta;
while(func(x1) < func(x0))
{
k++;
a = x0;
x0 = x1;
x1 = x1 + pow(2,k) * delta;
b = x1;
printf("%f %f %f\n", func(a),func(x0),func(b));
p++;
}
printf("Minimum lies between %.3f and %.3f", a, b);
printf("\nIteration no: %d\n", k+1);
printf("Total no. of function evaluations: %d\n", p);
mPt = ((a+b)/2.0);
printf("%f\n", mPt); //Here prints 5.1
return mPt; //Should return 5.1, but not returning
}
int main(void)
{
float x0, mPt;
float delta;
printf("Enter initial guess: "); // guess is .6
scanf("%f", &x0);
printf("Enter increment: "); // 0.5
scanf("%f", &delta);
bPhase(x0, delta);
printf("\n%f\n", mPt); //should print 5.1 but prints random
return 0;
}
函数 bPhase 没有返回任何内容。请帮忙。主函数未从 bPhase 函数接收 mPt 的值。 这里是新手。谢谢。
【问题讨论】:
-
bPhase(x0, delta);->mPt = bPhase(x0, delta);
标签: c