/* Function name      : computeNewtonRaphson
* Description         : Computes a root of an equation using the newton raphson algorithm.
* Parameter(s)        : 
*     nPn              [in]  The initial value P0.
*     fFunction        [in]  The function to evaluate f(Pn).
*     fFunction1       [in]  The function to evaluate f'(Pn).
*     nMaxIter         [in]  The allowable maximum number of iteration.
*     nPrint           [in]  Flag for printing the output on screen,
*     pOutput          [out]  The root.
*     pDp              [out]  The difference on iteration.
*     pIterCnt         [out]  The actual number of iteration performed.
* Return              :     
*    int     0 -  Success, -1 -  Iteration runout , -2 -  Divide error.
*/
int _cdecl 
computeNewtonRaphson(double nPn,double (*fFunction)(double),double (*fFunction1)(double),int nMaxIter,int nPrint,double *pOutput,double *pDp,int* pIterCnt)
{
    int nRet = -1;                    /* Initialize as iteration runout */      
    static const double nDelta    = 10e-8;  /* Tolerances*/
    static const double nEpsilon  = 10e-8;         
    static const double nSmall    = 10e-8;          
    double nDf;     /* derivative of a function */
    double nDp;     /* f(Pn)/f'(Pn) */
    double nPn1;    /* Pn + 1  */
    double nYn;     /* f(Pn)  */
    double nYn1;    /* f(Pn+1)  */
    int    i;
    double nRelErr;                                
            
    nYn = fFunction(nPn);    /* Compute the function value */
    if(nPrint)
    {
        printf("\n\n N    %-10s  %-10s  %-10s  ","Pn","Pn+1","Ek+1");
        printf("\n------------------------------------------------------------------");
    }
    for(i = 1; i <= nMaxIter && nRet == -1 ; i++)
    {
        nDf = fFunction1(nPn); /* Get the derivative of Pn */
        if(nDf == 0)
        {
            /* Divide error */
            nRet = -2;
            nDp  = 0;
        }
        else
            nDp = nYn/nDf;    

        nPn1  = nPn - nDp;    nYn1  = fFunction(nPn1);

        if(nPrint)
            printf("\n %2d  %2.8lf  %2.8lf  %2.8lf  ",i-1,nPn,nPn1,nPn1 - nPn);

        nRelErr = 2 * fabs(nDp)/(fabs(nPn1) + nSmall);

        if(nRelErr < nDelta && fabs(nYn1) < nEpsilon)
        {
            /* Check for convergence */
            if(nRet != -2)
            {
                nRet = 0;  
                *pOutput = nPn1;
                *pDp     = nDp;
                *pIterCnt = i;
            }
        }

        nPn = nPn1;  nYn = nYn1;
    }
    if(nPrint)
        printf("\n------------------------------------------------------------------");
    return nRet;
}
Пример #2
0
void
FunctionDPCCallback::DoDPC(DPCQueue* queue)
{
	fFunction(fArgument);

	if (fOwner != NULL)
		fOwner->Recycle(this);
}
Пример #3
0
string JSFormater::fStatement(){
	if(is_tag(cur_token,"function")){
		return fFunction();
	}else if(is_tag(cur_token,"var")){
		return fDeclare();
	}else if(is_tag(cur_token,"for")){
		return fFor();
	}
	//... do while...
	else if(is_tag(cur_token,"{")){
		return fBlock();
	}else if(is_tag(cur_token,";")){
		string rtn=cur_token.value;
		get_next_token();
		return rtn;
	}else{
		return fExpr();
	}
}
Пример #4
0
string JSFormater::fPriExpr(){
	string rtn;
	if(is_tag(cur_token,"function")){
		return fFunction();
	}else if(is_tag(cur_token,"{")){
		return fObject();
	}else if(is_tag(cur_token,"[")){
		return fArray();
	}else if(is_tag(cur_token,"(")){
		rtn.append("(");
		get_next_token();
		rtn.append(fExpr());
		if(!is_tag(cur_token,")"))
			throw 1;
		else{
			rtn.append(")");
			get_next_token();
		}
	}else{
		rtn=cur_token.value;
		get_next_token();
	}
	return rtn;
}
Пример #5
0
void    pfConsoleCmd::Execute( int32_t numParams, pfConsoleCmdParam *params, void (*PrintFn)( const char * ) )
{
    fFunction( numParams, params, PrintFn );
}
Пример #6
0
uint64 
Bench::TimeElapsed() const{
    uint64 start = GetTimeMs64();
    fFunction();
    return GetTimeMs64() - start;
}
Пример #7
0
		virtual void call1()
		{
			fFunction();
		}