Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using the bisection algorthim code : To find a solution to f(x) = 0 given the co

ID: 3579174 • 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<< " ";
  
}

Population Growth The growth of a population can be modeled mathematically over short periods of time. This is done by modeling the rate of growth of a population R(t The rate of growth is defined by AN(t) R(t where N (t) is the number of people (population) at a given time t, and the symbol Arepresents the change. For example, if the population increases by AN(t) 50 people in At 1 day, the rate of growth is R(t) 50 people per day To model R(t), we assume that the population is growing due to births at a rate proportional to the population size N(t) (i.e., more people, more births). Mathematically R(t) AN (t) where is the birth rate constant that indicates the fraction of the population born each year. The model can also include the effect of population growth by immigration. We can assume that the immigration occurs at a constant rate v, (i.e., a fixed number of people immigrating each year). Adding the effect to the birth rate yields R(t) AN(t) v If this model is combined with the definition of rate, we obtain the following equation AN(t) AN(t) v This equation can be solved for N(t analytically, yielding the following result at At where No is the initial population at t 0, and e is the exponential function. Note that this model ignores the effects of death rates and emigration (although these effects can be easily added by reinterpreting the parameters v and A) Problem 2.1: A particular population initially (No) consists of one million individuals. During one year (t-1), v 453,000 individuals immigrate and N -1,564,000 individuals are present at the end of the year. Use the bisection method to determine the birth rate A of this population (correct to 4 decimal places). Note that X is between 0 and 1

Explanation / Answer

#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;cstdlib&gt;
#include &lt;cmath&gt;
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)) &lt; one.E-19 or (b - a ) /2 &lt; tol then
if(fabs(f(p)) &lt; one.E-19 || (b-a)/2. &lt; tol)
come back p; // (Procedure completed with success.)
  
// Step five Set i = i + one
++i;
  
// Step half-dozen If f(a)f(p) &gt; zero then set a = p (compute ai, bi )
// else set b = p
if(f(a)*f(p) &gt; 0)
a = p;
else
b = p;
}
  
// Step seven OUTPUT (“Method unsuccessful once N0 iteractions, N0 = ‘, N0 );
// (procedure completed unsuccessfully.)
// STOP
cout &lt;&lt; "Method unsuccessful once N0 iteractions, N0 = "&lt;&lt;n0&lt;&lt;" ";
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 &lt;&lt; setw(3) &lt;&lt; "i ";
cout &lt;&lt; setw(9) &lt;&lt; "a ";
cout &lt;&lt; setw(9) &lt;&lt; "b ";
cout &lt;&lt; setw(9) &lt;&lt; "p ";
cout &lt;&lt; setw(9) &lt;&lt; "fp ";
cout &lt;&lt; fastened &lt;&lt; showpoint &lt;&lt; setprecision(5);
return;
}
cout &lt;&lt; setw(3) &lt;&lt; i &lt;&lt; " ";
cout &lt;&lt; setw(9) &lt;&lt; a &lt;&lt; " ";
cout &lt;&lt; setw(9) &lt;&lt; b &lt;&lt; " ";
cout &lt;&lt; setw(9) &lt;&lt; p &lt;&lt; " ";
cout &lt;&lt; setw(9) &lt;&lt; fp&lt;&lt; " ";
  
}