Question: Combine programs 14.1, 14.2 and 14.3 to compare the bisection, modified regulsa falsi, and secant methods to find a root of the function f(x) =

Combine programs 14.1, 14.2 and 14.3 to compare the bisection, modified regulsa falsi, and secant methods to find a root of the function f(x) = x2 2x - 3. Use the same a, b, convergence criteria, and maximum number of iterations and compare the number of iterations. Make a table for comparison. 14.1: #include #include using namespace std; void bisection(double, double, double, int); // function prototype double f(double); // function prototype using namespace std; int main() { int imax; // maximum number of iterations double a, b; // left and right ends of the original interval double epsilon; // convergence criterion// Obtain the input data cout << "Enter the limits of the original search interval, a and b: "; cin >> a >> b; cout << "Enter the convergence criteria: "; cin >> epsilon; cout << "Enter the maximum number of iterations allowed: "; cin >> imax; bisection(a, b, epsilon, imax); return 0; } // A bisection function that finds roots of a function // The interval a < x < b is known to contain a root of f(x). The // estimate of the root is improved successively by finding in // which half of the interval the root lies and then replacing // the original interval by that half-interval. void bisection(double a, double b, double epsilon, int imax) { int i; // current iteration counter double x1, x2, x3; // left, right, and midpoint of current interval double f1, f2, f3; // function evaluated at these points double width; // width of original interval = (b - a) double curwidth; // width of current interval = (x3 - x1) // Echo back the passed input data cout << " The original search interval is from " << a << " to " << b << endl; cout << "The convergence criterion is: interval < " << epsilon << endl; cout << "The maximum number of iterations allowed is " << imax << endl; // Calculate the root x1 = a; x3 = b; f1 = f(x1); f3 = f(x3); width = (b - a); // Verify there is a root in the interval if (f1 * f3 > 0.0) cout << " No root in the original interval exists" << endl;else { for (i = 1; i <= imax; i++) { // Find which half of the interval contains the root x2 = (x1 + x3) / 2.0; f2 = f(x2); if (f1 * f2 <= 0.0) // root is in left half-interval { curwidth = (x2 - x1) / 2.0; f3 = f2; x3 = x2; } else // root is in right half-interval { curwidth = (x3 - x2) / 2.0; f1 = f2; x1 = x2; } if (curwidth < epsilon) { cout << " A root at x = " << x2 << " was found " << "in " << i << " iterations" << endl; cout << "The value of the function is " << f2 << endl; return; } } } cout << " After " << imax << " iterations, no root was found " << "within the convergence criterion" << endl; return; } // Function to evaluate f(x) double f(double x) { const double PI = 2*asin(1.0); // value of pi return (exp(-x) - sin(0.5 * PI * x)); } 14.2: #include #include using namespace std; void modregfalsi(double, double, double, int); // function prototype double f(double); // function prototype int main() { int imax; // maximum number of iterations double a, b; // left and right ends of the original interval double epsilon; // convergence criterion// Obtain the input data cout << "Enter the limits of the original search interval, a and b: "; cin >> a >> b; cout << "Enter the convergence criterion: "; cin >> epsilon; cout << "Enter the maximum number of iterations allowed: "; cin >> imax; modregfalsi(a, b, epsilon, imax); return 0; } // A modified regula falsi function that finds roots of a function. // The maximum number of iterations permitted is imax. The convergence // criterion is that the fractional size of the search interval // (x3 - x1) / (b - a) is less than epsilon. A relaxation factor, // RELAX, is used. void modregfalsi(double a, double b, double epsilon, int imax) { const double RELAX = 0.9; // the relaxation factor int i; // current iteration counter double x1, x2, x3; // left, right, and midpoint of current interval double f1, f2, f3; // function evaluated at these points double width; // width of original interval = (b - a) double curwidth; // width of current interval = (x3 - x1) // Echo back the passed input data cout << " The original search interval is from " << a << " to " << b << " The convergence criterion is: interval < " << epsilon << " The maximum number of iterations allowed is " << imax << endl; // Calculate the root x1 = a; x3 = b; f1 = f(x1); f3 = f(x3); width = abs(b - a); // Iterations for (i = 1; i <= imax; i++) { curwidth = (x3 - x1) / width; x2 = x1 - width * curwidth * f1 / (f3 - f1);f2 = f(x2); if (abs(curwidth) < epsilon) // root is found { cout << " A root at x = " << x2 << " was found " << "in " << i << " iterations" << endl; cout << "The value of the function is " << f2 << endl; return; } else // check for left and right crossing { if (f1 * f2 < 0.0) // check for crossing on the left { x3 = x2; f3 = f2; f1 = RELAX * f1; } else if (f2 * f3 < 0.0) // check for crossing on the right { x1 = x2; f1 = f2; f3 = RELAX * f3; } else // no crossing in the interval { cout << "The search for a root has failed due to no root in " << "the interval " << "In step " << i << " of the iteration the function " << "does not change sign" << endl; } } } cout << " After " << imax << " iterations, no root was found " << "within the convergence criterion " << "The search for a root has failed due to excessive iterations " << "after the maximum number of " << imax << " iterations" << endl; return; } // Function to evaluate f(x) double f(double x) { const double PI = 2*asin(1.0); // value of pi return (exp(-x) - sin(0.5 * PI * x)); } 14.3: #include #include using namespace std; void secant(double, double, double, int); // function prototype double f(double); // function prototype int main() { int imax; // maximum number of iterations double a, b; // left and right ends of the original interval double epsilon; // convergence criterion // Obtain the input data cout << "Enter the limits of the original search interval, a and b: "; cin >> a >> b; cout << "Enter the convergence criteria: "; cin >> epsilon; cout << "Enter the maximum number of iterations allowed: "; cin >> imax; secant(a, b, epsilon, imax); return 0; } // This function implements the secant method for finding a root // of a function void secant(double a, double b, double epsilon, int imax) { int i; // current iteration counter double x0, x1; // left and right x values of current interval double f0, f1; // function evaluated at these points double dx0; // delta x0 double dx1; // delta x1// Echo back the passed input data cout << " The original search interval is from " << a << " to " << b << " The convergence criterion is: interval < " << epsilon << " The maximum number of iterations allowed is " << imax << endl; // Determine the root x0 = a; f0 = f(x0); dx0 = abs(b - a); // Iterations for (i = 1; i <= imax; i++) { x1 = x0 + dx0; f1 = f(x1); if (abs(f1) < epsilon) // root is found { cout << " A root at x = " << x1 + dx1 << " was found " << "in " << i << " iterations" << endl; cout << "The value of the function is " << f1 << endl; return; } else // do next iteration { dx1 = ( f1/(f0 - f1) ) * dx0; x0 = x1; dx0 = dx1; f0 = f1; } } cout << " After " << imax << " iterations, no root was found " << "within the convergence criterion " << "The search for a root has failed due to excessive iterations " << "after the maximum number of " << imax << " iterations" << endl; return; } // Function to evaluate f(x) double f(double x) { const double PI = 2*asin(1.0); // value of pi return (exp(-x) - sin(0.5 * PI * x)); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!