2
$\begingroup$

I want to create my own differentiation function Derivada. So, I already set some properties like:

Derivada[x_^(n_: 1), x_Symbol] /; FreeQ[n, x] := n*x^(n - 1)
Derivada[n_*x_, x_Symbol] /; FreeQ[n, x] := n
Derivada[Log[a_: E, x_], x_Symbol] := 1/(x*Log[a])
Derivada[Log[x_], x_Symbol] := 1/x
Derivada[f_, x_Symbol] /; FreeQ[f, x] := 0
Derivada[(a_?NumericQ) f_, x_Symbol] := a*Derivada[f, x]
Derivada[Exp[x_], x_Symbol] := Exp[x]
Derivada[a_^x_, x_Symbol] := a^x Log[a]
Derivada[u_Plus, x_Symbol] := Derivada[#, x] & /@ u
Derivada[u_*v_, x_Symbol] := u Derivada[v, x] + v Derivada[u, x]
Derivada[u_/v_, x_Symbol] := (Derivada[u, x]*v - u*Derivada[v, x])/v^2

I have a problem with rational functions.

The functions works for:

In[1224]:= Derivada[x/(x + 1), x]

Out[1224]= 1/(1 + x)^2

But the function doesn't works for:

In[1225]:= Derivada[1/(x + 1), x]

Out[1225]= Derivada[1/(1 + x), x]

What can be wrong?

$\endgroup$
  • $\begingroup$ just add the rule Derivada[1 /v_, x_Symbol] := -Derivada[v, x]/v^2? $\endgroup$ – kglr Aug 30 '18 at 23:05
  • 1
    $\begingroup$ Hi, I edited your question to improve the English a bit. Please have a look, and check that you're happy with the edit. Otherwise, you can always choose to rollback. $\endgroup$ – QuantumDot Aug 31 '18 at 15:16
  • $\begingroup$ Note that the derivative of x/(x+1) is -x/(1+x)^2 + 1/(1+x), NOT 1/(1+x)^2 as in your result! $\endgroup$ – evanb Aug 31 '18 at 15:39
0
$\begingroup$

The problem: Mathematica manages to change the form of the underlying expression, and the expression no longer matches the pattern.

u_/v_ // FullForm
Times[Pattern[u,Blank[]],Power[Pattern[v,Blank[]],-1]]
1/(x + 1) // FullForm
Power[Plus[1,x],-1]

It also can not match the rule Derivada[x_^(n_), x_Symbol] as in the case of Power[Plus[1,x],-1] the first argument is not the same x as the second argument in your rule (for a good reason).

The solution: You could keep on implementing a rule for every special case or you could implement a chain rule. Simplification of symbolic expressions is a field of research and sooner or later you will start publishing your methods in journals :)

EDIT

Just because it is a fun excuse to take the pattern matching to the limits, here is an implementation of 2 level chain rule:

Derivada[f:fh_[farg1___,g:gh_[garg1___,x_,garg2___],farg2___],x_]/;And[
Head[Derivada[g,x]]=!=Derivada,
Head[Block[{y},Derivada[f/.g->y,y]]]=!=Derivada
]:=Derivada[g,x]Block[{y},Derivada[f/.g->y,y]/.y->g]

Derivada[1/(x + 1), x]

enter image description here

Next step would be to make it recursive. It should check, if any of the arguments is also a function of x, and if it is possible to take a derivative of them. It is a deep rabbit hole.

$\endgroup$
  • $\begingroup$ Some suggestion to implement the chain rule? $\endgroup$ – Mateus Aug 30 '18 at 23:07
  • 1
    $\begingroup$ Please see the updated answer. $\endgroup$ – Johu Aug 31 '18 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.