Given
class Function
{
public:
virtual double compute(double value) = 0;
virtual double differentiate(double value) = 0;
virtual double integrate(double value) = 0;
};
class Sine : public Function
{
public:
double compute(double value); // compute sin(x) for a given x
double differentiate(double value); // compute derivative sin'(x) for a given x
double integrate(double value); // integrate sin(x) for a given x
};
class Tangent : public Function
{
public:
double compute(double value); // compute tan(x) for a given x
double differentiate(double value); // compute derivative tan'(x) for a given x
double integrate(double value); // integrate tan(x) for a given x
};
The classes represent mathematical functions, where each function f(x) (e.g. sine, tangent) can be computed, differentiated and integrated for a given x.
Writea method named quotientRule to compute the derivative of a quotient of any two functions. Using the notation f'(x) to indicate the derivative of function f(x), the derivative ofthe quotient f(x)/g(x) is defined as [f'(x)*g(x) + f(x) * g'(x)]/[g(x)*g(x)], where f(x) and g(x) are any two given functions, and f'(x) and g'(x) are function derivatives, respectiveley. Your method should be applicable to any two functions derived from the the Function interface. An example of the method call is given below:
Sine sin; // the sine function
Tangent tan; // the tangent function
double answer = sin.quotientRule(tan, 5.3); // compute the derivative of sin(5.3)/tan(5.3)
I've written a method quotientRule within the program for better understanding, please find explanation at the bottom.
#include<iostream>
#include<cmath>
using namespace std;
class Function
{
public:
virtual double compute(double value) = 0;
virtual double differentiate(double value) = 0;
virtual double integrate(double value) = 0;
/* Here's the method which takes two parameter, a Function object reference and value */
double quotientRule(Function &t,double value)
{
return (differentiate(value)*t.compute(value)+compute(value)*t.differentiate(value))/(t.compute(value)*t.compute(value));
}
};
class Sine : public Function
{
public:
double compute(double value)// compute sin(x) for a given x
{ return sin(value);
}
double differentiate(double value)// compute derivative sin'(x) for a given x
{
return cos(value);
}
double integrate(double value) // integrate sin(x) for a given x
{
return -cos(value);
}
};
class Tangent : public Function
{
public:
double compute(double value)
{
return sin(value)/cos(value);
}
double differentiate(double value)
{
return (1/pow(cos(value),2));
}
double integrate(double value)// integrate tan(x) for a given x
{ return -log(abs(cos(value)));
}
};
int main()
{
Tangent tan;
Sine sin;
double answer=sin.quotientRule(tan,5.3);
cout<<answer;
}
Output:
Explanantion:
In the above program we implement quotientRule method inside definition of Functionclass which uses quotient rule for finding differentiation of the two functions by the formula : [f'(x)*g(x) + f(x) * g'(x)]/[g(x)*g(x)]
In abstract class one can not create object of the abstract class hence we use reference to the class Function.
When we assign a child class object to the parent class reference it calls the function with that particular child class,
This is the reason why when we use sin.quotientRule(tan,5.3) it calls the method using Sine object and uses refrence to Tangent class as parameter.
Get Answers For Free
Most questions answered within 1 hours.