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

Write a program that implements the bisection method for root finding. Your prog

ID: 3863316 • Letter: W

Question

Write a program that implements the bisection method for root finding. Your program should be similar to the Etter & Ingber Chapter 6_9 program on blackboard since it is a variation on the same technique. Your program should find the roots to some user defined tolerance.

Here is the code:

/*----------------------------------------------------*/
/* Program chapter6_9 */
/* */
/* This program estimates the real roots of a */
/* polynomial function using incremental search. */
#include<iostream> //Required for cin, cout
#include<cmath> //Required for cell()
using namespace std;
// Function Prototypes
void check_roots(double left, double right, double a0,
double a1, double a2, double a3);
double poly(double x, double a0, double a1,
double a2, double a3);
int main()
{
// Declare objects and function prototypes.
int n;
double a0, a1, a2, a3, a, b, step, left, right;
// Get user input.
cout << "Enter coefficients a0, a1, a2, a3: ";
cin >> a0 >> a1 >> a2 >> a3;
cout << "Enter interval limits a, b (a<b): ";
cin >> a >> b;
cout << "Enter step size: ";
cin >> step;
//Check subintervals for roots.
n = ceil((b - a)/step);
for (int k=0; k<=n-1; k++)
{
left = a + k*step;
if (k == n-1)
{
right = b;
}
else
{
right = left + step;
}
check_roots(left,right,a0,a1,a2,a3);
}
check_roots(b,b,a0,a1,a2,a3);
// Exit program.
return 0;
}/
*----------------------------------------------------*/
/* This function checks a subinterval for a root. */
void check_roots(double left, double right, double a0,
double a1, double a2, double a3)
{
// Declare objects and function prototypes.
double f_left, f_right;
// Evaluate subinterval endpoints and
// test for roots.
f_left = poly(left,a0,a1,a2,a3);
f_right = poly(right,a0,a1,a2,a3);
if (fabs(f_left) < 0.1e-04)
{
cout << "Root detected at " << left << endl;
}
else
{
if (fabs(f_right) < 0.1e-04)
;
else
{
if (f_left*f_right < 0)
{
cout << "Root detected at " << (left+right)/2) << endl;
}
}
}
// Exit function.
return;
}
/*----------------------------------------------------*/
/* This function evaluates a cubic polynomial. */
double poly(double x, double a0, double a1, double a2,
double a3)
{
return a0*x*x*x + a1*x*x + a2*x + a3;
}
/*----------------------------------------------------*/

Use your programs for the following physics problem:

Modern Physics: The black body radiation curve has a peak that can be determined by maximizing the Planck radiation formula in frequency form with respect to frequency. The Planck radiation formula is

If we take the derivative of the equation above and set it equal to zero and then substitute x = (hn/kT) we get an expression that reduces to

Solve for the roots of this equation to determine the constant for Wien’s law. Use the bisection method and the Newton Raphson method. You will need to replace the function in each program with equation 1 above.

From your value of x determine the constant in Wien’s law:

Add a few lines of code to each program to (a) calculate the value of the constant and (b) compare your results to the standard constant (i.e. take a % error) from a your textbook.

(e -1)

Explanation / Answer

Solution ::

//header files
#include<iostream.h>
#include<cmath.h>
#include<iomanip.h>
double f(double x); //declare the function for given equation
double f(double x) //define the function here, i.e. give the equation
{
double p=pow(x,3)-x-11.0; //write the equation whose roots are to be determined
return p;
}
int main()
{
cout.precision(4); //set precision
cout.setf(ios::fixed);
double p,q,r,e,fp,fq,fr; //declare the some needed variables
p:cout<<"Enter the initial guesses: p=";
cin>>p;
cout<<" q="; //Enter the value of b
cin>>q;
cout<<" Enter degree of the accuracy desired"<<endl; //Enter the accuracy
cin>>e; //e stands for accuracy
if (f(p)*f(q)>0) //Check if a root exists between p and q
{ //If f(p)*f(q)>0 then the root does not exist between p and q
cout<<"Enter a different intial guess"<<endl;
goto p; //go back to 'p' i.e, 17 and ask for different values of p and q
}
else //else a root exists between p and q
{
while (fabs(p-q)>=e) /*if the mod of p-q is greater than the accuracy desired keep bisecting the interval*/
{
r=(p+q)/2.0; //bisect the interval and find the value of c
fp=f(p);
fq=f(q);
fr=f(r);
cout<<"p="<<p<<" "<<"q="<<q<<" "<<"r="<<r<<" fr="<<fr<<endl; //print the values of a,b,c and fc after each iteration*/
if (fr==0) //if f(r)=0, that means we have to found the root of the equation
{
cout<<"Root of the equation is "<<r;
break;
}

if (fp*fr>0) //if f(p)xf(r)>0, //this means no root exist between p and r
{
p=r; //hence make p=r, ie make r the starting point of the interval and q the end point*/
}
else if (fp*fr<0)
{
q=r; // this means that a root exist between p and r therfore make r the end point of the interval
}
  
  
}
}   
cout<<"Root of the equation is "<<r; //print the root of the equation   
return 0;
}

/// *** Thank You *** ///

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