4
$\begingroup$

I am not pasting my input code here since the website modifies my code. Instead I am attaching a screenshot of my notebook.

Issue: I enter two separate commands (asking Mathematica to differentiate in both case). I expected the answers to be '2' and '0' respectively, but it spits out some weird thing in the second case. Please tell me what went wrong?

In short, I expect Mathematica to identify 'x' and 'x_{p}' (where '_' stands for subscript) as two independent variables while performing the differentiation but it looks like it has some trouble in identifying variable name with a subscript component.

enter image description here

$\endgroup$
5
  • 2
    $\begingroup$ This is perhaps the commonest problem associated with subscripts. See this site for multiple explanations $\endgroup$
    – mikado
    Commented Sep 4, 2016 at 18:55
  • $\begingroup$ @mikado, you did not give the website. Please do so. And thanks! $\endgroup$ Commented Sep 4, 2016 at 19:45
  • $\begingroup$ This site means this site, Mma.SE :) -- Search this site for subscript, Symbolize and the Notation package, for instance. $\endgroup$
    – Michael E2
    Commented Sep 4, 2016 at 20:04
  • 2
    $\begingroup$ Please note that $x_p$ in your second example is not a symbol. Rather, it is an expression that is a function of two separate symbols Subscript[x,p], which merely formats as $x_p$. Therefore, Mathematica uses the chain rule to differentiate, giving the correct result. Never ever EVER use subscript to denote symbols. Use xp instead. $\endgroup$
    – QuantumDot
    Commented Sep 4, 2016 at 23:23
  • 1
    $\begingroup$ If you insist on Mathematica interpreting $x_p$ as a totally independent symbol, then use the following two lines: MakeBoxes[xp, StandardForm] := SubscriptBox["x", "p"]; MakeExpression[SubscriptBox["x", "p"], StandardForm] := MakeExpression["xp", StandardForm]. Then in all subsequent evaluations in the session, xp will display as $x_p$, but will be interpreted as xp behind-the-scenes. Only then, would D[$x_p$,x] yield zero as expected. (Try also running FullForm[$x_p$] to understand what's going on). $\endgroup$
    – QuantumDot
    Commented Sep 4, 2016 at 23:31

2 Answers 2

4
$\begingroup$

A subscripted symbol is an expression involving the symbol. It is not a new symbol. Here is what you are asking Mma to do:

D[Times[2, Subscript[x, p]], x]

This is an expression in x, so Mma does exactly what you ask it to: it differentiates an expression in x with respect to x.

$\endgroup$
4
$\begingroup$

You can use the Notation` package to get $x_p$ treated as a symbol.

Needs["Notation`"]
Symbolize[ParsedBoxWrapper[SubscriptBox["x", "p"]]]
D[2*x⎵Subscript⎵p^2, x⎵Subscript⎵p]
4*x⎵Subscript⎵p

This looks rather strange in input form, but in standard form it looks as you would expect it to.

image

Or even more generally,

cells

Which is the same as

grad

$\endgroup$

Your Answer

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

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