3

I'm trying to plot the vector field $F(x,y) = \cos (x+y) \vec{i} + x \vec{j}$ using tikz. I followed another topic on plotting vector fields I saw here, used to plot some other vector fields and it went pretty well, but this one is driving me nuts. The goal is this

enter image description here

but instead I'm getting this

enter image description here

using this code

\begin{tikzpicture}[trig format = rad]
\begin{axis}[ticks=none,
  view     = {0}{90},
  domain   = -1:1,
  y domain = -1:1,
  samples  = 21,
]

\addplot3 [cyan, quiver={u={cos (x + y)}, v={x}, scale arrows=0.1},samples=10, -latex] (x,y,0); \end{axis} \end{tikzpicture}

I didn't even try to put the lines that represent the $x$ and $y$ axis. First I wasn't using trig format = rad and it wasn't quite right. Then I tried changing the $x$ and $y$ domain, but it only made it worse. Then I tried using trig format = rad but then I got this random line there and the vector field isn't even what I wanted. Can anybody give some help please? Thanks in advance!

tulio
  • 338
  • 1
  • 9

1 Answers1

4

To get the arrow at center of grid coordinate, the (x,y) is subtracted half the arrow length.

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{arrows.meta}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\clip[rounded corners] (-3.2,-3.2) rectangle (3.2,3.2);
\begin{axis}[
x=1cm, y=1cm, z=0cm,
view={0}{90},
anchor=center,
trig format plots=rad,
xmin=-3, xmax=3,
ymin=-3, ymax=3,
axis lines=center,
domain=-3:3,
y domain=-3:3,
enlargelimits=0.1,
ticks=none,
]
\addplot3[
cyan, thick,
point meta={sqrt((cos(x+y))^2+x^2)},
quiver={
  u={cos(x+y)}, v={x},
  scale arrows=0.2,
  every arrow/.append style={-{Triangle[scale=0.2+0.8*\pgfplotspointmetatransformed/1000]}},
},
samples=10,
] (x-0.1*cos(x+y),y-0.1*x,0);
\end{axis}
\draw[cyan, ultra thick, rounded corners] (-3.2,-3.2) rectangle (3.2,3.2);
\end{tikzpicture}
\end{document}

Quiver plot in a frame

Edit: A more correct plot would be to calculate the arrow vector from this new position like in red below

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{arrows.meta}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\clip[rounded corners] (-3.2,-3.2) rectangle (3.2,3.2);
\begin{axis}[
x=1cm, y=1cm, z=0cm,
view={0}{90},
anchor=center,
trig format plots=rad,
xmin=-3, xmax=3,
ymin=-3, ymax=3,
axis lines=center,
domain=-3:3,
y domain=-3:3,
enlargelimits=0.1,
ticks=none,
]
\addplot3[
cyan, thick,
point meta={sqrt((cos(x+y))^2+x^2)},
quiver={
  u={cos(x+y)}, v={x},
  scale arrows=0.2,
  every arrow/.append style={-{Triangle[scale=0.2+0.8*\pgfplotspointmetatransformed/1000]}},
},
samples=10,
] (x-0.1*cos(x+y),y-0.1*x,0);
\addplot3[
red, thick,
point meta={sqrt((cos((x-0.1*cos(x+y))+y))^2+(x-0.1*cos(x+y))^2)},
quiver={
  u={cos((x-0.1*cos(x+y))+y)}, v={x-0.1*cos(x+y)},
  scale arrows=0.2,
  every arrow/.append style={-{Triangle[scale=0.2+0.8*\pgfplotspointmetatransformed/1000]}},
},
samples=10,
] (x-0.1*cos(x+y),y-0.1*x,0);\end{axis}
\draw[cyan, ultra thick, rounded corners] (-3.2,-3.2) rectangle (3.2,3.2);
\end{tikzpicture}
\end{document}

quiver plot with modified arrows

  • What does point metamean? Everything else I think I understand. – tulio Oct 14 '22 at 12:38
  • 1
    point meta is just some extra user defined "coordinate". It can be used for many things. Here I use it to set the size of the of the arrow tip app. proportional to the length of the arrow. – hpekristiansen Oct 14 '22 at 17:29