f (x) = x^4 -4x +1의 근원을 찾아내는 알고리즘을 작성하려고합니다. 이 함수의 4 개 근본을 얻으려고합니다. 2 개의 실수와 상수 . 이 알고리즘을 c에 씁니다.프로그래밍에서의 Bisection 방법
#include<stdio.h>
#include<math.h>
int computeroots(double, double, double, double);
int main()
{
double a ,b, soln;
double epsilon = pow(10, -8);
int MaxIter;
printf("please enter an initial guess for a: \n");
scanf("%ld",&a);
printf("please enter a second guess for b: \n");
scanf("%ld", &b);
printf("please enter the maximum number of iteration: \n");
scanf("%d", &MaxIter);
soln = computeroots(a, b, epsilon, MaxIter);
printf("Here is the solution: %d \n", soln);
return 0;
}
int computeroots(double a, double b, double epsilon, double MaxIter)
{
float FA = pow(a,4) - 4*a + 1;
float FB = pow(b,4) - 4*b +1;
float FP;
int i =1;
float p;
if(FA * FB < 0)
{
while(i<MaxIter)
{
p = a + (b-a)/2;
FP = pow(p,4) - 4*p +1;
if(FP == 0 || (b-a)/2 < epsilon)
{
return p;
break;
}
i++;
if(FA*FP > 0)
{
a = p;
FA = FP;
}
else
{
b = p;
}
}
}
}
왜 함수가 하나의 return만을 갖고 있습니까? 왜 그것은'float' 값을'int'로 반환합니까? 'FA * FB == 0.0'또는 'FA * FB> 0.0'일 때 어떻게됩니까? 왜 'double'대신에'float' 값을 사용하고 있습니까? '(b-a)/2 ε'라는 표현은 상대적인 차이가 아닙니다. 'a'가'b'보다 크면 조건이 만족 될 것입니다. –
당신은 완벽 해요. .. 내가 더 잘 알고 제어 할 수 있기 때문에 나는 루프를 FOR 루프로 바꿨다. 고마워. – user2059456