File: nrt.c

    1   /*
    2    * File: nrt.c
    3    *
    4    * MATLAB Coder version            : 2.6
    5    * C/C++ source code generated on  : 02-Apr-2014 12:52:38
    6    */
    7   
    8   /* Include files */
    9   #include "nrt.h"
   10   
   11   /* Function Definitions */
   12   
   13   /*
   14    * This function will use a Newton Search Technique to find
   15    * the nth root of a number, a, to the tolerance, tol.
   16    *  The square root
   17    *  nrt(10,2), or nrt(10,2,1e-9)
   18    *  The "n" root
   19    *  nrt(10,n), or nrt(10,n,1e-9)
   20    * Arguments    : double varargin_1
   21    *                double varargin_2
   22    *                double varargin_3
   23    *                double *nth_rt
   24    *                double *iterations
   25    *                double hstry[50]
   26    * Return Type  : void
   27    */
   28   void nrt(double varargin_1, double varargin_2, double varargin_3, double *nth_rt,
   29            double *iterations, double hstry[50])
   30   {
   31     int notDone;
   32     double a;
   33     int cnt;
   34     double slope;
   35     if (varargin_1 < 0.0) {
   36       *nth_rt = 0.0;
   37       *iterations = 0.0;
   38       memset(&hstry[0], 0, 50U * sizeof(double));
   39     } else {
   40       /*  Given, "a", this function finds the nth root of a */
   41       /*  number by finding where: x^n-a=0. */
   42       notDone = 1;
   43       *nth_rt = 0.0;
   44   
   45       /* Refined Guess Initialization */
   46       a = 1.0;
   47   
   48       /* Initial Guess */
   49       cnt = -1;
   50       memset(&hstry[0], 0, 50U * sizeof(double));
   51       hstry[0] = 1.0;
   52       while (notDone != 0) {
   53         cnt++;
   54   
   55         /*  Our function is f=a^n-b and it's derivative is n*a^(n-1). */
   56         slope = varargin_2 * pow(a, varargin_2 - 1.0);
   57   
   58         /* square */
   59         *nth_rt = -((pow(a, varargin_2) - varargin_1) - slope * a) / slope;
   60   
   61         /* The new guess */
   62         hstry[cnt] = *nth_rt;
   63         if (fabs(*nth_rt - a) < varargin_3) {
   64           /* Break if it's converged */
   65           notDone = 0;
   66         } else if (cnt + 1 > 49) {
   67           /* after 50 iterations, stop */
   68           notDone = 0;
   69           *nth_rt = 0.0;
   70         } else {
   71           a = *nth_rt;
   72         }
   73       }
   74   
   75       /*  Determine iterations */
   76       *iterations = 0.0;
   77       notDone = 0;
   78       while ((notDone < 50) && (hstry[notDone] != 0.0)) {
   79         (*iterations)++;
   80         notDone++;
   81       }
   82   
   83       /* iterations=length(find(hstry~=0)); */
   84     }
   85   }
   86   
   87   /*
   88    * File trailer for nrt.c
   89    *
   90    * [EOF]
   91    */
   92