Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- real_zeros_of_ the_polynomial_function.c
- 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
- https://www.symbolab.com/solver/roots-calculator/roots%20x%5E%7B4%7D%2B0x%5E%7B3%7D-5x%5E%7B2%7D%2B0x%2B4
- https://www.wolframalpha.com/widgets/view.jsp?id=b858339e64fa997454dd12f77cb1ece1
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- // print function
- void print_function( int degree, double coefficient[])
- {
- int i;
- printf("\n f(x) = ");
- for(i=degree;i>=0; i--)
- {
- if(coefficient[i]>=0)
- printf("+");
- if(i>0)
- printf("%.4f x^%d ", coefficient[i], i );
- else
- printf("%.4f ", coefficient[i]);
- }
- printf("\n\n");
- }
- // get value of function for x
- double function(double x, double coefficient[], int degree)
- {
- int i;
- double xd = 1.0, f = coefficient[0];
- for(i=1; i<=degree; i++)
- {
- xd = xd * x;
- f = f + coefficient[i] * xd;
- }
- return f;
- }
- int main(void)
- {
- int i, degree=4, number_of_real_zeros=0;
- unsigned long long int iterations_by_step=0, iterations_by_epsilon=0;
- double f1, f2, x1, x2, low_lim=-1000.0, upp_lim=1000.0, step=1e-3 , epsilon=1e-10;
- double zeros[20];
- // x^4-17x^3+91x^2-224x+245=0 , x = 3.42182383859132 , x = 9.76947662492925
- // double coefficient[] = {245.0 , -224.0 , 91.0 , -17.0 , 1.0 };
- // x^4-16x^3+90x^2-224x+245=0 , x = 5.0 , x = 7.0
- // double coefficient[] = {245.0 , -224.0 , 90.0 , -16.0 , 1.0 };
- // x^4+0x^3-5x^2+0x+4=0 , x = -2.0 , x = -1.0 , x = 1.0 , x = 2.0
- double coefficient[] = {4.0 , 0.0 , -5.0 , 0.0 , 1.0 };
- printf("\n Real zeros of function \n");
- print_function(degree,coefficient); // print function
- printf(" in interval [ %.4f , %.4f ] \n\n", low_lim, upp_lim );
- x2 = low_lim - epsilon; // initial value
- do // do while( x1<=upp_lim )
- {
- do // do while( f1*f2>0 && x1<=upp_lim )
- {
- x1 = x2; // next values
- x2 = x2 + step;
- f1 = function(x1, coefficient, degree); // f(x1)
- f2 = function(x2, coefficient, degree); // f(x2)
- iterations_by_step++; // count number of iterations_by_step by step
- } while( f1*f2>0 && x1<=upp_lim ); // until the function changes sign and until the end of the interval
- if ( f1*f2<=0 ) // if the function has changed the sign between x1 and x2
- { // then zero is between x1 and x2
- do // do while( f1*f2>0 && x1<=upp_lim )
- {
- x1 = x1 + epsilon; // next values
- x2 = x1 + epsilon;
- f1 = function(x1, coefficient, degree); // f(x1)
- f2 = function(x2, coefficient, degree); // f(x2)
- iterations_by_epsilon++; // count number of iterations_by_epsilon by epsilon
- } while( f1*f2>0 && x1<=upp_lim ); // until the function changes sign and until the end of the interval
- zeros[number_of_real_zeros] = x1; // it is real zero
- number_of_real_zeros++; // count number of real zeros
- }
- } while( x1<=upp_lim ); // until the end of the interval
- if( number_of_real_zeros > 0 ) // if there are real zeros, print report
- {
- printf("\n step = %.10f \n", step ); // print step
- printf("\n epsilon = %.10f \n", epsilon ); // print epsilon
- printf("\n iterations_by_step = %llu \n", iterations_by_step ); // print number of iterations_by_step
- printf("\n iterations_by_epsilon = %llu \n", iterations_by_epsilon ); // print number of iterations_by_epsilon
- printf("\n total number of iterations = %llu \n", iterations_by_step + iterations_by_epsilon);
- printf("\n number_of_real_zeros = %d \n\n", number_of_real_zeros ); // print number of zeros
- for(i=0;i<number_of_real_zeros; i++) // print real zeros and values of function in zeros
- printf("\n x%d = %.9f \t f( %.9f ) = %.9f \n",
- i+1, zeros[i], zeros[i], function(zeros[i], coefficient, degree) );
- }
- else // if there are no real zeros
- printf("\n The function has no real zeros on the interval [ %.4f , %.4f ] \n\n", low_lim, upp_lim );
- return 0;
- }
Add Comment
Please, Sign In to add comment