Пришлось почитать про метод линейной интерполяции
Вот код который правильный:
C |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| # define M_PI ((float)3.141592653589793)
# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
double x2,x1,x,fx1,fx2,e,n;
printf("Vvedite tochnost ");
scanf("%lf",&e);
n=0;
x1=0.8;
x2=1.2;
do
{
fx1=1-sin(x1)-log(x1);
fx2=1-sin(x2)-log(x2);
x=x2-(x2-x1)*fx2/(fx2-fx1);
x1=x2;
n+=1;
x2=x;
}
while ((fabs(x1-x2)>e));
printf("Koren=%lf,kolvo operatsii=%.1lf",x,n);
getch();
return;
} |
|
При вводе точности e=0.0001
мой код выдает: x=1.109956 , n=4
исходный код выдает: x=1.126237 , n=1
Проверяем так:
C |
1
2
3
4
5
| double a;
a=1-sin(1.109956)-log(1.109956);
printf("\n%lf\n", a);
a=1-sin(1.126237)-log(1.126237);
printf("\n%lf\n", a); |
|
В первом случае a=0.000001
Во втором случае a=-0.021682