⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newtoniteration.c

📁 牛顿迭代法的源程序
💻 C
字号:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define N 10
#define EPS 1e-3
#define ETA 1e-4
void main()
{
 float f(float);
 float f1(float);
 float x0,y0;
 float Newton(float(*)(float),float(*)(float),float);
 clrscr();
 printf("Please input x0:\n");
 scanf("%f",&x0);
 printf("x(0)=%f\n",x0);
 y0=Newton(f,f1,x0);
 printf("\nThe root of the equation is x=%f\n",y0);
 getch();
 }
 float Newton(float(*f)(float),float(*f1)(float),float x0)
 {
 float x1,d;
 int k=0;
 do
 {
   x1=x0-f(x0)/f1(x0);
   if((k++>N)||(fabs(f1(x1))<EPS))
   {
     printf("\nNewton method failed");
     getch();
     exit();
    }
    d=(fabs(x1)<1?x1-x0:(x1-x0)/x1);
    x0=x1;
    printf("x(%d)=%f\t",k,x0);
    }
   while(fabs(d)>EPS&&fabs(f(x1))>ETA);
   return x1;
   }
   float f(float x)
   {
   return 2*exp(-x)-sin(x);
   }
   float f1(float x)
   {
   return 2*(-exp(-x))-cos(x);
   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -