如何用 PGFPLOT 绘制二维图 – 两个函数的交点

如何用 PGFPLOT 绘制二维图 – 两个函数的交点

我要绘制这两个函数的图:

c_1(c_2): 2 + 1/2c_2
c_2(c_1): 2 + 1/2c_1

c_2 是在轴 y 还是 x 中并不重要(c_1 同样适用)。

我在 desmos 中绘制了函数,但我认为用 pgfplots 绘制图表比直接导入图像更好

图像

我有二维绘图的基本示例,但似乎无法弄清楚如何绘制具有不同变量的两个函数。

我尝试使用这个例子但只得到了错误和错误。

\begin{tikzpicture}
\begin{axis}[
axis lines = left,
xlabel = $c_1$,
ylabel = {$c_2$},
]

%Below the red parabola is defined
\addplot [
domain=0:10,
samples=100,
color=red,
]
{x^2 - 2*x - 1};
\addlegendentry{$x^2 - 2x - 1$}

%Here the blue parabloa is defined
\addplot [
domain=0:10,
samples=100,
color=blue,
]
{x^2 + 2*x + 1};
\addlegendentry{$x^2 + 2x + 1$}

\end{axis}
\end{tikzpicture}

抱歉,这个问题比较简单。我是一名人文专业的本科生。我的数学和 LaTeX 基础不是很好。

答案1

欢迎!如果你似乎想要直线,我不太明白你为什么要绘制抛物线。无论如何,否则你已经很接近了。我改变了一些东西,并添加了一些元素,比如网格。

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}%<- use a smaller version if you have an older installation
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
minor tick num=4,
grid=both,
xlabel = $c_1$,
ylabel = {$c_2$},
xmin=0,
xmax=4,
ymin=0,
enlargelimits=0.1,
legend style={at={(0.1,0.15)},anchor=south west}
]

\addplot [
domain=0:10,
samples=100,
color=blue,
]
{2+x/2};
\addlegendentry{$c_2=\frac{c_1}{2}-1$}

\addplot [
domain=0:10,
samples=100,
color=red,
]
{2*x-4};
\addlegendentry{$c_2=2c_1-4$}
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容