Re: Error when working with a derivative
- To: mathgroup at smc.vnet.net
- Subject: [mg104964] Re: Error when working with a derivative
- From: Peter Breitfeld <phbrf at t-online.de>
- Date: Sun, 15 Nov 2009 05:55:55 -0500 (EST)
- References: <hdlk4k$hcb$1@smc.vnet.net>
You are right.
You used SetDelayed (:=) to define DerivMuVaF. SetDelayed doesn't
evaluate its arguments (hat Attribute HoldAll), so the call
DerivMuVaF[1,2] does the following:
1. The values 1 and 2 are plugged in the rhs, giving D[MuVaF[1,2],1]
2. Then MuVaF[1,2] will be evaluated to 10.
So you asked for D[10,1] which is nonsense.
The easiest way to avoid this is to use Set (=) instead of SetDelayed
DerivMuVaF[ex_ ,ey_] = D[MuVaF[ex,ey],ex]
Another way is to hide the variables using a Module:
DerivMuVaF[ex_,ey_]:=Module[{u,v},D[MuVaF[u,v],u]/.{u->ex,v->ey}]
Keep in mind, that Set usually gives the same results as SetDelayed, if
at the moment of definition no parameter has a value associated with it.
Look at the output of the following:
Clear[x]
f[x_] = x^2 + 1
f[2]
x = 13
f[x]
f[4]
g[x_] := x^2 + 1
g[2]
g[x]
g[4]
but (x now has a the OwnValue 13):
h[x_] = x^2 + 1
h[2]
h[x]
Vicent wrote:
> Hello.
>
> This is my first message to the list.
>
> I work with Mathematica 7.0.0 on Windows.
>
> I work with a multiple-variable function, say "MuVaF", and I want to
> define a function which is the partial derivative with respect to one
> of the variables. I tried with this:
>
> MuVaF[ex_, ey_] := ex^2 + ey^2 + ex*ey + ex + ey
>
> Then, if I try to derivate it with respect to "ex":
>
> D[MuVaF[ex, ey], ex]
>
> I get this:
>
> 1 + 2 ex + ey
>
> Which sound OK to me. But if I try this:
>
> DerivMuVaF[ex_, ey_] := D[MuVaF[ex, ey], ex]
>
> And then this (trying to evaluate the function for a given point):
>
> DerivMuVaF[1, 2]
>
> I get an error message:
>
> General::ivar: 1 is not a valid variable. >>
>
> I think that's because Mathematica is understanding I am trying to
> perform the derivative on "1"; it is expecting to get a variable and I
> am giving a number instead. So, what's the right way to tell
> Mathematica I want to work with the derivative function of a
> previously defined function??
>
> I guess the answer should be easy, but I haven't been able to find it
> out by my own. :-(
>
> Thank you in advance for your answers!
>
>
> --
> Vicent Giner-Bosch
>
--
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de