5
$\begingroup$

Let's take the following example

V = (-G*Mn)/Sqrt[x^2 + y^2 + z^2 + cn^2];
Vx = D[V, x];
FortranForm[Vx]

The result is (G*Mn*x)/(cn**2 + x**2 + y**2 + z**2)**1.5.

My questions is the following:

  1. How can I replace x, y, z with x(1), x(2), x(3) respectively?

  2. How can I get rid of the spaces between the plus signs?

$\endgroup$
7
$\begingroup$
V = (-G*Mn)/Sqrt[x^2 + y^2 + z^2 + cn^2];
Vx = D[V, x] /. {x -> x[1], y -> x[2], z -> x[3]};
StringReplace[ToString[Vx, FortranForm], " " -> ""]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Many thanks! However according to my post the replacement rule should be: {x -> x[1], y -> x[2], z -> x[3]}. $\endgroup$ – Vaggelis_Z Jun 30 '15 at 13:23
  • $\begingroup$ @Vaggelis_Z fixed $\endgroup$ – Rolf Mertig Jul 1 '15 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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