2
$\begingroup$

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

Derivada[x_^n_, x_Symbol] := n*x^(n - 1)
Derivada[n_*x_, x_Symbol] := n
Derivada[Log[x_], x_Symbol] := 1/x
Derivada[_?NumericQ, x_Symbol] := 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]

How can I make a derivative of a polynomial fuction like:

Derivada[x^2 + 3 x, x]

Or:

Derivada[x^3 + x^2 + 3 x, x]

Another question:

How can I set to zero if I want to derive f[x] with respect to y for example.

And about the Chain Rule? How can I set this? For exemplo: Ho to derive

Exp[3x] 

Or

Sqrt[3x+1]
$\endgroup$

2 Answers 2

2
$\begingroup$

Here are some modified definitions you might find worth understanding

Derivada[x_^n_., x_Symbol] /; FreeQ[n, x] := n*x^(n - 1)
Derivada[f_, x_Symbol] /; FreeQ[f, x] := 0
Derivada[u_Plus, x_Symbol] := Derivada[#, x] & /@ u
Derivada[u_ *v_, x_Symbol] := u Derivada[v, x] + v Derivada[u, x]

Derivada[x^3 + x^2 + 3 x, x]
(* 3 + 2 x + 3 x^2 *)

Derivada[x^2 + 3 x, y]
(* 0 *)
$\endgroup$
3
  • $\begingroup$ Why there is a dot . after n_ at Derivada[x_^n_., x_Symbol] /; FreeQ[n, x] := n*x^(n - 1)? $\endgroup$
    – Mateus
    Aug 30, 2018 at 19:24
  • $\begingroup$ See the help for Optional. It matches x as well as x^n, reducing the number of definitions needed. $\endgroup$
    – mikado
    Aug 30, 2018 at 19:29
  • $\begingroup$ And about the Chain Rule? How can I set this? For exemplo: Ho to derive Exp[3x] or Sqrt[3x+1] $\endgroup$
    – Mateus
    Aug 30, 2018 at 20:57
0
$\begingroup$

You can approach this is the same kind of way you are trying above:

Derivada[z__ + y__, x_] := Derivada[z, x] + Derivada[y, x];
Derivada[x^2 + 3 x, x]
3 + 2 x

Note the double underscore, which indicates that the pattern can match a sequence, rather than a single element.

$\endgroup$
1
  • $\begingroup$ Plus is Flat, so you could just use single underscores instead. $\endgroup$
    – Carl Woll
    Aug 30, 2018 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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