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

C++ problem please use the follwoing cpp file to solve this problem: To find a s

ID: 3579837 • Letter: C

Question

C++ problem

please use the follwoing cpp file to solve this problem:


    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<< " ";
   
}

thank you

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 RO). The rate of growth is defined by AN where N is the number of people (population) at a given time t, and the symbol A represents 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 where A 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., afixed 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 AN(t) v This equation can be solved for N(t) analytically, yielding the following result Noe 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 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(t)-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 A is between 0 and 1.

Explanation / Answer

Small modification done to your code in calculating f(a) and f(b). The equation or expression written in f(a) is wrong and giving incorrect results.

#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)
{
int sign = 1;
if(x < 0){
sign = -1;
}
return (sign * 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<< " ";

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote