0
$\begingroup$

Sorry, I am new to Mathematica and this problem is doing my head in. I have the code in the picture below, I would expect Mathematica to do the derivative wrt q_1 when I write U'[q_1]. However, it just rewrites the command.

I would appreciate it if sb could point out what I am doing wrong and how I could solve it.

KR enter image description here

$\endgroup$
4
  • 2
    $\begingroup$ U’ denotes the derivative of U and U’[q] denotes the derivative value at q. However q_1 denotes a Pattern [] object with the head 1, which is almost certainly not what you meant. If U’[q] does not evaluate to something else then it’s because there are no definitions for U to be applied $\endgroup$
    – Michael E2
    Mar 15, 2022 at 20:10
  • 1
    $\begingroup$ Welcome to Mathematica StackExchange! For your further questions, please include your code in a copy-pastable form, not as a screenshot. And to answer your question: You have defined U with three arguments. Therefore, there is no such thing as U with one argument in your code. To get the derivative with respect to the first argument, use D[U[q0, q1, q2], q0]. However, as @MichaelE2 already pointed out, you also should avoid using subscripts. $\endgroup$
    – Domen
    Mar 15, 2022 at 20:16
  • 2
    $\begingroup$ In Mathematica, the square brackets are reserved for arguments of functions. Therefore, placing a subexpression at the end of your expression into square brackets is a syntactic error. Use the round ones. $\endgroup$ Mar 15, 2022 at 20:47
  • $\begingroup$ The image was too small for me to see. I think you're asking for Derivative[0, 1, 0][U][q1, q2, q3]. You want the partial derivative, not U', I think. $\endgroup$
    – Michael E2
    Mar 16, 2022 at 1:28

1 Answer 1

1
$\begingroup$
Clear[U, Subscript[q, 0], Subscript[q, 1], Subscript[q, 2]]
U[a_, b_, c_] := 
  Subscript[\[Alpha], 0] a + Subscript[\[Alpha], 1] b + 
   Subscript[\[Alpha], 2] c + (1/2) (Subscript[\[Beta], 1] b^2 +           Subscript[\[Beta], 2] c^2 + 2 \[Gamma] b c);
Subscript[\[Alpha], 0] = 1
U[Subscript[q, 0], Subscript[q, 1], Subscript[q, 2]]
D[U[Subscript[q, 0], Subscript[q, 1], Subscript[q, 
  2]], Subscript[q, 1]]

$$\alpha _1 q_1+\alpha _2 q_2+\frac{1}{2} \left(\beta _1 q_1^2+\beta _2 q_2^2+2 \gamma q_2 q_1\right)+q_0$$

$$\alpha _1+\frac{1}{2} \left(2 \beta _1 q_1+2 \gamma q_2\right)$$

$\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.