MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Error when working with a derivative

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104969] Re: [mg104947] Error when working with a derivative
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Sun, 15 Nov 2009 05:56:57 -0500 (EST)
  • References: <200911140657.BAA18027@smc.vnet.net>

Hi Vicent,

The problem you are having is due to the improper use of parameter-passing
semantics of Mathematica.

The D operation needs all your differentiation variables to be symbols
(well, normal expressions are also ok as long as their symbolic head is not
a built-in symbol, I guess) . On the other hand, the way parameters are
passed to a function in Mathematica is that their values are textually
substituted in the body, before  the body is evaluated.  By the time D looks
at it, it finds a number where it expects a variable (ex as a second
argument), an complains.

A couple of ways out:

1. Use Set instead of SetDelayed:

Clear[derivMuVaF1,ex,ey];
derivMuVaF1[ex_, ey_] = D[MuVaF[ex, ey], ex];

Then your derivMuVaF1 gets a definition where D has already evaluated

2. Use a local variable to perform differentiation, and then a replacement
rule:

Clear[derivMuVaF2];
derivMuVaF1[ex_, ey_] :=
  Module[{exl}, D[MuVaF[exl, ey], exl] /. exl -> ex];

In[1]: = {derivMuVaF1[1, 2],derivMuVaF2[1, 2]}

Out[1] = {5,5}

By the way, try to avoid starting your function names with a capital letter,
to prevent conflicts with built-in symbols.

Regards,
Leonid






On Fri, Nov 13, 2009 at 10:57 PM, Vicent <vginer at gmail.com> 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
>
>



  • Prev by Date: Re: Error when working with a derivative
  • Next by Date: Re: Fyi, Listing of Mathematica 7 functions (1869 functions)
  • Previous by thread: Re: Error when working with a derivative
  • Next by thread: Re: Error when working with a derivative