3

I learned not use the Norm[] function when computing vector derivative, so I use the dot product instead:

In: D[x.x, x]
Out: 1.x + x.1

What does the result mean? Is 1=(1, 1, .., 1) here? Why can't it show just 2x as the result? And Mathematica won't resolve it when I define x?

In: 1.x + x .1 /. x -> {3, 4}
Out: {0.3 + 1.{3, 4}, 0.4 + 1.{3, 4}}
Soid
  • 133
  • 3
  • 3
    One way to approach this to define x = Array[a, 3]; Then you can take the derivative x = D[x . x, {x}] and you'll get more what you expect. Otherwise it doesn't know what the dimensions of x are (if its a scalar, vector, matrix). – bill s Apr 11 '21 at 20:17
  • Thanks, now it makes sense why, since it might be a matrix. Can I tell it the "type"? I tried $Assumptions = (x) \[Element] Vectors[2, Reals]; but it didn't help. Also, if I use Array[a, 3], can I resolve a at some point? For example, take a derivative and then compute it for certain vector. D[x.x, {x}] /. x -> {1, 2, 3} doesn't seem to work – Soid Apr 11 '21 at 20:41
  • Related: https://mathematica.stackexchange.com/q/89651 – Michael E2 Apr 11 '21 at 21:35
  • There is not really a nice way to generically say "x is a 3 vector" using assumptions. But if you think about it, x = Array[a, 3]; is effectively a declaration that x is an arbitrary 3 vector. – bill s Apr 11 '21 at 22:00

1 Answers1

3

As suggested by @bill s, you can write

x = Array[a, 3];
deriv = D[x . x, {x}]
(* {2 a[1], 2 a[2], 2 a[3]} *)

Note that the you need to put {x} rather than x (otherwise it will attempt to interpret the 2nd item in the list as the order of the derivative - see the help for D - the syntax is rather over loaded).

The easiest way to substitute values is perhaps

deriv /. Thread[x -> {1, 2, 3}]
(* {2, 4, 6} *)
mikado
  • 16,741
  • 2
  • 20
  • 54