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

📄 root.c

📁 掌握如何用C来实现各种算法
💻 C
字号:
/*****************************************************************************
*                                                                            *
*  -------------------------------- root.c --------------------------------  *
*                                                                            *
*****************************************************************************/

#include <math.h>

#include "nummeths.h"

/*****************************************************************************
*                                                                            *
*  --------------------------------- root ---------------------------------  *
*                                                                            *
*****************************************************************************/

int root(double (*f)(double x), double (*g)(double x), double *x, int *n,
   double delta) {

int                satisfied,
                   i;

/*****************************************************************************
*                                                                            *
*  Use Newton's method to find a root of f.                                  *
*                                                                            *
*****************************************************************************/

i = 0;
satisfied = 0;

while (!satisfied && i + 1 < *n) {

   /**************************************************************************
   *                                                                         *
   *  Determine the next iteration of x.                                     *
   *                                                                         *
   **************************************************************************/

   x[i + 1] = x[i] - (f(x[i]) / g(x[i]));

   /**************************************************************************
   *                                                                         *
   *  Determine whether the desired approximation has been obtained.         *
   *                                                                         *
   **************************************************************************/

   if (fabs(x[i + 1] - x[i]) < delta)
      satisfied = 1;

   /**************************************************************************
   *                                                                         *
   *  Prepare for the next iteration.                                        *
   *                                                                         *
   **************************************************************************/

   i++;

}

/*****************************************************************************
*                                                                            *
*  Even without iterating, indicate that one value has been stored in x.     *
*                                                                            *
*****************************************************************************/

if (i == 0)
   *n = 1;
else
   *n = i + 1;

/*****************************************************************************
*                                                                            *
*  Return whether a root was found or the maximum iterations were reached.   *
*                                                                            *
*****************************************************************************/

if (satisfied)
   return 0;
else
   return -1;

}

⌨️ 快捷键说明

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