SHARE
TWEET

Untitled

a guest Apr 25th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <math.h>
  3. long double f(long double x)
  4. {
  5.     return cos(x);
  6. }
  7. long double fp1(long double x)
  8. {
  9.     return -sinl(x);
  10. }
  11. long double fp2(long double x)
  12. {
  13.     return -cosl(x);
  14. }
  15. long double power(long double x, int n)
  16. {
  17.     long double mult = 1;
  18.     if (n == 0)
  19.     {
  20.         return 1;
  21.     }
  22.     int i = 0;
  23.     for (i = 1; i <= n; i++)
  24.     {
  25.         mult *= x;
  26.     }
  27.     return mult;
  28. }
  29. void Simple_Iteration(int kmax, long double eps, long double x0, long double *x1,
  30.                       int *k)
  31. {
  32.     long double tau;
  33.     printf("\nMethod simple iteration\n");
  34.     *x1 = x0;
  35.     tau = -1.0 / fp1(x0);
  36.     *k = 0;
  37.     do
  38.     {
  39.         x0 = *x1;
  40.         *x1 = x0 + tau * f(x0);
  41.         *k += 1;
  42.         if (*k > kmax)
  43.         {
  44.             break;
  45.         }
  46.     } while (!(fabsl(*x1 - x0) <= eps) && !(fabsl(f(*x1)) <= eps));
  47.     if (*k > kmax)
  48.     {
  49.         printf("Solution not found\n");
  50.     }
  51.     else
  52.     {
  53.         printf("Solution = %Le\n", *x1);
  54.         printf("With iteration %d\n", *k);
  55.     }
  56. }
  57. void Method_Newton(int kmax, long double eps, long double x0, long double *x1, int *k)
  58. {
  59.     printf("\nMethod Newton\n");
  60.     *x1 = x0;
  61.     *k = 0;
  62.     do
  63.     {
  64.         x0 = *x1;
  65.         *x1 = x0 - f(x0) / fp1(x0);
  66.         *k += 1;
  67.         if (*k > kmax)
  68.         {
  69.             break;
  70.         }
  71.     } while (!(fabsl(*x1 - x0) <= eps) && !(fabsl(f(*x1)) <= eps));
  72.     if (*k > kmax)
  73.     {
  74.         printf("Solution not found\n");
  75.     }
  76.     else
  77.     {
  78.         printf("Solution = %Le\n", *x1);
  79.         printf("With iteration %d\n", *k);
  80.     }
  81. }
  82. void Method_Chebysheva(int kmax, long double eps, long double x0, long double *x1,
  83.                        int *k)
  84. {
  85.     printf("\nMethod Chebusheva\n");
  86.     *x1 = x0;
  87.     *k = 0;
  88.     do
  89.     {
  90.         x0 = *x1;
  91.         *x1 =
  92.             x0 - f(x0) / fp1(x0) - 0.5 * power(f(x0), 2) * fp2(x0) / power(fp1(x0), 3);
  93.         *k += 1;
  94.         if (*k > kmax)
  95.         {
  96.             break;
  97.         }
  98.     } while (!(fabsl(*x1 - x0) <= eps) && !(fabsl(f(*x1)) <= eps));
  99.     if (*k > kmax)
  100.     {
  101.         printf("Solution not found\n");
  102.     }
  103.     else
  104.     {
  105.         printf("Solution = %Le\n", *x1);
  106.         printf("With iteration %d\n", *k);
  107.     }
  108. }
  109. void Method_Hord(int kmax, long double eps, long double x0, long double *x2, int *k)
  110. {
  111.     long double tau, x1;
  112.     printf("\nMethod Hord\n");
  113.     *k = 0;
  114.     tau = -1.0 / fp1(x0);
  115.     x1 = x0 + tau * f(x0);
  116.     *x2 = x1;
  117.     x1 = x0;
  118.     do
  119.     {
  120.         x0 = x1;
  121.         x1 = *x2;
  122.         *x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0));
  123.         *k += 1;
  124.         if (*k > kmax)
  125.         {
  126.             break;
  127.         }
  128.     } while (!(fabsl(*x2 - x1) <= eps) && !(fabsl(f(*x2)) <= eps));
  129.     if (*k > kmax)
  130.     {
  131.         printf("Solution not found\n");
  132.     }
  133.     else
  134.     {
  135.         printf("Solution = %Le\n", *x2);
  136.         printf("With iteration %d\n", *k);
  137.     }
  138. }
  139. long double rr(long double x0, long double x1)
  140. {
  141.     long double res = (f(x1) - f(x0)) / (x1 - x0);
  142.     return res;
  143. }
  144. long double delta(long double x1, long double x2, long double root, long double rr1, long double rr2, int ind)
  145. {
  146.     long double res;
  147.     if (ind == 0)
  148.     {
  149.         res = (-((x2 - x1) * rr2 + rr1) + root) / (2 * rr2);
  150.     }
  151.     if (ind == 1)
  152.     {
  153.         res = (-((x2 - x1) * rr2 + rr1) - root) / (2 * rr2);
  154.     }
  155.     return res;
  156. }
  157. void Method_Parabol(int kmax, long double eps, long double x0, long double *x3,
  158.                     int *k)
  159. {
  160.     long double x1, x2, delta0, delta1, delta2, rr1, rr2, root, tau;
  161.     printf("\nMethod parabol\n");
  162.     *k = 0;
  163.     tau = -1.0 / fp1(x0);
  164.     x1 = x0 + tau * f(x0);
  165.     x2 = x1 + tau * f(x1);
  166.     *x3 = x2;
  167.     x2 = x1;
  168.     x1 = x0;
  169.     do
  170.     {
  171.         x0 = x1;
  172.         x1 = x2;
  173.         x2 = *x3;
  174.         rr1 = rr(x1, x2);
  175.         rr2 = (rr(x1, x2) - rr(x0, x1)) / (x2 - x0);
  176.         root = sqrtl(power((x2 - x1) * rr2 + rr1, 2) - 4 * rr2 * f(x2));
  177.         delta1 = delta(x1, x2, root, rr1, rr2, 0);
  178.         delta2 = delta(x1, x2, root, rr1, rr2, 1);
  179.         if (fabsl(delta1) < fabsl(delta2))
  180.         {
  181.             delta0 = delta1;
  182.         }
  183.         else
  184.         {
  185.             delta0 = delta2;
  186.         }
  187.         *x3 = x2 + delta0;
  188.         *k += 1;
  189.         if (*k > kmax)
  190.         {
  191.             break;
  192.         }
  193.     } while (!(fabsl(*x3 - x2) <= eps) && !(fabsl(f(*x3)) <= eps));
  194.     if (*k > kmax)
  195.     {
  196.         printf("Solution not found\n");
  197.     }
  198.     else
  199.     {
  200.         printf("Solution = %Le\n", *x3);
  201.         printf("With iteration %d\n", *k);
  202.     }
  203. }
  204. void Method_Reverse_Interpolation(int kmax, long double eps, long double x0, long double *x2, int *k)
  205. {
  206.     printf("\nMethod_reverse_interpolation\n");
  207.     long double x1, tau;
  208.     *k = 0;
  209.     tau = -1.0 / fp1(x0);
  210.     x1 = x0 + tau * f(x0);
  211.     *x2 = x1;
  212.     x1 = x0;
  213.     do
  214.     {
  215.         x0 = x1;
  216.         x1 = *x2;
  217.         *x2 = -x0 * f(x1) / (f(x0) - f(x1)) - x1 * f(x0) / (f(x1) - f(x0));
  218.         *k += 1;
  219.         if (*k > kmax)
  220.         {
  221.             break;
  222.         }
  223.     } while (!(fabsl(*x2 - x1) <= eps) && !(fabsl(f(*x2)) <= eps));
  224.     if (*k > kmax)
  225.     {
  226.         printf("Solution not found\n");
  227.     }
  228.     else
  229.     {
  230.         printf("Solution = %Le\n", *x2);
  231.         printf("With iteration %d\n", *k);
  232.     }
  233. }
  234. void Method_Eitkena(int kmax, long double eps, long double x0, long double *x3,
  235.                     int *k)
  236. {
  237.     printf("\nMethod Eitkena\n");
  238.     long double x2, x1, tau;
  239.     tau = -1.0 / fp1(x0);
  240.     *x3 = x0;
  241.     *k = 0;
  242.     do
  243.     {
  244.         x0 = *x3;
  245.         x1 = x0 + tau * f(x0);
  246.         x2 = x1 + tau * f(x1);
  247.         *x3 = x2 + power(x2 - x1, 2) / (2 * x1 - x2 - x0);
  248.         *k += 1;
  249.         if (*k > kmax)
  250.         {
  251.             break;
  252.         }
  253.     } while (!(fabsl(*x3 - x2) <= eps) && !(fabsl(f(*x3)) <= eps));
  254.     if (*k > kmax)
  255.     {
  256.         printf("Solution not found\n");
  257.     }
  258.     else
  259.     {
  260.         printf("Solution = %Le\n", *x3);
  261.         printf("With iteration %d\n", *k);
  262.     }
  263. }
  264. int main()
  265. {
  266.     FILE *output = fopen("result.txt", "w");
  267.     long double x0, x1, x2, x3;
  268.     long double eps = 1e-10;
  269.     int k;
  270.     int k_max = 1e+4;
  271.     x0 = 5;
  272.     Simple_Iteration(k_max, eps, x0, &x1, &k);
  273.     fprintf(output, "\nSimple_Iteration\n");
  274.     fprintf(output, "Solution = %Le\n", x1);
  275.     fprintf(output, "With iteration %d\n", k);
  276.     Method_Newton(k_max, eps, x0, &x1, &k);
  277.     fprintf(output, "\nMethod_Newton\n");
  278.     fprintf(output, "Solution = %Le\n", x1);
  279.     fprintf(output, "With iteration %d\n", k);
  280.     Method_Chebysheva(k_max, eps, x0, &x1, &k);
  281.     fprintf(output, "\nMethod_Chebysheva\n");
  282.     fprintf(output, "Solution = %Le\n", x1);
  283.     fprintf(output, "With iteration %d\n", k);
  284.     Method_Hord(k_max, eps, x0, &x2, &k);
  285.     fprintf(output, "\nMethod_Hord\n");
  286.     fprintf(output, "Solution = %Le\n", x2);
  287.     fprintf(output, "With iteration %d\n", k);
  288.     Method_Parabol(k_max, eps, x0, &x3, &k);
  289.     fprintf(output, "\nMethod_Parabol\n");
  290.     fprintf(output, "Solution = %Le\n", x3);
  291.     fprintf(output, "With iteration %d\n", k);
  292.     Method_Reverse_Interpolation(k_max, eps, x0, &x2, &k);
  293.     fprintf(output, "\nMethod_Reverse_Interpolation\n");
  294.     fprintf(output, "Solution = %Le\n", x2);
  295.     fprintf(output, "With iteration %d\n", k);
  296.     Method_Eitkena(k_max, eps, x0, &x3, &k);
  297.     fprintf(output, "\nMethod_Eitkena\n");
  298.     fprintf(output, "Solution = %Le\n", x3);
  299.     fprintf(output, "With iteration %d\n", k);
  300.     return 0;
  301. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top