Non-Linear Roots
An implementation of the Newton-Raphson method to compute the root of a non-linear equation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | //-------------------------------------------------------------------- // Name: Shyam M Guthikonda // Email: shyamguth@gmail.com // URL: http://shy.am // Desc: Program to calculate the root of a non-linear function, utilizing // the Newton-Raphson method. //-------------------------------------------------------------------- //-------------------------------------------------------------------- // Include Files //-------------------------------------------------------------------- #include "math.h" #include "windows.h" //-------------------------------------------------------------------- // Globals //-------------------------------------------------------------------- unsigned int Iter; // Keeps track of the number of iterations each // root location method requires. Avoids the need // to pass an extra parameter by reference. ULONG ErrorFlags = 0; // Used to keep track of any errors. // Possible error flags enum ERRORS { ERRORS_DERIV_EQ_ZERO = 1, ERRORS_DENOM_EQ_ZERO = 2, ERRORS_MAX_ITERATION = 4, ERROR_FORCE_32BIT = 0x7FFFFFFF }; // Struct to hold the X and Y roots of a non-linear system struct Point { double X; double Y; }; //-------------------------------------------------------------------- // Name: F1() // Desc: The first function. //-------------------------------------------------------------------- double F1( double x, double y ) { return ( (x * x) + 1 - y ); } //-------------------------------------------------------------------- // Name: F2() // Desc: The second function. //-------------------------------------------------------------------- double F2( double x, double y ) { return ( 3 * cos( x ) - y ); } //-------------------------------------------------------------------- // Name: F1dx() // Desc: Derivative of the first function with respect to x. //-------------------------------------------------------------------- double F1dx( double x, double y ) { return ( 2 * x ); } //-------------------------------------------------------------------- // Name: F1dy() // Desc: Derivative of the first function with respect to y. //-------------------------------------------------------------------- double F1dy( double x, double y ) { return ( -1 ); } //-------------------------------------------------------------------- // Name: F2dx() // Desc: Derivative of the second function with respect to x. //-------------------------------------------------------------------- double F2dx( double x, double y ) { return ( -3 * sin( x ) ); } //-------------------------------------------------------------------- // Name: F2dy() // Desc: Derivative of the second function with respect to y. //-------------------------------------------------------------------- double F2dy( double x, double y ) { return ( -1 ); } //-------------------------------------------------------------------- // Name: Es() // Desc: Calculates the percent tolerance, given a desired number of // significant figures. //-------------------------------------------------------------------- double Es( unsigned int SigFigs ) { return (.5 * pow( 10.0, (2.0 - (double)SigFigs) )); } //-------------------------------------------------------------------- // Name: NewtonRaphson() // Desc: Determines the root of a non-linear function using the // Newton-Raphson method. //-------------------------------------------------------------------- Point NewtonRaphsonNonLinear( double Xi, double Yi, double Es, unsigned int Imax ) { double Ea = 1000.0, Denom; double Xrold = Xi, Yrold = Yi; Point Root; Iter = 0; Root.X = Xi; Root.Y = Yi; // No errors yet with this method! ErrorFlags = 0; while ( 1 ) { Xrold = Root.X; Yrold = Root.Y; Iter++; // Prevent division by 0. Denom = F1dx( Root.X, Root.Y ) * F2dy( Root.X, Root.Y ) - F1dy( Root.X, Root.Y ) * F2dx( Root.X, Root.Y ); if (Denom == 0.0) { // Set the given error flag and return the most recent root estimates. ErrorFlags |= ERRORS_DENOM_EQ_ZERO; return Root; } // Newton-Raphson formula for a non-linear system. Root.X = Xrold - ( (F1( Xrold, Yrold ) * F2dy( Xrold, Yrold )) - (F2( Xrold, Yrold ) * F1dy( Xrold, Yrold )) ) / Denom; Root.Y = Yrold - ( (F2( Xrold, Yrold ) * F1dx( Xrold, Yrold )) - (F1( Xrold, Yrold ) * F2dx( Xrold, Yrold )) ) / Denom; // Calculate the relative absolute error, Ea (of X term) // Don't calculate the error on the first iteration since there is no previous term if ( (Root.X != 0.0) && (Iter != 1) ) Ea = abs( (Root.X - Xrold) / Root.X ) * 100; // Root found within error range desired, or number of iterations has // reached the maximum amount permitted by Imax. Either way: return. if ( Ea < Es ) { return Root; } if ( Iter >= Imax ) { ErrorFlags |= ERRORS_MAX_ITERATION; return Root; } } // End while } //-------------------------------------------------------------------- // Name: ErrorMsg() // Desc: Function to print the currently set bits in ErrorFlags, of enum // type ERRORS. //-------------------------------------------------------------------- void ErrorMsg() { if ( ErrorFlags ) { printf( "Errors:\n" ); if ( ErrorFlags & ERRORS_DERIV_EQ_ZERO ) printf( "\tDerivative = 0\n" ); if ( ErrorFlags & ERRORS_DENOM_EQ_ZERO ) printf( "\tDenominator = 0\n" ); if ( ErrorFlags & ERRORS_MAX_ITERATION ) printf( "\tMax Iterations Reached\n" ); } // End if errors } //-------------------------------------------------------------------- // Name: _tmain() (Application Entry Point) // Desc: Entry point for the program. //-------------------------------------------------------------------- int _tmain() { Point Root; double fEs = Es( 6 ); // Number of desired significant figures. printf( "\nRoots Accurate to 6 Sig Figs\n\n" ); // NEWTON-RAPHSON----------------------------------------------- Root = NewtonRaphsonNonLinear( 1.5, // Initial X guess 3.5, // Initial Y guess fEs, // Percent tolerance 1000 // Maximum iterations allowed ); printf( "Method: Newton-Raphson (Non-Linear)\n" ); // Display any errors. ErrorMsg(); printf( "X_Root = %g\n", Root.X ); printf( "Y_Root = %g\n", Root.Y ); printf( "Iterations = %i\n", Iter ); return 0; } |