#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double f(double x)
{
return (x*x)*log10(x)-3.8;
}
double psi(double x, double *x1)
{
double h=(*x1-x);
*x1=x;
return -1*h/(f(x+h)-f(x));
}
int main()
{
double x,x1,p;
double eps;
int mt,i;
double delta;
printf ("Vvedite: x0, xl, epsilon, max itexation\n");
scanf("%lf",&x);
scanf("%lf",&x1);
scanf("%lf",eps);
scanf("%d",&mt);
for(i=1; i<=mt;i++)
{
p=psi(x,&x1);
if(p==0.0)
{
printf("Error,psi(x)=0!\n");
}
delta=f(x)*p;
x=x+delta;
printf("x=%lf\n",x);
if(fabs(delta)<eps)break;
}
if(i>=mt)
{
printf("Error max iterations\n");
}
printf("x=%lf \n",x);
printf("f(x)=%lf",f(x));
}
1 ответ
Легкое ощущение, что мы понимаем под методом Вегстейна разные вещи...
Вот как это делал бы я:
#include <stdio.h>
#include <math.h>
double f(double x) { return x * x * log10(x) - 3.8; }
int main()
{
double x0, x1, f0, f1, eps;
int mt;
printf("Input x0, xl, epsilon, max itexation\n");
scanf("%lf %lf %lf %d", &x0, &x1, &eps, &mt);
f0 = f(x0);
f1 = f(x1);
while (fabs(x1 - x0) > eps)
{
double x = x1 - f1 * (x1 - x0) / (f1 - f0);
f0 = f1;
f1 = f(x);
x0 = x1;
x1 = x;
if (--mt == 0)
{
printf("Iterations limit reached; x = %lf\n", x);
return 0;
}
}
printf("Solution: x = %lf\n", x1);
}
scanf("%lf",eps);
- прошло?... – Harry 19 окт '20 в 12:00