0

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

1 Answers1

1

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]
jiaoeyushushu
  • 911
  • 6
  • 10