8

How can I draw a vector field of a function with tikz?

For example f(x,y) = 4x2 + y2 - 5

What I want to plot

2

1 Answer 1

13

Using pgfplots you can draw a vector field with the option quiver. This option creates a 3d vector field, but you can choose to view it 'from above' adding the view={0}{90} to the axis options.

Something like this:

\documentclass[border=2mm]{standalone}
\usepackage {pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
 \begin{tikzpicture}
 \begin{axis}[%
  view     = {0}{90}, % for a view 'from above'
  domain   = -3:3,
  y domain = -3:3,
  xtick    = {-3,...,3},
  ytick    = {-3,...,3},
]
\addplot3[blue, quiver={u=8*x, v=2*y, scale arrows=0.05}, samples=16, -latex] (x,y,0);
\addplot3[red, thick, domain=0:360, samples=41] ({0.5*sqrt(5)*cos(x)},{sqrt(5)*sin(x)},0);
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

4
  • Thank you so much!
    – Huhu
    Aug 11, 2021 at 6:30
  • @Juan Castano: I think the red one is the ellipse 4x^2 + y^2 =5 where the speed (velocity) of solutions of the ODE y' = 4x^2 + y^2 - 5 is vanishing. It seems to contradict with your figure. Am I wrong something?
    – Black Mild
    Nov 15, 2021 at 5:12
  • 1
    @BlackMild, Well, the original post asked for the representation of a vector field but gave the equation of a scalar field. What I did was to assume (I don't know if correctly) that what was needed was to represent the gradient. And the ellipse, in that case, would be a level set. It looks quite similar to the original drawing, but it may be wrong. Nov 15, 2021 at 6:04
  • @JuanCastaño Yeap! I think there is some incorrect information from OP
    – Black Mild
    Nov 15, 2021 at 9:53

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.