File: nrt.c

    1   /*
    2    * nrt.c
    3    *
    4    * Code generation for function 'nrt'
    5    *
    6    */
    7   
    8   /* Include files */
    9   #include "rt_nonfinite.h"
   10   #include "nrt.h"
   11   #include "eml_error.h"
   12   #include "nrt_data.h"
   13   
   14   /* Variable Definitions */
   15   static emlrtRSInfo emlrtRSI = { 23, "nrt",
   16     "C:\\MATLAB\\MATLAB_WORK\\MATLAB_Coder\\ML_to_C_Presentation\\Demo\\Newton-Raphson\\nrt.m"
   17   };
   18   
   19   static emlrtRSInfo b_emlrtRSI = { 13, "newtonSearchAlgorithm",
   20     "C:\\MATLAB\\MATLAB_WORK\\MATLAB_Coder\\ML_to_C_Presentation\\Demo\\Newton-Raphson\\newtonSearchAlgorithm.m"
   21   };
   22   
   23   static emlrtRSInfo c_emlrtRSI = { 31, "newtonSearchAlgorithm",
   24     "C:\\MATLAB\\MATLAB_WORK\\MATLAB_Coder\\ML_to_C_Presentation\\Demo\\Newton-Raphson\\newtonSearchAlgorithm.m"
   25   };
   26   
   27   static emlrtRSInfo d_emlrtRSI = { 32, "newtonSearchAlgorithm",
   28     "C:\\MATLAB\\MATLAB_WORK\\MATLAB_Coder\\ML_to_C_Presentation\\Demo\\Newton-Raphson\\newtonSearchAlgorithm.m"
   29   };
   30   
   31   static emlrtRSInfo e_emlrtRSI = { 37, "mpower",
   32     "C:\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\ops\\mpower.m" };
   33   
   34   static emlrtRSInfo f_emlrtRSI = { 42, "power",
   35     "C:\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\ops\\power.m" };
   36   
   37   static emlrtRSInfo g_emlrtRSI = { 56, "power",
   38     "C:\\MATLAB\\R2014a\\toolbox\\eml\\lib\\matlab\\ops\\power.m" };
   39   
   40   static emlrtBCInfo emlrtBCI = { 1, 50, 16, 9, "h", "newtonSearchAlgorithm",
   41     "C:\\MATLAB\\MATLAB_WORK\\MATLAB_Coder\\ML_to_C_Presentation\\Demo\\Newton-Raphson\\newtonSearchAlgorithm.m",
   42     0 };
   43   
   44   /* Function Definitions */
   45   void nrt(const emlrtStack *sp, real_T varargin_1, real_T varargin_2, real_T
   46            varargin_3, real_T *nth_rt, real_T *iterations, real_T hstry[50])
   47   {
   48     int32_T notDone;
   49     real_T a;
   50     int32_T cnt;
   51     real_T slope;
   52     emlrtStack st;
   53     emlrtStack b_st;
   54     emlrtStack c_st;
   55     emlrtStack d_st;
   56     emlrtStack e_st;
   57     emlrtStack f_st;
   58     st.prev = sp;
   59     st.tls = sp->tls;
   60     b_st.prev = &st;
   61     b_st.tls = st.tls;
   62     c_st.prev = &b_st;
   63     c_st.tls = b_st.tls;
   64     d_st.prev = &c_st;
   65     d_st.tls = c_st.tls;
   66     e_st.prev = &d_st;
   67     e_st.tls = d_st.tls;
   68     f_st.prev = &e_st;
   69     f_st.tls = e_st.tls;
   70   
   71     /* This function will use a Newton Search Technique to find */
   72     /* the nth root of a number, a, to the tolerance, tol. */
   73     /*  The square root */
   74     /*  nrt(10,2), or nrt(10,2,1e-9) */
   75     /*  The "n" root */
   76     /*  nrt(10,n), or nrt(10,n,1e-9) */
   77     if (varargin_1 < 0.0) {
   78       *nth_rt = 0.0;
   79       *iterations = 0.0;
   80       memset(&hstry[0], 0, 50U * sizeof(real_T));
   81     } else {
   82       st.site = &emlrtRSI;
   83   
   84       /*  Given, "a", this function finds the nth root of a */
   85       /*  number by finding where: x^n-a=0. */
   86       notDone = 1;
   87       *nth_rt = 0.0;
   88   
   89       /* Refined Guess Initialization */
   90       a = 1.0;
   91   
   92       /* Initial Guess */
   93       cnt = 0;
   94       memset(&hstry[0], 0, 50U * sizeof(real_T));
   95       hstry[0] = 1.0;
   96       while (notDone != 0) {
   97         cnt++;
   98         b_st.site = &b_emlrtRSI;
   99   
  100         /*  Our function is f=a^n-b and it's derivative is n*a^(n-1). */
  101         c_st.site = &c_emlrtRSI;
  102         d_st.site = &e_emlrtRSI;
  103         e_st.site = &f_emlrtRSI;
  104         if ((a < 0.0) && (muDoubleScalarFloor(varargin_2) != varargin_2)) {
  105           f_st.site = &g_emlrtRSI;
  106           eml_error(&f_st);
  107         }
  108   
  109         c_st.site = &d_emlrtRSI;
  110         d_st.site = &e_emlrtRSI;
  111         e_st.site = &f_emlrtRSI;
  112         if ((a < 0.0) && (muDoubleScalarFloor(varargin_2 - 1.0) != varargin_2 -
  113                           1.0)) {
  114           f_st.site = &g_emlrtRSI;
  115           eml_error(&f_st);
  116         }
  117   
  118         slope = varargin_2 * muDoubleScalarPower(a, varargin_2 - 1.0);
  119   
  120         /* square */
  121         *nth_rt = -((muDoubleScalarPower(a, varargin_2) - varargin_1) - slope * a)
  122           / slope;
  123   
  124         /* The new guess */
  125         hstry[emlrtDynamicBoundsCheckFastR2012b(cnt, 1, 50, &emlrtBCI, &st) - 1] =
  126           *nth_rt;
  127         if (muDoubleScalarAbs(*nth_rt - a) < varargin_3) {
  128           /* Break if it's converged */
  129           notDone = 0;
  130         } else if (cnt > 49) {
  131           /* after 50 iterations, stop */
  132           notDone = 0;
  133           *nth_rt = 0.0;
  134         } else {
  135           a = *nth_rt;
  136         }
  137   
  138         emlrtBreakCheckFastR2012b(emlrtBreakCheckR2012bFlagVar, &st);
  139       }
  140   
  141       /*  Determine iterations */
  142       *iterations = 0.0;
  143       notDone = 0;
  144       while ((notDone < 50) && (hstry[notDone] != 0.0)) {
  145         (*iterations)++;
  146         notDone++;
  147         emlrtBreakCheckFastR2012b(emlrtBreakCheckR2012bFlagVar, sp);
  148       }
  149   
  150       /* iterations=length(find(hstry~=0)); */
  151     }
  152   }
  153   
  154   /* End of code generation (nrt.c) */
  155