f(x,y) = -2 + 3x - y - x^2 - xy - y^2 > 0
我想创建一个图,表示 TikZ 中 范围内的区域[-2.5, 2.5] \times [2.5, 2.5]
。由于这是一个隐式函数,我认为我必须使用gnuplot
,我目前只能绘制f(x,y) = 0
,我曾尝试使用:
[编辑:更新了最少的工作示例]
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.38]
\clip (-2.5,-2.5) rectangle (2.5,2.5);
\draw plot[raw gnuplot, fill=blue!50] function{
f(x,y) = -2 * 1 + 3 * x + -1 * y + -1 * x**2 + -1 * x*y + -1 * y**2;
set view 0,0;
set isosample 1000, 1000;
set size square;
set contour base;
set cntrparam levels incre 0,0.1,0;
unset surface;
splot [-2.5:2.5] [-2.5:2.5] f(x,y);
};
\draw (-2.5,-2.5) rectangle (2.5,2.5);
\end{tikzpicture}%
\end{document}
产生以下结果:
我也尝试过使用以下函数,但它却给了我一个“块状”轮廓。
f(x, y) = (\coeff[#1,1] * 1 + \coeff[#1,2] * x + \coeff[#1,3] * y + \coeff[#1,4] * x**2 + \coeff[#1,5] * x*y + \coeff[#1,6] * y**2 > 0)?0:1;
有没有什么办法可以为该区域着色f(x,y)>0
?
PS 由于我有很多不同的二次方程需要着色,因此我正在寻找一种可以对二次方程进行着色的解决f(x,y)>0
方案f(x,y)
。
答案1
问题是 gnu plot 生成了许多短的拉伸而不是一个连续的拉伸。您可以通过查看生成的 .table 文件来看到这一点。您也可以通过将其放置在fill=blue!50
其所属的位置来看到这一点,即\draw[fill=blue] plot ...
。可能可以说服 gnu plot 在一次拉伸中绘制轮廓。但是,也可以通过分析解决问题。众所周知,隐式二次方程的解导致圆锥曲线。在 tikz 中实现解析解很简单,但很乏味。pic
在此代码中,这针对椭圆的情况进行了说明,这是您选择系数后得到的。其他情况类似。在您的用例中,您将得到
这是定义并生成该图的代码pic
以及一些讨论。
\documentclass{article}
\usepackage[fleqn]{amsmath}
\usepackage{xurl}
\usepackage{tikz}
\tikzset{declare function={%
conicdet2(\a,\b,\c)=-\b*\b/4+\a*\c;
conicdet3(\a,\b,\c,\d,\e,\f)=(-\c*\d*\d+\b*\d*\e-\a*\e*\e-\b*\b*\f+4*\a*\c*\f)/4;},
pics/conic section/.style n args={6}{code={%
\pgfmathtruncatemacro{\mydiscriminant}{sign(-4*conicdet2(#1,#2,#3))+1}%
\ifcase\mydiscriminant\relax
% ellipse
\pgfmathsetmacro{\mya}{-sqrt(2*((#1)*(#5)*(#5)+(#3)*(#4)*(#4)-(#2)*(#4)*(#5)+((#2)*(#2)-4*(#1)*(#3))*(#6))*((#1)+(#3)+sqrt(((#1)-(#3))*((#1)-(#3))+(#2)*(#2))))/((#2)*(#2)-4*(#1)*(#3))}%
\pgfmathsetmacro{\myb}{-sqrt(2*((#1)*(#5)*(#5)+(#3)*(#4)*(#4)-(#2)*(#4)*(#5)+((#2)*(#2)-4*(#1)*(#3))*(#6))*((#1)+(#3)-sqrt(((#1)-(#3))*((#1)-(#3))+(#2)*(#2))))/((#2)*(#2)-4*(#1)*(#3))}%
\pgfmathsetmacro{\myx}{(2*(#3)*(#4)-(#2)*(#5))/((#2)*(#2)-4*(#1)*(#3))}%
\pgfmathsetmacro{\myy}{(2*(#1)*(#5)-(#2)*(#4))/((#2)*(#2)-4*(#1)*(#3))}%
\ifdim#2pt=0pt\relax
\ifdim#1pt<#3pt\relax
\pgfmathsetmacro{\mytheta}{0}%
\else
\pgfmathsetmacro{\mytheta}{90}%
\fi
\else
\pgfmathsetmacro{\mytheta}{atan((#2)/((#3)-(#1)-sqrt(((#1)-(#3))*((#1)-(#3))+(#2)*(#2))))}%
\fi
% debug
%\typeout{A=#1,B=#2,C=#3,D=#4,E=#5,F=#6,a=\mya,b=\myb,x=\myx,y=\myy,theta=\mytheta}%
\draw[pic actions,rotate around={\mytheta:(\myx,\myy)},shift={(\myx,\myy)}] circle[x radius=\mya,y radius=\myb];
\or
% parabola
% not implemented
\or
% hyperbola
% not implemented
\fi
}}
}
\begin{document}
It is well known that the implicit equation
\begin{equation}\label{eq:quadratic}
A\,x^2+B\,x\,y+C\,y^2+D\,x+E\,y+F=0
\end{equation}
leads to conic sections, see e.g.\ \url{https://en.wikipedia.org/wiki/Conic_section}. Depending on the sign of $B^2-4AC$ one obtains an ellipse ($<0$), parabola ($=0$) or hyperbola ($>0$). The ellipse case is discussed in more detail at e.g.\ \url{https://en.wikipedia.org/wiki/Ellipse#Canonical_form}, where explicit expressions for the axes, shift and rotations in terms of the coefficients of the quadratic equation \eqref{eq:quadratic} are given. This allows one to define a \texttt{pic} that draws the conic section in terms of the parameters.
\begin{figure}[htb]
\centering
\begin{tikzpicture}[scale=0.38]
\draw (-2.5,-2.5) rectangle (2.5,2.5);
\clip (-2.5,-2.5) rectangle (2.5,2.5);
\path pic[transform shape,fill=blue]{conic section={-1}{-1}{-1}{3}{-1}{-2}};
\draw plot[raw gnuplot] function{
f(x,y) = -2 * 1 + 3 * x + -1 * y + -1 * x**2 + -1 * x*y + -1 * y**2;
set view 0,0;
set isosample 1000, 1000;
set size square;
set contour base;
set cntrparam levels incre 0,0.1,0;
unset surface;
splot [-2.5:2.5] [-2.5:2.5] f(x,y);
};
\end{tikzpicture}%
\caption{Elliptical conic section.}
\end{figure}
\end{document}