If you don't want a numerical derivative, then you need to have a formula that can be differentiated. Unfortunately C++ doesn't really have a data type that can represent formula's, or manipulate them. Even then, writing a symbolic derivative program is often a common exercise given in CS courses to teach recursion. You simply have to program in every differentiation rule from a calculus book: addition, subtraction, multiplication, division, power, chain rule, trig functions, etc. You can either make some kind of a tree data type to contain formulas, or simply use strings, with a lot of parsing.
Languages like Maple, Mathematica and others have built-in symbolic derivatives.
Languages that can manipulate strings easily, eg. perl, are relatively easy to write such programs in.
On the other hand, there is a brilliant idea (search for "automatic differentiation" or "algorithmic differentiation"), where one can differentiate a subroutine (yes!) by using overloading, and special data types. Let me explain so that this doesn't sound like mumbo-jumbo:
For any variables which used to be defined as "float x,y,z", instead define them as a new data type eg: "varfloat x,y,z". The varfloat data type keeps track of two numbers x.value and x.derivative. When you say z=x*y, the multiplication operator is overloaded to compute the value and the derivative, so that what really happens is:
Code: Select all
z.value = x.value + y.value
z.derivative = x.value * y.derivative + y.value *x.derivative
which is really an implementation of the multiplication rule. Similar overloading of all operators makes it possible to compute an exact derivative simply by changing the data types of your variables.