Skip to main content

Program for Newton Raphson General.

 Program for Newton Raphson General.


CODE

πŸ‘‡

  1. #include<stdio.h>
  2. #include<math.h>

  3. int user_power, i = 0, cnt = 0, flag = 0;
  4. int coef[10] = {0};
  5. float x1 = 0, x2 = 0, t = 0;
  6. float fx1 = 0, fdx1 = 0;

  7. int main()
  8. {
  9.     printf("PROGRAM FOR NEWTON RAPHSON GENERAL");

  10.     printf("\nEnter the total no. of power : ");
  11.     scanf("%d", &user_power);

  12.     for (i = 0; i <= user_power; i++)
  13.     {
  14.         printf("\nx^%d : ", i);
  15.         scanf("%d", &coef[i]);
  16.     }

  17.     printf("\n");

  18.     printf("\n\nThe Polynomial is ");

  19.     //printing coeff.
  20.     for (i = user_power; i >= 0; i--)
  21.     {
  22.         printf("%dx^%d", coef[i], i);
  23.     }

  24.     printf("\n\nIntial x1 -> ");
  25.     scanf("%f", &x1);

  26.     printf("Iteration\tX1\tFX1\tF'X1");

  27.     do
  28.     {
  29.         cnt++;
  30.         fx1 = fdx1 = 0;
  31.         for (i = user_power; i >= 1; i--)
  32.         {
  33.             fx1 += coef[i] * (pow(x1, i));
  34.         }

  35.         fx1 += coef[0];

  36.         for (i = user_power; i >= 0; i--)
  37.         {
  38.             fdx1 += coef[i] * (i * pow(x1, (i - 1)));
  39.         }

  40.         t = x2;
  41.         x2 = (x1 - (fx1 / fdx1));
  42.         x1 = x2;

  43.         printf("\n\t%d\t%.3f\t%.3f\t%.3f ", cnt, x2, fx1, fdx1);

  44.     }
  45.     while ((fabs(t - x1)) >= 0.0001);

  46.     printf("\n\nThe root of equation is %f", x2);

  47.     return 0;
  48. }

πŸ‘‰ExcuteπŸ‘ˆ

//OUTPUT
/*

*/