牛頓迭代法求根

zhongta發表於2024-05-12
#include<stdio.h>
#include<math.h>

double Solut(double a,double b,double c,double d);
int main(void){

double a,b,c,d;
        scanf("%lf,%lf,%lf,%lf",&a,&b,&c,&d);
        printf("result is %lf",Solut(a,b,c,d));
}
//x=x0-f(x0)/f'(x0)
double Solut(double a,double b,double c,double d){
        double x=1,x0,f,f1;
        do{
            x0=x;
            f=(a*pow(x,2)+b*pow(x,1)+c)*x+d;
            f1=(3*a*x+2*b)*x+c;
            x=x0-f/f1;
        }while(fabs(x-x0)>=1e-6);

        return x;
}


相關文章