Derivative Calculator

I am interested in creating a derivative calculator. I puzzled over the solution to the problem, but I did not find the right solution. Maybe you have a hint how to start? Thanks

Sorry! I clearly want to make symbolic differentiation.

Say you have a function f (x) = x ^ 3 + 2x ^ 2 + x

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

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

+4
source share
6 answers

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

You need to analyze the mathematical expression and save the individual operations in the function in a tree structure.

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

Then you can recursively differentiate the tree by applying differentiation rules to each node. For example, a + node will become u' + v' , and a * node will become uv' + vu' .

+6
source

you need to remember your calculus. basically you need two things: a table of the derivatives of the main functions and the rules for how to generate compound expressions (for example, d(f + g)/dx = df/dx + dg/dx ). Then take the parser expression and recursively move to another tree. ( http://www.sosmath.com/tables/derivative/derivative.html )

+4
source

Split your string into S-expression (although this is usually accepted in the Lisp context, you can do the equivalent thing in most cases in any language), the easiest way is with lex / yacc or the equivalent, and then write a recursive "output" function. In the 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 the OCaml syntax - derive is a two-parameter recursive function with the first parameter the variable name and the second in consecutive lines, for example, if this parameter is a structure of the Add(x, y) form, return the Add structure constructed from two fields , with the values ​​of the derivatives of x and derivatives of y , and similarly for other cases that derive can receive as a parameter; _ in the first pattern means "match something")

After that, you may have some cleaning function to remove the resulting expression (reduction of fractions, etc.), but it becomes complicated and is not necessary for the output itself (i.e. what you get without it, is still the correct answer).

When your s-exp conversion is complete, convert the resulting s-exp to lowercase, again with a recursive function

+4
source

SLaks has already described the procedure of symbolic differentiation. I just wanted to add a few things:

  • Symbolic math is basically parsing and transforming trees. ANTLR is a great tool for both. I suggest starting with this great book Language Implementation Templates
  • There are open source programs that do what you want (like Maxima). Unpacking such a program can be interesting (but it may be easier to understand what happens if you first tried to write it yourself).
  • You might also want to simplify the exit. For example, simply applying the basic derived rules to the expression 2 * x will give 2 + 0*x . This can also be done by processing the tree (for example, by converting 0 * [...] to 0 and [...] + 0 to [...] etc.)
+2
source

For what kinds of operations do you want to calculate the derivative? If you enable trigonometric functions like sine, cosine and tangent, they are probably best stored in a table, while others like polynomials can be much easier to do. You allow functions to have multiple inputs, for example. f (x, y), not just f (x)?

Polynomials in a single variable will be my suggestion, and then consider adding trigonometric, logarithmic, exponential, and other advanced functions to calculate derivatives that can be harder to do.

+1
source

Symbolic differentiation by common functions (+, -, *, /, ^, sin, cos, etc.), ignoring areas where a function or its derivative undefined is simple. What is difficult, perhaps counterintuitively, simplifies the result afterwards.

To perform differentiation, store the operations in a tree (or even only in Polish notation) and create a table of derivatives of each of the elementary operations. Then we re-apply the chain rule and elementary derivatives together with setting the derivative of the constant to 0. This is quick and easy to implement.

+1
source

Source: https://habr.com/ru/post/1310861/


All Articles