home *** CD-ROM | disk | FTP | other *** search
- # include <math.h>
- float AFunction(float X)
- {
- float _fret;
-
- _fret = (2.0 * X + 3.0) * (X - 3.0);
- return(_fret);
- }
-
- float AFunctionPrime(float X)
- {
- float _fret;
-
- _fret = 4.0 * X - 3.0;
- return(_fret);
- }
-
- char RootBracketed(float x1, float x2)
- {
- char _fret;
- char result;
-
- if ( ((x1 > 0.0) && (x2 > 0.0)) || ((x1 < 0.0) && (x2 < 0.0)) ) {
- result = 0;
- }
- else {
- result = 1;
- }
- _fret = result;
- return(_fret);
- }
-
- float Minimum(float x1, float x2)
- {
- float _fret;
- float result;
-
- if ( x1 < x2 ) {
- result = x1;
- }
- else {
- result = x2;
- }
- _fret = result;
- return(_fret);
- }
-
- float BrentRoots(float x1,float x2,float Tolerance,
- int maxIterations,float *valueAtRoot,int *error)
- {
- float _fret;
- # define FPP 1.0e-11
- # define nearzero 1.0e-10
- float result;
- float AA;
- float BB;
- float CC;
- float DD;
- float EE;
- float FA;
- float FB;
- float FC;
- float Tol1;
- float PP;
- float QQ;
- float RR;
- float SS;
- float XM;
- int i;
- int j;
- int k;
- char done;
-
- i = 0;
- done = 0;
- (*error) = 0;
- AA = x1;
- BB = x2;
- FA = AFunction(AA);
- FB = AFunction(BB);
- if ( ! (RootBracketed(FA,FB)) ) {
- (*error) = 1;
- }
- else {
- FC = FB;
- do {
- if ( ! (RootBracketed(FC,FB)) ) {
- CC = AA;
- FC = FA;
- DD = BB - AA;
- EE = DD;
- }
- if ( fabs(FC) < fabs(FB) ) {
- AA = BB;
- BB = CC;
- CC = AA;
- FA = FB;
- FB = FC;
- FC = FA;
- }
- Tol1 = 2.0 * FPP * fabs(BB) + 0.5 * Tolerance;
- XM = 0.5 * (CC - BB);
- if ( (fabs(XM) <= Tol1) || (fabs(FA) < nearzero) ) {
- result = BB;
- done = 1;
- (*valueAtRoot) = AFunction(result);
- }
- else {
- if ( (fabs(EE) >= Tol1) && (fabs(FA) > fabs(FB)) ) {
- SS = FB / FA;
- if ( fabs(AA - CC) < nearzero ) {
- PP = 2.0 * XM * SS;
- QQ = 1.0 - SS;
- }
- else {
- QQ = FA / FC;
- RR = FB / FC;
- PP = SS * (2.0 * XM * QQ * (QQ - RR) - (BB - AA) * (RR - 1.0));
- QQ = (QQ - 1.0) * (RR - 1.0) * (SS - 1.0);
- }
- if ( PP > nearzero ) {
- QQ = -QQ;
- }
- PP = fabs(PP);
- if ( (2.0 * PP) < Minimum(3.0 * XM * QQ - fabs(Tol1 * QQ),fabs(EE * QQ)) ) {
- EE = DD;
- DD = PP / QQ;
- }
- else {
- DD = XM;
- EE = DD;
- }
- }
- else {
- DD = XM;
- EE = DD;
- }
- AA = BB;
- FA = FB;
- if ( fabs(DD) > Tol1 ) {
- BB = BB + DD;
- }
- else {
- if ( XM > 0.0 ) {
- BB = BB + fabs(Tol1);
- }
- else {
- BB = BB - fabs(Tol1);
- }
- }
- FB = AFunction(BB);
- i = 2;
- }
- } while ( ! (done || (i == maxIterations)) );
- if ( i == maxIterations ) {
- (*error) = 2;
- }
- }
- _fret = result;
- return(_fret);
- }
-
- float BisectionRoots(float x1, float x2, float Tolerance,
- int maxIterations,float *valueAtRoot,int *error)
- {
- float _fret;
- # define FPP 1.0e-11
- # define nearzero 1.0e-10
- float result;
- float FA;
- float FB;
- float FC;
- float XA;
- float XB;
- float XC;
- int i;
- int j;
- int k;
- char done;
-
- (*error) = 0;
- i = 0;
- done = 0;
- XA = x1;
- XB = x2;
- FA = AFunction(XA);
- FB = AFunction(XB);
- if ( ! (RootBracketed(FA,FB)) ) {
- (*error) = 1;
- }
- else {
- do {
- XC = (XA + XB) / 2.0;
- FC = AFunction(XC);
- if ( RootBracketed(FA,FC) ) {
- XB = XC;
- }
- else {
- XA = XC;
- FA = FC;
- }
- if ( fabs(XB - XA) <= Tolerance ) {
- done = 1;
- result = (XA + XB) / 2.0;
- (*valueAtRoot) = AFunction(result);
- }
- i = i + 1;
- } while ( ! (done || (i == maxIterations)) );
- if ( i == maxIterations ) {
- (*error) = 2;
- }
- }
- _fret = result;
- return(_fret);
- }
-
- float NewtonRoots(float x1,float Tolerance,float maxIterations,
- float *valueAtRoot, int *error)
- {
- float _fret;
- # define FPP 1.0e-11
- # define nearzero 1.0e-10
- float result;
- float lastX;
- float FX;
- float FPX;
- float X;
- float XP;
- int i;
- int j;
- int k;
- char done;
-
- i = 0;
- (*error) = 0;
- done = 0;
- X = x1;
- FX = AFunction(X);
- do {
- if ( fabs(X) < nearzero ) {
- (*error) = 1;
- }
- else {
- FPX = AFunctionPrime(X);
- if ( fabs(FPX) < nearzero ) {
- (*error) = 2;
- }
- else {
- lastX = X;
- X = X - FX / FPX;
- FX = AFunction(X);
- if ( fabs(X - lastX) <= Tolerance ) {
- done = 1;
- result = X;
- (*valueAtRoot) = AFunction(X);
- }
- i = i + 1;
- }
- }
- } while ( ! (done || (i == maxIterations) || ((*error) != 0)) );
- if ( i == maxIterations ) {
- (*error) = 3;
- }
- _fret = result;
- return(_fret);
- }
-