5

I would like to plot the graph of the function f(x,y)=2x^2+3y^2-2xy-4x+7y using TikZ.

I tried the following :

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[axis lines=middle,xmin=-10,ymin=-12,xmax=10,ymax=10,zmin=-30,zmax=490,ticks=none]
    \addplot3 [surf,samples=25,data cs=polar, domain=0:360, y domain=0:10] {2*y^2*cos(x)*cos(x)+3*y^2*sin(x)*sin(x)-2*y^2*sin(x)*cos(x)-4*y*cos(x)+7*y*sin(x)};
\end{axis}
\end{tikzpicture}
\end{document}

which gives me

enter image description here

But I would like to obtain something "cleaner" like this : enter image description here

My attempt is based on the answer of this question , but it is not working as well since my function is not symmetric. Thanks for your help!

  • Your function is as symmetric as the one you are referring to, only in a rotated (and stretched) coordinate system. So perhaps the simplest way would be to diagonalize the Hesse matrix and rotate the axes rather than the plot. – user121799 Mar 11 '18 at 21:32
  • @marmot Yes, that would be a solution, but I do not see how I could stretch the surface and rotate the axis of my TikZ picture... – J. Zapayor Mar 11 '18 at 21:39
  • Mathematica says that if you rotate the coordinate system by 0.3238 pi then the axes of the paraboloid coincide with the coordinate axes, and the eccentricity is about 1.62. And then you may use e.g. this answer to draw the ellipsoid or your above code or some other appropriate code. – user121799 Mar 11 '18 at 21:47
4

enter image description here

\documentclass{article}
\usepackage{amsmath} % only for the blablabla before the plot
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{calc}

\begin{document}
Your function can be plotted symmetrically in the coordinate system defined by
the principal axes of the ellipse. Your function is minimized at $x=x_0=19/10$ and 
$y=y_0=9/5$ with $f(x_0,y_0)=-10.1=:z_0$. So the first step is a translation
\[ x'~=~x-x_0\;,\quad y'~=~y-y_0\quad\text{and}\quad
z'~=~z-z_0\;.\]
The second step is to rotate to the principal axes, which can be achieved by
diagronalizing the Hesse matrix of $f$,
\[
 H~=~\begin{pmatrix} 4 &  -2\\ -2 &  6\end{pmatrix}\;.
\]
This yields a rotation angle of $31.8^\circ$. The perhaps simplest way to
produce the figure is thus to plot the function in the principal axes system and
draw the unrotated axes by hand.
\begin{center}
\begin{tikzpicture}
\begin{axis}
[%view={135}{45},%colormap/blackwhite, 
axis equal,
width=12cm,
axis lines=center, axis on top,
axis line style={draw=none},
ticks=none,
set layers=default,
domain=0:1.50,
samples=20, % this was 200, but I changed it to 20 because of my slow PC
z buffer=sort,
]
\addplot3 [surf,shader=interp,opacity=0.8,
domain y=0:180] ({1.62*cos(y)*sqrt(x)},{sin(y)*sqrt(x)},{x});
\addplot3 [surf,shader=interp,opacity=0.8,
domain y=-180:0,on layer=axis foreground] ({1.62*cos(y)*sqrt(x)},{sin(y)*sqrt(x)},{x});
\coordinate (O) at (axis cs: 0,0); 
\def\AxLen{2.5}
\coordinate (oriX) at (axis cs: {\AxLen*cos(-31.8)},{\AxLen*sin(-31.8)});
\coordinate (oriY) at (axis cs: {-\AxLen*sin(-31.8)},{\AxLen*cos(-31.8)});
\coordinate (oriZ) at (axis cs: 0,0,\AxLen*1);
\coordinate (realO) at ($-{19/10}*(oriX)-{9/5}*(oriY)$);
\end{axis}
\draw[-latex] (O) -- (oriX) node[right]{$x'$};
\draw[-latex] (O) -- (oriY) node[below]{$y'$};
\draw[-latex] (O) -- (oriZ) node[right]{$z'$};
\end{tikzpicture}
\end{center}
\end{document}

EDIT: minor layout and replaced rotated by unrotated.

| improve this answer | |

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.