C/C++の宿題やらせてください。6代目。
■ このスレッドは過去ログ倉庫に格納されています
0096デフォルトの名無しさん
NGNG#include <stdio.h>
#include <math.h>
double f1(double x) { return cos(x) - pow(x, 4.0); }
double f2(double x) { return exp(-pow(x, 4.0)) - sin(x); }
double newton(double (*f)(double), double x0, double x1) {
double x, y, y0, y1;
y0 = f(x0); y1 = f(x1);
while (fabs(x0 - x1) > 0.0000001) {
printf("%f = f(%1.7f) %f = f(%1.7f)\n", y0, x0, y1, x1);
x = (x0 + x1) / 2; y = f(x);
if (y0 * y < 0) {
x1 = x; y1 = y;
} else {
x0 = x; y0 = y;
}
}
return x;
}
int main() {
printf("%1.7f\n", newton(f1, 0.37, 1.0));
printf("%1.7f\n", newton(f2, 0.37, 1.0));
return 0;
}
■ このスレッドは過去ログ倉庫に格納されています