Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- long double f(long double x)
- {
- return cos(x);
- }
- long double fp1(long double x)
- {
- return -sinl(x);
- }
- long double fp2(long double x)
- {
- return -cosl(x);
- }
- long double power(long double x, int n)
- {
- long double mult = 1;
- if (n == 0)
- {
- return 1;
- }
- int i = 0;
- for (i = 1; i <= n; i++)
- {
- mult *= x;
- }
- return mult;
- }
- void Simple_Iteration(int kmax, long double eps, long double x0, long double *x1,
- int *k)
- {
- long double tau;
- printf("\nMethod simple iteration\n");
- *x1 = x0;
- tau = -1.0 / fp1(x0);
- *k = 0;
- do
- {
- x0 = *x1;
- *x1 = x0 + tau * f(x0);
- *k += 1;
- if (*k > kmax)
- {
- break;
- }
- } while (!(fabsl(*x1 - x0) <= eps) && !(fabsl(f(*x1)) <= eps));
- if (*k > kmax)
- {
- printf("Solution not found\n");
- }
- else
- {
- printf("Solution = %Le\n", *x1);
- printf("With iteration %d\n", *k);
- }
- }
- void Method_Newton(int kmax, long double eps, long double x0, long double *x1, int *k)
- {
- printf("\nMethod Newton\n");
- *x1 = x0;
- *k = 0;
- do
- {
- x0 = *x1;
- *x1 = x0 - f(x0) / fp1(x0);
- *k += 1;
- if (*k > kmax)
- {
- break;
- }
- } while (!(fabsl(*x1 - x0) <= eps) && !(fabsl(f(*x1)) <= eps));
- if (*k > kmax)
- {
- printf("Solution not found\n");
- }
- else
- {
- printf("Solution = %Le\n", *x1);
- printf("With iteration %d\n", *k);
- }
- }
- void Method_Chebysheva(int kmax, long double eps, long double x0, long double *x1,
- int *k)
- {
- printf("\nMethod Chebusheva\n");
- *x1 = x0;
- *k = 0;
- do
- {
- x0 = *x1;
- *x1 =
- x0 - f(x0) / fp1(x0) - 0.5 * power(f(x0), 2) * fp2(x0) / power(fp1(x0), 3);
- *k += 1;
- if (*k > kmax)
- {
- break;
- }
- } while (!(fabsl(*x1 - x0) <= eps) && !(fabsl(f(*x1)) <= eps));
- if (*k > kmax)
- {
- printf("Solution not found\n");
- }
- else
- {
- printf("Solution = %Le\n", *x1);
- printf("With iteration %d\n", *k);
- }
- }
- void Method_Hord(int kmax, long double eps, long double x0, long double *x2, int *k)
- {
- long double tau, x1;
- printf("\nMethod Hord\n");
- *k = 0;
- tau = -1.0 / fp1(x0);
- x1 = x0 + tau * f(x0);
- *x2 = x1;
- x1 = x0;
- do
- {
- x0 = x1;
- x1 = *x2;
- *x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0));
- *k += 1;
- if (*k > kmax)
- {
- break;
- }
- } while (!(fabsl(*x2 - x1) <= eps) && !(fabsl(f(*x2)) <= eps));
- if (*k > kmax)
- {
- printf("Solution not found\n");
- }
- else
- {
- printf("Solution = %Le\n", *x2);
- printf("With iteration %d\n", *k);
- }
- }
- long double rr(long double x0, long double x1)
- {
- long double res = (f(x1) - f(x0)) / (x1 - x0);
- return res;
- }
- long double delta(long double x1, long double x2, long double root, long double rr1, long double rr2, int ind)
- {
- long double res;
- if (ind == 0)
- {
- res = (-((x2 - x1) * rr2 + rr1) + root) / (2 * rr2);
- }
- if (ind == 1)
- {
- res = (-((x2 - x1) * rr2 + rr1) - root) / (2 * rr2);
- }
- return res;
- }
- void Method_Parabol(int kmax, long double eps, long double x0, long double *x3,
- int *k)
- {
- long double x1, x2, delta0, delta1, delta2, rr1, rr2, root, tau;
- printf("\nMethod parabol\n");
- *k = 0;
- tau = -1.0 / fp1(x0);
- x1 = x0 + tau * f(x0);
- x2 = x1 + tau * f(x1);
- *x3 = x2;
- x2 = x1;
- x1 = x0;
- do
- {
- x0 = x1;
- x1 = x2;
- x2 = *x3;
- rr1 = rr(x1, x2);
- rr2 = (rr(x1, x2) - rr(x0, x1)) / (x2 - x0);
- root = sqrtl(power((x2 - x1) * rr2 + rr1, 2) - 4 * rr2 * f(x2));
- delta1 = delta(x1, x2, root, rr1, rr2, 0);
- delta2 = delta(x1, x2, root, rr1, rr2, 1);
- if (fabsl(delta1) < fabsl(delta2))
- {
- delta0 = delta1;
- }
- else
- {
- delta0 = delta2;
- }
- *x3 = x2 + delta0;
- *k += 1;
- if (*k > kmax)
- {
- break;
- }
- } while (!(fabsl(*x3 - x2) <= eps) && !(fabsl(f(*x3)) <= eps));
- if (*k > kmax)
- {
- printf("Solution not found\n");
- }
- else
- {
- printf("Solution = %Le\n", *x3);
- printf("With iteration %d\n", *k);
- }
- }
- void Method_Reverse_Interpolation(int kmax, long double eps, long double x0, long double *x2, int *k)
- {
- printf("\nMethod_reverse_interpolation\n");
- long double x1, tau;
- *k = 0;
- tau = -1.0 / fp1(x0);
- x1 = x0 + tau * f(x0);
- *x2 = x1;
- x1 = x0;
- do
- {
- x0 = x1;
- x1 = *x2;
- *x2 = -x0 * f(x1) / (f(x0) - f(x1)) - x1 * f(x0) / (f(x1) - f(x0));
- *k += 1;
- if (*k > kmax)
- {
- break;
- }
- } while (!(fabsl(*x2 - x1) <= eps) && !(fabsl(f(*x2)) <= eps));
- if (*k > kmax)
- {
- printf("Solution not found\n");
- }
- else
- {
- printf("Solution = %Le\n", *x2);
- printf("With iteration %d\n", *k);
- }
- }
- void Method_Eitkena(int kmax, long double eps, long double x0, long double *x3,
- int *k)
- {
- printf("\nMethod Eitkena\n");
- long double x2, x1, tau;
- tau = -1.0 / fp1(x0);
- *x3 = x0;
- *k = 0;
- do
- {
- x0 = *x3;
- x1 = x0 + tau * f(x0);
- x2 = x1 + tau * f(x1);
- *x3 = x2 + power(x2 - x1, 2) / (2 * x1 - x2 - x0);
- *k += 1;
- if (*k > kmax)
- {
- break;
- }
- } while (!(fabsl(*x3 - x2) <= eps) && !(fabsl(f(*x3)) <= eps));
- if (*k > kmax)
- {
- printf("Solution not found\n");
- }
- else
- {
- printf("Solution = %Le\n", *x3);
- printf("With iteration %d\n", *k);
- }
- }
- int main()
- {
- FILE *output = fopen("result.txt", "w");
- long double x0, x1, x2, x3;
- long double eps = 1e-10;
- int k;
- int k_max = 1e+4;
- x0 = 5;
- Simple_Iteration(k_max, eps, x0, &x1, &k);
- fprintf(output, "\nSimple_Iteration\n");
- fprintf(output, "Solution = %Le\n", x1);
- fprintf(output, "With iteration %d\n", k);
- Method_Newton(k_max, eps, x0, &x1, &k);
- fprintf(output, "\nMethod_Newton\n");
- fprintf(output, "Solution = %Le\n", x1);
- fprintf(output, "With iteration %d\n", k);
- Method_Chebysheva(k_max, eps, x0, &x1, &k);
- fprintf(output, "\nMethod_Chebysheva\n");
- fprintf(output, "Solution = %Le\n", x1);
- fprintf(output, "With iteration %d\n", k);
- Method_Hord(k_max, eps, x0, &x2, &k);
- fprintf(output, "\nMethod_Hord\n");
- fprintf(output, "Solution = %Le\n", x2);
- fprintf(output, "With iteration %d\n", k);
- Method_Parabol(k_max, eps, x0, &x3, &k);
- fprintf(output, "\nMethod_Parabol\n");
- fprintf(output, "Solution = %Le\n", x3);
- fprintf(output, "With iteration %d\n", k);
- Method_Reverse_Interpolation(k_max, eps, x0, &x2, &k);
- fprintf(output, "\nMethod_Reverse_Interpolation\n");
- fprintf(output, "Solution = %Le\n", x2);
- fprintf(output, "With iteration %d\n", k);
- Method_Eitkena(k_max, eps, x0, &x3, &k);
- fprintf(output, "\nMethod_Eitkena\n");
- fprintf(output, "Solution = %Le\n", x3);
- fprintf(output, "With iteration %d\n", k);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement