using the bisection algorthim code : To find a solution to f(x) = 0 given the co
ID: 3577174 • Letter: U
Question
using the bisection algorthim code :
To find a solution to f(x) = 0 given the continuous function f
on the interval [a,b] where f(a) and f(b) have opposite signs:
INPUT endpoints a,b; tolerance tol; maximum number of iteractions N0.
OUTPUT approximate solution p or message of failure.
*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
using namespace std;
double bisection(double, double, double, int);
int main()
{
// Input parameters and check for errors
double a,b; // endpoints
double tol;
int n0;
cout << "Enter endpoints a and b surrounding solution: ";
cin >> a >> b;
if(a>b){
cout << "Error: invalid endpoints ";
return 1;
}
cout << "Enter tolerance: ";
cin >> tol;
if(tol<=0){
cout << "Error: invalid tolerance ";
return 1;
}
cout << "Enter maximum number of iterations: ";
cin >> n0;
if(n0<=0){
cout << "Error: invalid number of iterations ";
return 1;
}
// Call Bisection function
double p = bisection(a, b, tol, n0);
// Output results
cout << "A zero was found at p = "<<p<<endl;
return 0;
}
double f(double x)
{
return (20.*pow(x,2.) - 7.*x - 40.);
}
//
// BISECTION ALGORITHM
//
//
void printpars(int=0, double=0, double=0, double=0, double=0); // optional
double bisection(double a, double b, double tol, int n0)
{
// Check input for logical error
if(f(a)*f(b) > 0){
cout << "Bisection Error: invalid endpoints. ";
cout << " function must have opposite signs at endpoints. ";
exit(1);
}
//Step 1 Set i = 1
int i = 1;
//Step 2 While i < N0 do Steps 3–6.
printpars(); // optional: print header
while(i < n0) {
// Step 3 Set p = a + (b - a)/2 . (Compute pi)
double p = a + (b-a)/2.;
// Step 3.opt OUTPUT i a b p f(p) (optional)
printpars(i, a, b, p, f(p)); // optional: parameters
// Step 4 If abs(f(p)) < 1.E-19 or (b - a ) /2 < tol then
if(fabs(f(p)) < 1.E-19 || (b-a)/2. < tol)
return p; // (Procedure completed successfully.)
// Step 5 Set i = i + 1
++i;
// Step 6 If f(a)f(p) > 0 then set a = p (compute ai, bi )
// else set b = p
if(f(a)*f(p) > 0)
a = p;
else
b = p;
}
// Step 7 OUTPUT (“Method failed after N0 iteractions, N0 = ‘, N0 );
// (procedure completed unsuccessfully.)
// STOP
cout << "Method failed after N0 iteractions, N0 = "<<n0<<" ";
exit(2);
}
// Extra: print parameters
void printpars(int i, double a, double b, double p, double fp)
{
static bool first = true;
if (first){ // print header only if first time
first = false;
cout << setw(3) << "i ";
cout << setw(9) << "a ";
cout << setw(9) << "b ";
cout << setw(9) << "p ";
cout << setw(9) << "fp ";
cout << fixed << showpoint << setprecision(5);
return;
}
cout << setw(3) << i << " ";
cout << setw(9) << a << " ";
cout << setw(9) << b << " ";
cout << setw(9) << p << " ";
cout << setw(9) << fp<< " ";
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
using namespace std;
double bisection(double, double, double, int);
int main()
//
// division algorithmic program
//
//
void printpars(int=0, double=0, double=0, double=0, double=0); // ex gratia
double bisection(double a, double b, double tol, int n0)
three Set p = a + (b - a)/2 . (Compute pi)
double p = a + (b-a)/2.;
// Step three.opt OUTPUT i a b p f(p) (optional)
printpars(i, a, b, p, f(p)); // optional: parameters
// Step four If abs(f(p)) < one.E-19 or (b - a ) /2 < tol then
if(fabs(f(p)) < one.E-19 || (b-a)/2. < tol)
come back p; // (Procedure completed with success.)
// Step five Set i = i + one
++i;
// Step half-dozen If f(a)f(p) > zero then set a = p (compute ai, bi )
// else set b = p
if(f(a)*f(p) > 0)
a = p;
else
b = p;
}
// Step seven OUTPUT (“Method unsuccessful once N0 iteractions, N0 = ‘, N0 );
// (procedure completed unsuccessfully.)
// STOP
cout << "Method unsuccessful once N0 iteractions, N0 = "<<n0<<" ";
exit(2);
}
// Extra: print parameters
void printpars(int i, double a, double b, double p, double fp)
initial = true;
if (first)providing 1st time
1st = false;
cout << setw(3) << "i ";
cout << setw(9) << "a ";
cout << setw(9) << "b ";
cout << setw(9) << "p ";
cout << setw(9) << "fp ";
cout << fastened << showpoint << setprecision(5);
return;
}
cout << setw(3) << i << " ";
cout << setw(9) << a << " ";
cout << setw(9) << b << " ";
cout << setw(9) << p << " ";
cout << setw(9) << fp<< " ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.