牛顿迭代法求根

/*
 Date: 07/03/19 15:40
 Description: 用牛顿迭代法求下面方程在1附近的近视根
          f(x)=a*x*x*x+b*x*x+c*x+d=0

        x1=x0-f(x)/f'(x)
*/

 1 #include<stdio.h>
 2 #include<math.h>
 3 float soult(float a,float b,float c,float d);
 4  
 5 int main()
 6 {
 7    float a,b,c,d;
 8    printf("Please enter a、b、c and d:\n");
 9    scanf("%f %f %f %f",&a,&b,&c,&d);
10    printf("x=%10.7f\n",soult(a,b,c,d));
11  
12    return 0;
13 }
14 float soult(float a,float b,float c,float d)
15 {
16    float x1,x0,f,f1;
17    x1=1.0;
18    do
19    {
20       x0=x1;
21       //f=((a*x0+b)*x0+c)*x0+d;
22       //f1=(3*a*x0+2*b)*x0+c;
23       f=(a*x0*x0*x0)+(b*x0*x0)+(c*x0)+d;//f表示函数f(x)
24       f1=(3*a*x0*x0)+(2*b*x0)+c;//表示函数f(x)的导数 
25       x1=x0-f/f1;
26    } while(fabs(x1-x0)>=1e-5);
27  return x1;
28 }

 

posted @ 2019-03-07 16:19  sinlearn  阅读(638)  评论(0编辑  收藏  举报