#include <stdio.h> #include <math.h>
double F1(int,int,int,int,double); //源函数
double F2(int,int,int,int,double); //求解的函数的一阶导数函数
double Newton(int,int,int,int,double, double); int main(int argc, char *argv[]) { int a,b,c,d; double x0,x1 = 1; double e = pow(10,-5); printf("question : a * x * x * x + b * x * x + c * x + d = 0 \n"); printf("please input a,b,c,d\n"); scanf("%d,%d,%d,%d",&a,&b,&c,&d); x0 = Newton(a,b,c,d,x1,e); printf("the result is x = %f",x0); system("pause"); return 0; }
double F1(int a, int b, int c, int d, double x) { return a * x * x * x + b * x * x + c * x + d; }
double F2(int a, int b, int c, int d, double x) { return 3 * a * x * x + 2 * b * x + c; }
double Newton(int a, int b, int c, int d,double x, double e) { double x1; do { x1 = x; x = x1 - F1(a,b,c,d,x1) / F2(a,b,c,d,x1); }while (fabs(x1 - x) > e); return x; }
|