QuestionQuestion

Transcribed TextTranscribed Text

Q2. Building a class hierarchy In this question, we revisit our root-finding methods: bisection & secant (we'll ignore Newton-Raphson) 1. Declare an abstract class SimpleFunction with a pure virtual method called: virtual double f (double x) = 0; Derived classes should return a value for a specific function f for the given value of x. 2. Declare a derived class of SimpleFunction called F1 that implements f as follows: f(x)=Ax3+Bx2+Cx+D Make sure a constructor for F1 accepts values for A, B, C and D as parameters 3. Declare a derived class of SimpleFunction called F2 that implements f as follows: Make sure a constructor for F2 accepts values for A, B, C, K, L and M as parameters 4. Declare an abstract class RootFinder with a pure virtual method called: virtual double findRoot (SimpleFunction* F, double x0) = 0; Derived classes of RootFinder should use x0 as a starting point for finding a root of F. findRoot should return a root of F. A root x for f is defined as the absolute value of f(x) being less than 1e- 10. 5. Declare a derived class of RootFinder called RootFinderBisection that implements the bisection method. Note: in the past, we've always started with 2 values for the bisection method. One where f(x) is less than zero and one where f(x) is greater than zero. We expect the findRoot() method of RootFinderBisection to start at x0 and then search for another starting point. This search should take place within the findRoot() method of RootFinderBisection. This search should work with any function F passed in to findRoot() (not just F1 and F2 from the assignment) as long as the function has both negative and positive values (that is, as long as it has a root). You can assume any F passed in to findRoot() will be continuous. 6. Declare a derived class of RootFinder called RootFinderSecant that implements the secant method. Note: Make sure to stop iteration within the secant method if the next iteration will result in a division by zero. double A = 1; double B . -6; double C = 11; double D - -6; F1 f(A, B, c, D); RootFinderBisection bisection; RootFinderSecant secant; ofstream myfile; myfile.open ("F1Roots.csv"); myfile << "x,bisection,secant,f(bisection),f(secant) << endl; for (double x { double r_bisection bisection.findRoot(&f, x); double r_secant secant.findRoot(&f, x); myfile f.f(r_bisection) "<","<} myfile.close(); } { double A . 3; double B = 2; double C = 1; double K = -5.25; double L . 7.25; double M = -1.5; F2 f(A, B, c, K, L, M); RootFinderBisection bisection; RootFinderSecant secant; ofstream myfile; myfile.open ("F2Roots.csv"); myfile << "x,bisection,secant,f(bisection),f(secant)" << endl; for (double x x <= 2.0; X+= 0.005) { double r_bisection bisection.findRoot(&f, x); double r_secant secant.findRoot(&f, x); myfile <<"," << f.f(r_secant) << endl; } myfile.close(); } return 0;

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

//
// You will need to implement RootFinder, RootFinderBisection, RootFinderSecant, SimpleFunction, F1 and F2
// as classes.
//
//
// Your code should work with the main() function below. DO NOT CHANGE THE CODE IN THE MAIN FUNCTION.
//
//

const double Epsilon = 1e-10;

bool isZero(double x) {
    return fabs(x) < Epsilon;
}

class SimpleFunction {
public:

    SimpleFunction() {
    };

    SimpleFunction(const SimpleFunction& orig) {
    };

    virtual ~SimpleFunction() {
    };
    // pure virtual function
    virtual double f(double x) = 0;
private:
};

class F1 : public SimpleFunction {
public:

    F1(double A, double B, double C, double D) {
       this->A = A;
       this->B = B;
       this->C = C;
       this->D = D;
    };

    F1(const F1& o) {
       this->A = o.A;
       this->B = o.B;
       this->C = o.C;
       this->D = o.D;
    };

    virtual ~F1() {
    };

    double f(double x) {
       double x2 = x * x;
       double x3 = x2 * x;

       return A * x3 + B * x2 + C * x + D;
    };
private:
    double A, B, C, D;
};

class F2 : public SimpleFunction {
public:

    F2(double A, double B, double C, double K, double L, double M) {
       this->A = A;
       this->B = B;
       this->C = C;
       this->K = K;
       this->L = L;
       this->M = M;
    };...

By purchasing this solution you'll be able to access the following files:
Solution.cpp.

$40.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats