C/C++の宿題やらせてください。9代目
■ このスレッドは過去ログ倉庫に格納されています
0517デフォルトの名無しさん
NGNG#include <math.h>
#include <stdlib.h>
#include <stdio.h>
double
newton(double (*f)(double), double (*df)(double), double x, double h, int imax)
{
int c;
while (fabs(f(x))>h && c<imax){x -= f(x)/df(x); c++;}
return c>=imax ? 0.0/0.0 : x;
}
#define IMAX 1000000000
double f(double x){return 3*x*x*x - 2*x*x + x - 11;}
double df(double x){return 9*x*x - 4*x + 1;}
int
main(int argc, char **argv)
{
double x,y,h;
int i;
x = argc>1 ? atof(argv[1]) : 10;
h = argc>2 ? atof(argv[2]) : 0.001;
i = argc>3 ? atoi(argv[3]) : IMAX;
printf("x0 = %g; delta = %g; iter_max = %d;\n", x,h,i);
y = newton(f, df, x, h, i);
if (y == y) printf("%.10e\n", y); else printf("failed\n");
return 0;
}
■ このスレッドは過去ログ倉庫に格納されています