1.1 – Derivative Calculator

Summary

This project will require you to create a program that will take a mathematical expression in a single variable (x) and either

  • evaluate the expression for a given value of x, or
  • calculate the derivative (with respect to x) of the expression

The purpose of this project is to demonstrate a working knowledge of

  • Class creation
  • Inheritance
  • Polymorphism
  • Linked Lists
  • Templates

 

Diagram

The diagram only shows the public methods necessary for objects to function. You may add any additional private/protected/public members you deem necessary since they will not be tested.

AbstractTerm and sub-classes

You will need the following sub-classes of AbstractTerm:

  • A ConstantTerm object will represent a term of the form

±a
 
where a is of type int. The derivative of a constant term is always
+0
 

  • A LinearTerm object will represent a term of the form

±ax
 
where a is of type int and x is the independent variable. The derivative of a linear term is a constant term of the form
±a
 

  • A PolynomialTerm object will represent a term of the form

±ax^b
 
where a is of type int, b is a positive int greater than one, and x is the independent variable. If b>2, the derivative of the polynomial term is a polynomial term of the form
±(ab)x^(b-1)
 
If b=2, the derivative of the polynomial term is a linear term of the form
±2ax
 

  • A TrigTerm object will represent a term of the form

±a cos(x)
 
or
±a sin(x)
 
where a is of type int and x is the independent variable. The derivative of the sine term is a trigonometric term of the form
±a cos(x)
 
The derivative of the cosine term is a sine term of the form
∓a sin(x)
 
Note that the sign flips when taking the derivative of cos(). Evaluation of trigonometric functions should be done in degrees.
Each subclass of AbstractTerm will need to override the following pure virtual functions:

  • derivative() – returns a new AbstractTerm that represents the derivative of the current term
  • evaluate(double) – returns the evaluation of the term with the double value substituted for x
  • toString() – returns a string representation of the term (see below for examples)

TrigType Enumeration

The TrigType enumeration should have two values used to distinguish between the trigonometric functions:

  • COSINE
  • SINE

ProjNode and ProjLinkedList

You will need to create a ProjNode class (or struct) that holds data and points to the next node. You will also need a ProjLinkedList class that contains a head pointer, and the necessary functions. Both classes will need to be templated to contain any type of data, and you will need to use them in your Expression class to store AbstractTerms. At minimum I expect the ProjLinkedList to contain:

  • a default constructor
  • add(T) – a function that adds to the beginning of the list
  • getAt(int) – a function that returns the value at the int-th node (zero-based)

Expression class

The Expression class will need to contain a ProjLinkedList of AbstractTerms. It will also need the following functions:

  • getDerivative() – returns a new Expression object containing the derivative of each term of the original object. Zero-valued constant terms should not be included.
  • getEvaluation(double) – returns the sum of the evaluations of the individual terms.
  • toString() – returns a string version of the expression. The polynomial terms should be displayed in descending exponential order, followed by linear, constant, sine, and cosine
  • An overloaded += operator that will add an AbstractTerm to the expression
  • A destructor that will delete all of the terms in the expression

main()

The main() function will not be tested. Use it for your own tests.

EXAMPLE

AbstractTerm* t1 = new LinearTerm(5);
AbstractTerm* t2 = new PolynomialTerm(-4,3);
AbstractTerm* t3 = new TrigTerm(-6, TrigType::COSINE);
cout << t1->toString() << endl; // + 5x
cout << t1->evaluate(5) << endl; // 25
cout << t2->toString() << endl; // – 4x^3
cout << t2->evaluate(2) << endl; // -32
cout << t3->toString() << endl; // – 6cos(x)
cout << t3->evaluate(45) << endl; // -4.24
Expression* e1 = new Expression();
(*e1) += t1;
(*e1) += t2;
(*e1) += t3;
Expression* e2 = e1->getDerivative();
cout << e1->toString() << endl; // – 4x^3 + 5x – 6cos(x)
cout << e2->toString() << endl; // – 12x^2 + 5 + 6sin(x)
cout << e1->getEvaluation(0) << endl; // -6
cout << e2->getEvaluation(0) << endl; // 5
delete e2;
delete e1;
 

C++ Create A Program That Will Take A Mathematical Expression In A Single Variable (X) And Either Evaluate The Expression For A Given Value Of X, Or Calculate The Derivative (With Respect To X) Of The Expression
We have updated our contact contact information. Text Us Or WhatsApp Us+1-(309) 295-6991