Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <locale.h>
- #include <math.h>
- float *func(float xl, float xr) {
- float res[2];
- float n = 0;
- float X = (xl + xr) / 2;
- while (fabs(X - cos(X)) > 0.000001) {
- n++;
- if (X > cos(X)) {
- xr = X;
- X = (xr + xl) / 2;
- } else if (X < cos(X)) {
- xl = X;
- X = (xr + xl) / 2;
- }
- }
- res[0] = X;
- res[1] = n;
- return res;
- }
- float *func2(float xl1, float xr1) {
- float res1[2];
- float n1 = 0;
- float X1 = (xl1 + xr1) / 2;
- while (fabs(X1 - cos(X1)) > 0.00000001) {
- n1++;
- if (X1 > cos(X1)) {
- xr1 = X1;
- X1 = (xr1 + xl1) / 2;
- } else if (X1 < cos(X1)) {
- xl1 = X1;
- X1 = (xr1 + xl1) / 2;
- }
- }
- res1[0] = X1;
- res1[1] = n1;
- return res1;
- }
- float *func3(float xl2, float xr2, float k) {
- float res2[2];
- float n2 = 0;
- float X2 = (xl2 + xr2) / 2;
- while (fabs(X2 - k * cos(X2)) > 0.000001) {
- n2++;
- if (X2 > k * cos(X2)) {
- xr2 = X2;
- X2 = (xr2 + xl2) / 2;
- } else if (X2 < k * cos(X2)) {
- xl2 = X2;
- X2 = (xr2 + xl2) / 2;
- }
- }
- res2[0] = X2;
- res2[1] = n2;
- return res2;
- }
- int main() {
- setlocale(0, "");
- float x = 0;
- int n = 0;
- float delta = 0.000001;
- float delta1 = 0.00000001;
- fflush(stdout);
- printf("\nУравнение: x-cos(x)=0. Погрешность: %f", delta);
- printf("\n № метода%8sx%15sN", "", "");
- while (fabs(x - cos(x)) > delta) {
- x = cos(x);
- n++;
- }
- printf("\n1%15s%.8f%7s%d", "", x, "", n);
- float *res;
- res = func(-1, 1);
- printf("\n2%15s%.8f%7s%d", "", res[0], "", (int) res[1]);
- x = 0;
- n = 0;
- while (fabs(x - cos(x)) > delta) {
- x = x - ((x - cos(x)) / (1 + sin(x)));
- n++;
- }
- printf("\n3%15s%.8f%7s%d", "", x, "", n);
- printf("\n\nУравнение: x-cos(x)=0. Погрешность: %.8f", delta1);
- printf("\n № метода%8sx%15sN", "", "");
- double x1 = 0;
- int n1 = 0;
- while (fabs(x1 - cos(x1)) > delta1) {
- x1 = cos(x1);
- n1++;
- }
- printf("\n1%15s%.8lf%7s%d", "", x1, "", n1);
- float *res1;
- res1 = func2(-1, 1);
- printf("\n2%15s%.8f%7s%d", "", res1[0], "", (int) res1[1]);
- x1 = 0;
- n1 = 0;
- while (fabs(x1 - cos(x1)) > delta1) {
- x1 = x1 - ((x1 - cos(x1)) / (1 + sin(x1)));
- n1++;
- }
- printf("\n3%15s%.8f%7s%d", "", x1, "", n1);
- printf("\n\nУравнение: x - k * cos(x) = 0. k = 5. Погрешность: %f", delta);
- printf("\n № метода%8sx%15sN", "", "");
- double x2 = 0;
- int n2 = 0;
- double k = 5;
- while (fabs(x2 - k * cos(x2)) > 0.000001) {
- x2 = k * cos(x2);
- n2++;
- }
- printf("\n1%15s%.8lf%7s%d", "", x2, "", n2);
- float *res2;
- res2 = func3(-5, 5, 5);
- printf("\n2%15s%.8f%7s%d", "", res2[0], "", (int) res2[1]);
- x2 = 0;
- n2 = 0;
- while (fabs(x2 - k * cos(x2)) > delta) {
- x2 = x2 - ((x2 - k * cos(x2)) / (1 + k * sin(x2)));
- n2++;
- }
- printf("\n3%15s%.8lf%7s%d", "", x2, "", n2);
- printf("\n\nУравнение: x - k * cos(x) = 0, k = 10. Погрешность: %f", delta);
- printf("\n № метода%8sx%15s№", "", "");
- double x3 = 0;
- int n3 = 0;
- double k1 = 10;
- while (fabs(x3 - k1 * cos(x3)) > 0.000001) {
- x3 = k * cos(x3);
- n3++;
- }
- printf("\n1%15s%.8lf%7s%d", "", x3, "", n3);
- float *res3;
- res3 = func3(-10, 10, 10);
- printf("\n2%15s%.8f%7s%d", "", res3[0], "", (int) res3[1]);
- x3 = 0;
- n3 = 0;
- while (fabs(x3 - k1 * cos(x3)) > delta) {
- x3 = x3 - ((x3 - k1 * cos(x3)) / (1 + k1 * sin(x3)));
- n3++;
- }
- printf("\n3%15s%.8lf%7s%d", "", x3, "", n3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement