//========================================================================= #define PROGRAMMER "SOLUTIONS, NoFrills" #define PROG_CODE "soln" #define COURSE "ECE-1021" #define YEAR (2003) #define TERM "Fall" #define SECTION (0) #define ASSIGNMENT "HW #1B" #define REVISION (0) #define TITLE "Zeros of Functions" #define SUBTITLE "by Newton's Method" #define EMAIL "wbahn@eas.uccs.edu" #define FILENAME "01b0soln.c" //========================================================================= // PROBLEM: // // Find the zeroes of a function using Newton's Method. // // PSEUDOCODE: // // 1) TASK: Get data from the user // 1.1) GET: x_guess (initial guess) // 1.2) GET: n_max (maximum number of iterations to perform) // 1.3) GET: h (width of derivative secant) // 1.4) GET: tol (tolerance of function zero) // 2) TASK: Compute Estimate of Function Zero // 2.1) SET: n = 0 // 2.2) SET: x = x_guess // 2.3) SET: f_x = FUNCTION // 2.4) WHILE: ( (abs(f_x) > tol) AND (n < n_max) ) // 2.4.1) SET: x = x_guess - (h/2.0); // 2.4.2) SET: f_xlower = FUNCTION; // 2.4.3) SET: x = x_guess + (h/2.0); // 2.4.4) SET: f_xupper = FUNCTION; // 2.4.5) SET: x_guess = x_guess - [ (h*f_x) / (f_xupper - f_xlower) ] // 2.4.6) SET: x = x_guess; // 2.4.7) SET: f_x = FUNCTION; // 2.4.8) SET: n = n+1 // 3) TASK: Report Results // 3.1) PUT: "Iterations Performed: " n // 3.2) PUT: "Last estimate of function zero = " x_guess // 3.3) PUT: "Function evaluated at nominal zero = " f_x //== INCLUDE FILES ======================================================== #include // printf(), scanf() #include // fabs() //== #DEFINES ============================================================= #define FUNCTION ((x)*(x)*(x) - 3.0) //== FUNCTION PROTOTYPES ================================================== void PrintHeader(void); //== MAIN FUNCTION ======================================================== int main(void) { int n, n_max; double x_guess, h, tol; double x, f_x, f_xupper, f_xlower; PrintHeader(); printf("\n"); // Print a blank line // 1) TASK: Get data from the user printf("Enter initial guess: "); scanf("%lf", &x_guess); printf("Enter maximum number of iterations: "); scanf("%i", &n_max); printf("Enter width of the derivative secant: "); scanf("%lf", &h); printf("Enter tolerance for function zero: "); scanf("%lf", &tol); // 2) TASK: Compute Estimate of Function Zero n = 0; x = x_guess; f_x = FUNCTION; while ( (fabs(f_x) > tol) && (n < n_max) ) { x = x_guess - (h/2.0); f_xlower = FUNCTION; x = x_guess + (h/2.0); f_xupper = FUNCTION; x_guess = x_guess - ( (h*f_x) / (f_xupper - f_xlower) ); x = x_guess; f_x = FUNCTION; n = n + 1; } // 3) TASK: Report Results printf("Iterations Performed: %i\n", n); printf("Last estimate of function zero = %f\n", x_guess); printf("Function evaluated at last nominal zero = %f\n", f_x); return(0); } //== SUPPORT FUNCTIONS ==================================================== void PrintHeader(void) { printf("========================================" "=======================================\n"); printf("Course....... %s-%i (%s %i)\n", COURSE, SECTION, TERM, YEAR); printf("Programmer... %s (%s)\n", PROGRAMMER, PROG_CODE); printf("Assignment... %s (Rev %i) (Source Code in %s)\n", ASSIGNMENT, REVISION, FILENAME); printf("Description.. %s\n", TITLE); printf(" %s\n", SUBTITLE); printf("========================================" "=======================================\n"); return; }