Apologies, I'm still new at Mathematica. I have defined the function

ψ[x_, y_] := 
   (C1 Cos[(k y)/Sqrt[Gy]] + C2 Sin[(k y)/Sqrt[Gy]]) 
     (C3 Cosh[(k x)/Sqrt[Gx]] +C4 Sinh[(k x)/Sqrt[Gx]])

When I type in ψ[-a, y] everything is fine. and I get the output I wanted:

(C1 Cos[(k y)/Sqrt[Gy]] + C2 Sin[(k y)/Sqrt[Gy]]) 
     (C3 Cosh[(a k)/Sqrt[Gx]] - C4 Sinh[(a k)/Sqrt[Gx]])

But when I type in

dx[x_] := D[ψ[x, y], x]

I get the error message

General::ivar: -a is not a valid variable.

but Mathematica still outputs more or less the correct answer

$$ \frac{\partial \left(\left(\text{C3} \cosh \left(\frac{a k}{\sqrt{\text{Gx}}}\right)-\text{C4} \sinh \left(\frac{a k}{\sqrt{\text{Gx}}}\right)\right) \left(\text{C1} \cos \left(\frac{k y}{\sqrt{\text{Gy}}}\right)+\text{C2} \sin \left(\frac{k y}{\sqrt{\text{Gy}}}\right)\right)\right)}{\partial -a} $$

Is there a problem with when I take the partial derivative of ψ?


closed as off-topic by m_goldberg, Henrik Schumacher, glS, Öskå, bbgodfrey Mar 10 at 12:48

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, Henrik Schumacher, glS, Öskå, bbgodfrey
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ Why are you differentiating with respect to -a and not a? Otherwise, what you seem to intend is to differentiate first before plugging in; in which case, try ClearAll[dx]; dx[x_] = D[ψ[x, y], x] $\endgroup$ – J. M. is away Mar 3 at 2:25
  • $\begingroup$ Oh I see! So in this case dx is always the differential of Psi, right? I see my mistake now. What I wanted to achieve instead is to make dx be the result of the partial differential of Psiwrt x and be a function of two variables. Is there a way to do that without having to copy the result of D[ψ[x, y], x] into a new cell and assigning it to dx? $\endgroup$ – enea19 Mar 3 at 2:37
  • 1
    $\begingroup$ If you want partial derivatives, perhaps try Derivative[1, 0][ψ][-a, y] for the first partial derivative with respect to x, evaluated at x == -a. (Derivative[0, 1][ψ][-a, y] can be similarly interpreted.) $\endgroup$ – J. M. is away Mar 3 at 2:41
  • 1
    $\begingroup$ These tutorials may be useful for the distinction between immediate = and delayed := assignments: reference.wolfram.com/language/tutorial/… and wolfram.com/language/elementary-introduction/2nd-ed/… $\endgroup$ – Roman Mar 3 at 2:46

As Roman pointed out, the problem was that I was using := instead of =. The following code solved the problem:

dx[x_,y_] = D[ψ[x, y], x]

It is a matter as to when the value -a is substituted for x during evaluation. You want the differentiation to be made before the substitution, but the way you wrote the code the substitution is done first. The two normal ways to fix the problem is to write

 dx[x_] := Evaluate@D[ψ[x, y], x]

or else

 dx[x_] = D[ψ[x, y], x];

In either case, d[-a] will now return



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