214k views
5 votes
Use the Bisection Method to find the root 2. Write a main program and a function program 3. Main Program a. define constants b. define es c. define function handle for the function you are finding the root for. Note that the function handle you define will be a function of S and t, i.e. f(S,t) d. define Sl and Su e. t will go from 0 days to 40 days. Use 40 values for t f. use a for loop (inside main program) to loop through the values of t (one at a time) to generate a vector for S (one value at a time). Note that each value of t will give a different value for S g. use a convergence criteria corresponding to 6 significant figures h. generate plot of S vs t 4. Function Program an. algorithm for Bisection Method

1 Answer

4 votes

Answer:

// C++ program for implementation of Bisection Method for

// solving equations

#include<bits/stdc++.h>

using namespace std;

#define EPSILON 0.01

// An example function whose solution is determined using

// Bisection Method. The function is x^3 - x^2 + 2

double func(double x)

{

return x*x*x - x*x + 2;

}

// Prints root of func(x) with error of EPSILON

void bisection(double a, double b)

{

if (func(a) * func(b) >= 0)

{

cout << "You have not assumed right a and b\\";

return;

}

double c = a;

while ((b-a) >= EPSILON)

{

// Find middle point

c = (a+b)/2;

// Check if middle point is root

if (func(c) == 0.0)

break;

// Decide the side to repeat the steps

else if (func(c)*func(a) < 0)

b = c;

else

a = c;

}

cout << "The value of root is : " << c;

}

// Driver program to test above function

int main()

{

// Initial values assumed

double a =-200, b = 300;

bisection(a, b);

return 0;

}

User Luin
by
3.1k points