#include <stdio.h>
#include <math.h>
double funktion(double x)
{
return -2*x*x+4;
}
double ableitung(double x)
{
return -4*x;
}
void ausgabe(int k, double x_k, double fx_k)
{
printf("%d %G %G\n", k
, x_k
, fx_k
); }
double newton_operator(double x)
{
return x - funktion(x) / ableitung(x);
}
int main(void)
{
double x1 = 0, // x_k
x2 = -1; // x_k+1
int k = 0;
double epsilon = 1E-10;
while(fabs(x2
-x1
) >= epsilon
) {
x1 = x2;
k = k+1;
x2 = newton_operator(x1);
ausgabe(k, x1, funktion(x1));
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+Cgpkb3VibGUgZnVua3Rpb24oZG91YmxlIHgpCnsKCXJldHVybiAtMip4KngrNDsKfQoKZG91YmxlIGFibGVpdHVuZyhkb3VibGUgeCkKewoJcmV0dXJuIC00Kng7Cn0KCnZvaWQgYXVzZ2FiZShpbnQgaywgZG91YmxlIHhfaywgZG91YmxlIGZ4X2spCnsKCXByaW50ZigiJWQgJUcgJUdcbiIsIGssIHhfaywgZnhfayk7Cn0KCmRvdWJsZSBuZXd0b25fb3BlcmF0b3IoZG91YmxlIHgpCnsKCXJldHVybiB4IC0gZnVua3Rpb24oeCkgLyBhYmxlaXR1bmcoeCk7Cn0KCmludCBtYWluKHZvaWQpCnsKCWRvdWJsZSB4MSA9IDAsICAvLyB4X2sKCQl4MiA9IC0xOyAvLyB4X2srMQoJaW50IGsgPSAwOwoJZG91YmxlIGVwc2lsb24gPSAxRS0xMDsKCQoJd2hpbGUoZmFicyh4Mi14MSkgPj0gZXBzaWxvbikKCXsKCQl4MSA9IHgyOwoJCWsgPSBrKzE7CgkJeDIgPSBuZXd0b25fb3BlcmF0b3IoeDEpOwoJCWF1c2dhYmUoaywgeDEsIGZ1bmt0aW9uKHgxKSk7Cgl9CglyZXR1cm4gMDsKfQo=