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

PLEASE USE C++ TO DO THE CODE!!! For this question, it is fine to use the code p

ID: 3790427 • Letter: P

Question

PLEASE USE C++ TO DO THE CODE!!!

For this question, it is fine to use the code provided in class as your starting point. You will need make changes to the functions. For the following exercise, we are going to use the polynomial: f (at) = (x + 1)0 - 4)0 - 10) = x^3 - 13x^2 + 26x + 40 f'(0) = 3x^2 - 26x + 26 Clearly, the function has 3 roots: -1, 4 and 10. Also, for x 0 Implement a function double f (double x) that returns the polynomial value and a function double f Prime (x) that returns the derivative of the polynomial. Implement double find Root Newton (double x, int N) that implements the Newton Raphson root finding method for starting point x and iterating N times. That is, we're not trying to find a root to a specified precision. Rather, we want to 'loop' N times and return the estimated root. Implement double find Root Secant (double x, int N) that implements the secant root finding method for starting point x and iterating N times. For your second starting point, use x + 0.25. your code should check whether f(x prev) - f(x) is essentially zero. For our purposes, you can use the fabs() function for the difference and check whether it is less than le-10. look at the lecture notes;-) Implement double find Root Bisection (double x, int N)that implements the bisection root finding method for starting point x and iterating N times. For your second starting point, use -2 if f(x) > 0 and 11 if f(x)

Explanation / Answer

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

double f(double x);    //declare the function for the given equation

double f(double x)    //define the function here, ie give the equation

{

    double a=pow(x,3)-26x+26.0;    //write the equation whose roots are to be determined

    return a;

}

int main()

{   

    cout.precision(4);        //set the precision

    cout.setf(ios::fixed);

    double a,b,c,e,fa,fb,fc;    //declare some needed variables

    a:cout<<"Enter the initial guesses: a=";    //Enter the value of a(set a label('a:') for later use with goto)

    cin>>a;

    cout<<" b=";            //Enter the value of b

    cin>>b;

    cout<<" Enter the degree of accuracy desired"<<endl;    //Enter the accuracy

    cin>>e;                //e stands for accuracy

    if (f(a)*f(b)>0)        //Check if a root exists between a and b

    {                //If f(a)*f(b)>0 then the root does not exist between a and b

        cout<<"Please enter a different intial guess"<<endl;

        goto a;            //go back to 'a' ie 17 and ask for different values of a and b

    }

    else                //else a root exists between a and b

    {

    while (fabs(a-b)>=e)        /*if the mod of a-b is greater than the accuracy desired keep                         bisecting the interval*/

    {

        c=(a+b)/2.0;        //bisect the interval and find the value of c

        fa=f(a);       

        fb=f(b);

        fc=f(c);

        cout<<"a="<<a<<"     "<<"b="<<b<<"     "<<"c="<<c<<"      fc="<<fc<<endl;/*print the                             values of a,b,c and fc after each iteration*/       

        if (fc==0)        //if f(c)=0, that means we have found the root of the equation

        {

            cout<<"The root of the equation is "<<c;    /*print the root of the equation                                         and break out of the loop*/

            break;

        }

        if (fa*fc>0)    //if f(a)xf(c)>0, that means no root exist between a and c

        {

            a=c;    /*hence make a=c, ie make c the starting point of the interval and b the                     end point*/

        }

        else if (fa*fc<0)

        {   

            b=c;    /*this means that a root exist between a and c therfore make c the end                     point of the interval*/

        }   

       

   

    }

    }            //The loop ends when the difference between a and b becomes less than the desired accuracy ie now the value stored in 'c' can be called the approximate root of the equation        

    cout<<"The root of the equation is "<<c;    //print the root   

    return 0;   

}

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