CodePlsRun

C

Apr 26th, 2022
1,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. void main()
  4. {
  5.     int i, j, k, n;
  6.     float a[20][20], c, x[10], sum = 0.0;
  7.     printf("\nEnter the order of matrix: ");
  8.     scanf("%d", &n);
  9.     printf("\nEnter the elements of augmented matrix row-wise:\n\n");
  10.     for (i = 1; i <= n; i++)
  11.     {
  12.         for (j = 1; j <= (n + 1); j++)
  13.         {
  14.             printf("a[%d][%d] : ", i, j);
  15.             scanf("%f", &a[i][j]);
  16.         }
  17.     }
  18.     for (j = 1; j <= n; j++)
  19.     {
  20.         for (i = 1; i <= n; i++)
  21.         {
  22.             if (i > j)
  23.             {
  24.                 c = a[i][j] / a[j][j];
  25.                 for (k = 1; k <= n + 1; k++)
  26.                 {
  27.                     a[i][k] = a[i][k] - c * a[j][k];
  28.                 }
  29.             }
  30.         }
  31.     }
  32.     x[n] = a[n][n + 1] / a[n][n];
  33.     for (i = n - 1; i >= 1; i--)
  34.     {
  35.         sum = 0;
  36.         for (j = i + 1; j <= n; j++)
  37.         {
  38.             sum = sum + a[i][j] * x[j];
  39.         }
  40.         x[i] = (a[i][n + 1] - sum) / a[i][i];
  41.     }
  42.     printf("\nThe solution is: \n");
  43.     for (i = 1; i <= n; i++)
  44.     {
  45.         printf("\nx%d=%f\t", i, x[i]);
  46.     }
  47.     getch();
  48. }
  49. //--------------------------------------
  50. //--------------------------------------
  51. //--------------------------------------
  52. //--------------------------------------
  53. #include <stdio.h>
  54. #include <math.h>
  55. #include <stdlib.h>
  56. void main()
  57. {
  58.     int k = 0;        // counts number of iterations
  59.     float x1, x0;     // x0 is the initial guess
  60.     float eps = 1e-5; // error tolerance
  61.     float f(float x);
  62.     float fd(float x);
  63.     printf("\nEnter the initial guess x0 : ");
  64.     scanf("%f", &x0);
  65.     x1 = x0;
  66.     do
  67.     {
  68.         k++;
  69.         x0 = x1;
  70.         x1 = x0 - f(x0) / fd(x0);
  71.     } while (fabs(x1 - x0) > eps);
  72.     printf("One root is %8.5f obtained at %dth iteration", x1, k);
  73. }
  74.  
  75. // definition of the function f(x)
  76. float f(float x)
  77. {
  78.     return (x * x * x - 3 * x + 1);
  79. }
  80.  
  81. // definition of the function fd(x)
  82. float fd(float x)
  83. {
  84.     return (3 * x * x - 3);
  85. }
  86. //---------------------------------
  87. //---------------------------------
  88. //---------------------------------
  89. //---------------------------------
  90. #include <conio.h>
  91. #include <stdio.h>
  92. #include <stdlib.h>
  93. #include <math.h>
  94. int user_power, i = 0, cnt = 0, flag = 0;
  95. int coef[10] = {0};
  96. float x1 = 0, x2 = 0, t = 0;
  97. float fx1 = 0, fdx1 = 0;
  98. void main()
  99. {
  100.     printf("\n\n\t\t\t PROGRAM FOR NEWTON RAPHSON GENERAL");
  101.     printf("\n\n\n\tENTER THE TOTAL NO. OF POWER:::: ");
  102.     scanf("%d", &user_power);
  103.     for (i = 0; i <= user_power; i++)
  104.     {
  105.         printf("\n\t x^%d::", i);
  106.         scanf("%d", &coef[i]);
  107.     }
  108.     printf("\n");
  109.     printf("\n\t THE POLYNOMIAL IS ::: ");
  110.     for (i = user_power; i >= 0; i--) // printing coeff.
  111.     {
  112.         printf(" %dx^%d", coef[i], i);
  113.     }
  114.     printf("\n\tINTIAL X1---->");
  115.     scanf("%f", &x1);
  116.     printf("\n******************************************************");
  117.     printf("\n ITERATION X1 FX1 F'X1 ");
  118.     printf("\n******************************************************");
  119.     do
  120.     {
  121.         cnt++;
  122.         fx1 = fdx1 = 0;
  123.         for (i = user_power; i >= 1; i--)
  124.         {
  125.             fx1 += coef[i] * (pow(x1, i));
  126.         }
  127.         fx1 += coef[0];
  128.         for (i = user_power; i >= 0; i--)
  129.         {
  130.             fdx1 += coef[i] * (i * pow(x1, (i - 1)));
  131.         }
  132.         t = x2;
  133.         x2 = (x1 - (fx1 / fdx1));
  134.         x1 = x2;
  135.         printf("\n %d %.3f %.3f %.3f ", cnt, x2, fx1, fdx1);
  136.     } while ((fabs(t - x1)) >= 0.0001);
  137.     printf("\n\t THE ROOT OF EQUATION IS %f", x2);
  138.     getch();
  139. }
  140. //----------------------------
  141. //----------------------------
  142. //----------------------------
  143. //----------------------------
  144. #include <stdio.h>
  145. #include <conio.h>
  146. #include <math.h>
  147. float f(float);
  148. float f(float x)
  149. {
  150.     float y;
  151.  
  152.     y = pow(x, 3) - 2 * x - 5;
  153.     return y;
  154. }
  155. main()
  156. {
  157.     int i = 0;
  158.     float a, b, e, x;
  159.     printf("enter the desire accuracy= ");
  160.     scanf("%f", &e);
  161.     do
  162.     {
  163.         printf("enter the values of a : ");
  164.         scanf("%f", &a);
  165.         printf("enter the values of b : ");
  166.         scanf("%f", &b);
  167.     } while (f(a) * f(b) > 0);
  168.     do
  169.     {
  170.         x = a - (b - a) / (f(b) - f(a)) * f(a);
  171.         if (f(x) * f(a) > 0)
  172.             b = x;
  173.         else
  174.             a = x;
  175.         i = i + 1;
  176.     } while (fabs(f(x)) > e);
  177.     printf("the values of i and x are : %d,%f", i, x);
  178.     getch();
  179.     return 0;
  180. }
  181.  
Advertisement
Add Comment
Please, Sign In to add comment