0
$\begingroup$

I use a code to plot3D gradient (the vector of the partial derivatives) of a function f to surface

z=x+y-xy

such that

fieldArrow[pos_, field_, scale_] := {Hue[Norm[field]], 
   Arrowheads[.02], Arrow[Tube[{{pos, pos + scale field}}]]};

grid = Table[{x , y}, {y, -5, 5, 5}, {x, -5, 5, 5}];

gridData = Table[x + x - x y, {y, -5, 5, 5}, {x, -5, 5, 5}];

fieldX = -DerivativeFilter[gridData, {0, 1}, InterpolationOrder -> 3];
fieldY = -DerivativeFilter[gridData, {1, 0}, InterpolationOrder -> 3];
data3D = MapThread[Append[#1, #2] &, {grid, gridData}, 2];

vectorField = 
  MapThread[
   fieldArrow[Append[#1, #2], {#3, #4, 0}, 3] &, {grid, gridData, 
    fieldX, fieldY}, 2];

arrows = Graphics3D[vectorField];

Show[ListPlot3D[Flatten[data3D, 1], MeshFunctions -> (#3 &)], arrows]

but, it not be a good graphic as enter image description here

Can the output be improved to give a similar shape to this, enter image description here

$\endgroup$
  • 2
    $\begingroup$ Have you seen VectorPlot3D[]? $\endgroup$ – J. M. will be back soon Sep 2 '17 at 5:41
  • $\begingroup$ @J.M. I am starting with Mathematica and I do not have enough experience, but I tried many attempts and did not find a suitable result $\endgroup$ – user44376 Sep 2 '17 at 5:45
  • $\begingroup$ You should cite your source: The code is based on, and the image is from,this answer. $\endgroup$ – Michael E2 Sep 3 '17 at 20:37
  • $\begingroup$ You might change the grid and gridData delta from 5 to maybe 1; it was 0.5 in the answer you copied. (I mean the last 5 in {y, -5, 5, 5} and in the one for x.) $\endgroup$ – Michael E2 Sep 3 '17 at 20:45
1
$\begingroup$

Try this:

scalarField = x + y - x y - z;
vectorField = D[scalarField, {{x, y, z}}];
g = Graphics3D[Cone[{{-0.5, 0, 0}, {1.5, 0, 0}}, 0.5]];
v = VectorPlot3D[vectorField, {x, -5, 5}, {y, -5, 5}, {z, -5, 5}, 
   VectorPoints -> 20, VectorScale -> {Automatic, Scaled[0.5]}, 
   RegionFunction -> Function[{x, y, z}, 0 <= scalarField <= 2], 
   VectorColorFunction -> "Rainbow", VectorStyle -> {g}];
c = ContourPlot3D[
   scalarField == 0, {x, -5, 5}, {y, -5, 5}, {z, -5, 5}, Mesh -> None,
    ContourStyle -> Opacity[0.5, LightBlue]];
Show[v, c]
$\endgroup$

Your Answer

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