# My function for computing a partial derivative generates an error message [closed]

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]
dx[-a]


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 ψ?

• 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] – J. M.'s technical difficulties Mar 3 '19 at 2:25
• 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? – enea19 Mar 3 '19 at 2:37
• 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.) – J. M.'s technical difficulties Mar 3 '19 at 2:41
• 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/… – Roman Mar 3 '19 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 