你能发现错误吗?我正在尝试绘制一个具有两个参数的函数,即微分方程的初始条件。同样的模式适用于简单函数。
\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usetikzlibrary{calc,patterns}
\usetikzlibrary{arrows.meta}
\tikzset{arrow/.style={-{Stealth[length=2mm, width=2mm, sep=0.5pt]}}}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=2.5,
declare function={ fctP(\x,\a,\b) = (sqrt(\b)+(\x-\a)/2)**2;
fctN(\x,\a,\b) = - (sqrt(-\b)+(\a-\x)/2)**2; }]
\draw [arrow] (-2.2,0) -- (2.2,0) node [below] {$x$};
\draw [arrow] (0,-1.2) -- (0,1.2) node [right] {$y$};
\draw[samples=200,domain=1:3,smooth,variable=\x,thick] plot ({\x},{fctP(\x,3,1)}) node[above right] {$?$};
\end{tikzpicture}
\end{center}
\end{document}
答案1
主要问题是您使用**
而不是^
进行 pgfmath
指数运算。我还调整了轴范围并删除了一些您不使用的内容。我fctN()
故意保留了 的定义,以作为如何一次定义多个函数的示例pgfmath
,但它未在代码中使用。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\tikzset{arrow/.style={-{Stealth[length=2mm, width=2mm, sep=0.5pt]}}}
\begin{document}
\begin{tikzpicture}[scale=2.5,
declare function={
fctP(\x,\a,\b) = (sqrt(\b)+(\x-\a)/2)^2;
fctN(\x,\a,\b) = - (sqrt(-\b)+(\a-\x)/2)^2; }]
\def\myxMax{3.0}
\draw [arrow] (-0.2,0) -- (\myxMax,0) node [below] {$x$};
\draw [arrow] (0,-0.2) -- (0,1.2) node [right] {$y$};
\draw[samples=200, domain=1:\myxMax, smooth, variable=\x, thick]
plot ({\x}, {fctP(\x,3,1)}) node[above right] {$?$};
\end{tikzpicture}
\end{document}
注意:\usetkzobj{all}
我删除的 也导致我的计算机出现问题(Command \tkzMarkRightAngle already defined).
我不知道这是tkz-euclide
错误还是我的安装存在问题。无论如何,由于tkz-euclide
在您的示例中未使用 ,因此删除此行(以及\usepackage{tkz-euclide}
)可解决问题。我向 的作者发送了一封邮件,tkz-euclide
其中包含以下最少的代码:
\documentclass{article}
\usepackage{tikz}
\usepackage{tkz-euclide}
%\usetkzobj{all} you can remove this line with the new version
\begin{document}
\end{document}
没必要再打扰他。:)
已完成 OP (@PeptideChain) 的剧情
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\tikzset{
bullet/.style={circle, fill, minimum size=4pt, inner sep=0pt, outer sep=0pt},
arrow/.style={-{Stealth[length=2mm, width=2mm, sep=0.5pt]}}
}
\begin{document}
\begin{tikzpicture}[scale=1.5,
declare function={
fctP(\x,\a,\b) = (sqrt(\b)+(\x-\a)/2)^2;
fctN(\x,\a,\b) = - (sqrt(-\b)+(\a-\x)/2)^2; }]
\draw [arrow] (-2.4,0) -- (3.4,0) node [below] {$x$};
\draw [arrow] (0,-1.4) -- (0,1.4) node [right] {$y$};
\draw[samples=200, domain=1:3.2, smooth, variable=\x, thick]
plot ({\x}, {fctP(\x,3,1)}) node[above right] {$y(x)$};
\draw[samples=200, domain=0:1, smooth, variable=\x, thick]
plot ({\x},{0});
\draw[samples=200, domain=-2.2:0, smooth, variable=\x, thick]
plot ({\x}, {fctN(\x,-2,-1)});
\draw (1,0) -- node [below] {$1$} (1,-0.03);
\draw (2,0) -- node [below] {$2$} (2,-0.03);
\draw (3,0) -- node [below] {$3$} (3,-0.03);
\draw (-1,0) -- node [above] {$-1$} (-1,-0.03);
\draw (-2,0) -- node [above] {$-2$} (-2,-0.03);
\draw (0,1) -- node [left] {$1$} (-0.03,1);
\draw (0,-1) -- node [right] {$-1$} (-0.03,-1);
\node[bullet, minimum size=3pt] at (3,1) {};
\node[bullet, minimum size=3pt] at (-2,-1) {};
\draw [dotted] (0,1) -- (3,1);
\draw [dotted] (3,0) -- (3,1);
\draw [dotted] (-2,0) -- (-2,-1);
\draw [dotted] (0,-1) -- (-2,-1);
\end{tikzpicture}
\end{document}