fork download
  1. // ------------------------------------------------------
  2. // force-bisector
  3. //
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <float.h>
  8. // ------------------------------------------------------
  9. double A, B, C, M, N;
  10. double func(double x)
  11. {
  12. return
  13. A * exp(M*x) + B * exp(N*x) - C;
  14. }
  15. // -------------------------------------------------------
  16. double ForceBiSector(double eps,
  17. double (*fx)(double),
  18. int max_itera)
  19. {
  20. double low = -DBL_MAX;
  21. double up = DBL_MAX;
  22. double mid = 0.0;
  23. double ylow=fx(low), yup=fx(up), ymid=fx(mid);
  24. unsigned long long times=0;
  25. while(fabs(ymid) > eps && times < max_itera){
  26. if(ymid * yup < 0.0) ylow = ymid, low=mid;
  27. else yup = ymid, up=mid;
  28. mid = 0.5 * (up+low);
  29. ymid = fx(mid);
  30. ++times;
  31. }
  32. printf("\ntimes : %u\n", times);
  33. return mid;
  34. }
  35.  
  36. // -------------------------------------------------------
  37. int main()
  38. {
  39. const double eps=1E-15;
  40. const int max_iterator=1000000;
  41. double x0, x;
  42.  
  43. puts("A * e^(mx) + B * e^(nx) - C = 0 < force bisector >");
  44. printf("input A : "); scanf("%lf", &A);
  45. printf("input B : "); scanf("%lf", &B);
  46. printf("input C : "); scanf("%lf", &C);
  47. printf("input N : "); scanf("%lf", &N);
  48. printf("input M : "); scanf("%lf", &M);
  49.  
  50. x = ForceBiSector(eps, func, max_iterator);
  51.  
  52. printf("x : %.15lf\n", x);
  53. printf("func(x) : %.15lf\n", func(x));
  54. // system("pause"); // 看不到結果, 這行加進去
  55. return 0;
  56. }
Success #stdin #stdout 0.01s 1724KB
stdin
1
2
4
8
16

1.234
3.145
100.145
3.337
6.145

1000.247
123.597
12347.345
-10.2
-5.7
stdout
A * e^(mx) + B * e^(nx) - C = 0 < force bisector >
input A : input B : input C : input N : input M : 
times : 1078
x       : 0.026491919437543
func(x) : 0.000000000000001