코드 다 짯는데 왜 이렇게 자꾸 한쪽 값이 고정이 되지.....


계산상으로는 저렇게 고정이 되면 안되는데 Xl의 값이 변하지 않고 그냥 5로 계속 고정됨 ㅠㅠ


#include <stdio.h>

#include <math.h>


long double fun ( long double k) // 주어진 the trajectory of a ball의 주어진 거리에 대한 height식을 fun 함수로 설정


 {  

long double fun = sqrt((16 * k*k) / ((k - 4)*(k - 4)) + k*k);


return fun ; // fun 함수로 반환 


}



 void main()

 {

FILE *fp=fopen("data.txt", "w" ); 


  int iter = 1 ;

  long double R = ( sqrt(5.0) - 1 ) /2 ; // golden ratio값

  

  long double xl = 5; // 초기 lower value

  long double xu = 10; // 초기 upper value 

  long double d = R * ( xu - xl ) ; //golden ratio를 이용한 d 값 

  

  long double xopt ;


   printf(" iter\t ea\t \tapproximate \n");

   fprintf( fp , " iter\t ea\t \tapproximate \n");

   printf("──────────────────\n");

  

  while ( iter < 50 ) 

{

       // golden section 구현 

 long double d = R * ( xu - xl ) ;

      long double x1 = xl + d ;

      long double x2 = xu - d;

// 반복문을 이용한 golden section 새로운 x값을 구하는 과정

// 교재의 수도코드를 참고 하여 작성 

 if( fun(x1) > fun(x2) ) 

 {

 xopt = x1;

 }

 else if ( fun(x2) > fun(x1) )

 {

 xopt = x2;

 }


      if( fun(x1) > fun(x2) )

 {

 xl = x2;

 x2 = x1;

 x1 = xl + d;

 

 }

 else if ( fun(x2) > fun(x1) )

 {

 xu = x1;

 x1=x2;

 x2 = xu -d ;

 

 }

      long double ea = (1-R)*abs((xu-xl)/xopt) * 100 ; // golden section 에서의 상대오차 식

 long double es = 0.1 ; // 문제에서 주어진 stopping condition 


 printf("\n %d\t %f\t %f\n " , iter , ea , xopt );  

 fprintf(fp ," %d\t %f\t %f\n " , iter , ea , xopt );

 if ( abs(ea) < es )  // stopping condition

 {

 break;

 }

         iter++;

   }


   long double gold = xopt ; // 반복문을 빠져나온 값이 golden section 으로 구한 root 

   printf("──────────────────\n");

   fprintf(fp ,"\n\n Gold = %f\n" , xopt );

   printf(" Gold = %f\n" , xopt );

   

 }