dmilicev

real_zeros_of_ the_polynomial_function.c

Feb 8th, 2021 (edited)
808
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     real_zeros_of_ the_polynomial_function.c
  4.  
  5. https://www.emathhelp.net/calculators/algebra-2/zeros-calculator/?f=x%5E4%2B0x%5E3-5x%5E2%2B0x%2B4%3D0&var=&sa=0&a=-inf&b=inf&sb=0&real=on
  6. https://www.symbolab.com/solver/roots-calculator/roots%20x%5E%7B4%7D%2B0x%5E%7B3%7D-5x%5E%7B2%7D%2B0x%2B4
  7. https://www.wolframalpha.com/widgets/view.jsp?id=b858339e64fa997454dd12f77cb1ece1
  8.  
  9.  
  10.     You can find all my C programs at Dragan Milicev's pastebin:
  11.  
  12.     https://pastebin.com/u/dmilicev
  13.  
  14. */
  15.  
  16. #include <stdio.h>
  17.  
  18. // print function
  19. void print_function( int degree, double coefficient[])
  20. {
  21.     int i;
  22.  
  23.     printf("\n f(x) = ");
  24.     for(i=degree;i>=0; i--)
  25.     {
  26.         if(coefficient[i]>=0)
  27.             printf("+");
  28.  
  29.         if(i>0)
  30.             printf("%.4f x^%d ", coefficient[i], i );
  31.         else
  32.             printf("%.4f ", coefficient[i]);
  33.     }
  34.     printf("\n\n");
  35. }
  36.  
  37. // get value of function for x
  38. double function(double x, double coefficient[], int degree)
  39. {
  40.     int i;
  41.     double xd = 1.0, f = coefficient[0];
  42.  
  43.     for(i=1; i<=degree; i++)
  44.     {
  45.         xd = xd * x;
  46.         f = f + coefficient[i] * xd;
  47.     }
  48.     return f;
  49. }
  50.  
  51. int main(void)
  52. {
  53.     int i, degree=4, number_of_real_zeros=0;
  54.     unsigned long long int iterations_by_step=0, iterations_by_epsilon=0;
  55.     double f1, f2, x1, x2, low_lim=-1000.0, upp_lim=1000.0, step=1e-3 , epsilon=1e-10;
  56.     double zeros[20];
  57.  
  58. // x^4-17x^3+91x^2-224x+245=0 , x = 3.42182383859132 , x = 9.76947662492925
  59. //  double coefficient[] = {245.0 , -224.0 , 91.0 , -17.0 , 1.0 };
  60.  
  61. // x^4-16x^3+90x^2-224x+245=0 , x = 5.0 , x = 7.0
  62. //  double coefficient[] = {245.0 , -224.0 , 90.0 , -16.0 , 1.0 };
  63.  
  64. // x^4+0x^3-5x^2+0x+4=0 , x = -2.0 , x = -1.0 , x = 1.0 , x = 2.0
  65.     double coefficient[] = {4.0 , 0.0 , -5.0 , 0.0 , 1.0 };
  66.  
  67.     printf("\n Real zeros of function \n");
  68.     print_function(degree,coefficient);                 // print function
  69.     printf(" in interval [ %.4f , %.4f ] \n\n", low_lim, upp_lim );
  70.  
  71.     x2 = low_lim - epsilon;                             // initial value
  72.     do                                                  // do while( x1<=upp_lim )
  73.     {
  74.         do                                              // do while( f1*f2>0 && x1<=upp_lim )
  75.         {
  76.             x1 = x2;                                    // next values
  77.             x2 = x2 + step;
  78.             f1 = function(x1, coefficient, degree);     // f(x1)
  79.             f2 = function(x2, coefficient, degree);     // f(x2)
  80.             iterations_by_step++;                       // count number of iterations_by_step by step
  81.         } while( f1*f2>0 && x1<=upp_lim );  // until the function changes sign and until the end of the interval
  82.         if ( f1*f2<=0 )                     // if the function has changed the sign between x1 and x2
  83.         {                                   // then zero is between x1 and x2
  84.             do                                          // do while( f1*f2>0 && x1<=upp_lim )
  85.             {
  86.                 x1 = x1 + epsilon;                      // next values
  87.                 x2 = x1 + epsilon;
  88.                 f1 = function(x1, coefficient, degree); // f(x1)
  89.                 f2 = function(x2, coefficient, degree); // f(x2)
  90.                 iterations_by_epsilon++;                // count number of iterations_by_epsilon by epsilon
  91.             } while( f1*f2>0 && x1<=upp_lim );  // until the function changes sign and until the end of the interval
  92.  
  93.             zeros[number_of_real_zeros] = x1;           // it is real zero
  94.             number_of_real_zeros++;                     // count number of real zeros
  95.         }
  96.     } while( x1<=upp_lim );                             // until the end of the interval
  97.  
  98.     if( number_of_real_zeros > 0 )                      // if there are real zeros, print report
  99.     {
  100.         printf("\n step    = %.10f \n", step );         // print step
  101.         printf("\n epsilon = %.10f \n", epsilon );      // print epsilon
  102.         printf("\n iterations_by_step         = %llu \n", iterations_by_step );    // print number of iterations_by_step
  103.         printf("\n iterations_by_epsilon      = %llu \n", iterations_by_epsilon ); // print number of iterations_by_epsilon
  104.         printf("\n total number of iterations = %llu \n", iterations_by_step + iterations_by_epsilon);
  105.         printf("\n number_of_real_zeros = %d \n\n", number_of_real_zeros );   // print number of zeros
  106.  
  107.         for(i=0;i<number_of_real_zeros; i++)            // print real zeros and values of function in zeros
  108.             printf("\n x%d = %.9f \t f( %.9f ) = %.9f \n",
  109.                     i+1, zeros[i], zeros[i], function(zeros[i], coefficient, degree) );
  110.     }
  111.     else                                                // if there are no real zeros
  112.         printf("\n The function has no real zeros on the interval [ %.4f , %.4f ] \n\n", low_lim, upp_lim );
  113.  
  114.     return 0;
  115. }
  116.  
RAW Paste Data Copied