4

I'm interested in building a derivative calculator. I've racked my brains over solving the problem, but I haven't found a right solution at all. May you have a hint how to start? Thanks

I'm sorry! I clearly want to make symbolic differentiation.

Let's say you have the function f(x) = x^3 + 2x^2 + x

I want to display the derivative, in this case f'(x) = 3x^2 + 4x + 1

I'd like to implement it in objective-c for the iPhone.

burki
  • 2,946
  • 6
  • 37
  • 51
  • 2
    What exactly are you trying to do? Symbolic differentiation? – SLaks May 25 '10 at 14:23
  • Numerical differentiation is trivial. Symbolic is definitely trickier, but if you do not require the output to be simplified, it is quite readily doable. – Andreas Rejbrand May 25 '10 at 14:44
  • 1
    @Andreas Numerical differentiation is extremely difficult. At least it is if you mean computing an approximation to a derivative by sampling the function at nearby points. How close should the points be? How do you know you're measure 'real' values and are not simply measuring numerical error? – sigfpe May 25 '10 at 16:55
  • Is it your intention to ultimately evaluate the function you're differentiating? Or is your goal purely to generate a symbolic expression? Some people, who are unaware of alternative accurate differentiation techniques, say they want symbolic differentiation, when their ultimate goal is to use the symbolic derivative to compute numerical values. – sigfpe May 25 '10 at 16:58
  • @user207442: Yes, but in 99 % of all cases a very naïve approach works perfectly. – Andreas Rejbrand May 25 '10 at 17:13
  • @Andreas Rejbrand: That's fine as long as you don't hit one of the 1% of cases where it doesn't just work *without realizing that you did*, and then use the result to build a bridge / launch a satellite / publish a paper that will make or break your career / whatever else. – Stephen Canon May 25 '10 at 18:00
  • @Stephen Canon: Of course. I just didn't realise the OP was a rocket engineer. :) – Andreas Rejbrand May 25 '10 at 18:12
  • 2
    @Andreas: He doesn't need to be a rocket engineer for me to worry; he only needs to write some software that some rocket engineer someday decides to use without really understanding =) – Stephen Canon May 25 '10 at 18:15
  • Here's a simple [derivative calculator](http://calculius.com/derivative-calculator/) using the powerful Javascript for math, calculus and differential problems. – The Os Dec 14 '16 at 12:24
  • Refer to the [derivatives calculator](http://easycalculation.com/differentiation/derivative-calculator.php) of easycalculation and learn about the rules of specifying input function in differentiations – Adam Biju Dec 02 '11 at 11:27

6 Answers6

8

I assume that you're trying to find the exact derivative of a function. (Symbolic differentiation)

You need to parse the mathematical expression and store the individual operations in the function in a tree structure.

For example, x + sin²(x) would be stored as a + operation, applied to the expression x and a ^ (exponentiation) operation of sin(x) and 2.

You can then recursively differentiate the tree by applying the rules of differentiation to each node. For example, a + node would become the u' + v', and a * node would become uv' + vu'.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
4

you need to remember your calculus. basically you need two things: table of derivatives of basic functions and rules of how to derivate compound expressions (like d(f + g)/dx = df/dx + dg/dx). Then take expressions parser and recursively go other the tree. (http://www.sosmath.com/tables/derivative/derivative.html)

Andrey
  • 59,039
  • 12
  • 119
  • 163
4

Parse your string into an S-expression (even though this is usually taken in Lisp context, you can do an equivalent thing in pretty much any language), easiest with lex/yacc or equivalent, then write a recursive "derive" function. In OCaml-ish dialect, something like this:

let rec derive var = function
    | Const(_) -> Const(0)
    | Var(x) -> if x = var then Const(1) else Deriv(Var(x), Var(var))
    | Add(x, y) -> Add(derive var x, derive var y)
    | Mul(a, b) -> Add(Mul(a, derive var b), Mul(derive var a, b))
    ...

(If you don't know OCaml syntax - derive is two-parameter recursive function, with first parameter the variable name, and the second being mathched in successive lines; for example, if this parameter is a structure of form Add(x, y), return the structure Add built from two fields, with values of derived x and derived y; and similarly for other cases of what derive might receive as a parameter; _ in the first pattern means "match anything")

After this you might have some clean-up function to tidy up the resultant expression (reducing fractions etc.) but this gets complicated, and is not necessary for derivation itself (i.e. what you get without it is still a correct answer).

When your transformation of the s-exp is done, reconvert the resultant s-exp into string form, again with a recursive function

Amadan
  • 191,408
  • 23
  • 240
  • 301
2

SLaks already described the procedure for symbolic differentiation. I'd just like to add a few things:

  • Symbolic math is mostly parsing and tree transformations. ANTLR is a great tool for both. I'd suggest starting with this great book Language implementation patterns
  • There are open-source programs that do what you want (e.g. Maxima). Dissecting such a program might be interesting, too (but it's probably easier to understand what's going on if you tried to write it yourself, first)
  • Probably, you also want some kind of simplification for the output. For example, just applying the basic derivative rules to the expression 2 * x would yield 2 + 0*x. This can also be done by tree processing (e.g. by transforming 0 * [...] to 0 and [...] + 0 to [...] and so on)
Niki
  • 15,662
  • 5
  • 48
  • 74
1

For what kinds of operations are you wanting to compute a derivative? If you allow trigonometric functions like sine, cosine and tangent, these are probably best stored in a table while others like polynomials may be much easier to do. Are you allowing for functions to have multiple inputs,e.g. f(x,y) rather than just f(x)?

Polynomials in a single variable would be my suggestion and then consider adding in trigonometric, logarithmic, exponential and other advanced functions to compute derivatives which may be harder to do.

JB King
  • 11,860
  • 4
  • 38
  • 49
1

Symbolic differentiation over common functions (+, -, *, /, ^, sin, cos, etc.) ignoring regions where the function or its derivative is undefined is easy. What's difficult, perhaps counterintuitively, is simplifying the result afterward.

To do the differentiation, store the operations in a tree (or even just in Polish notation) and make a table of the derivative of each of the elementary operations. Then repeatedly apply the chain rule and the elementary derivatives, together with setting the derivative of a constant to 0. This is fast and easy to implement.

Charles
  • 11,269
  • 13
  • 67
  • 105