我目前正在尝试弄清楚如何创建包含两条曲线并阴影它们之间的区域的某些图形:
其中tikz
和pgftools
。在第一幅图中,曲线为:f(x)=1/2*x^2-2*x+5 和 g(x)=-1/10*x^2+2 以及 a=1、b=4。在第二幅图中,曲线可以是 f(x)=-1/6*x^2+2、g(x)=1/4*x^2 以及 x_1=-2.19 和 x_2=2.19。
我已经阅读了这里的几个帖子,但我似乎无法弄清楚代码是什么,所以我希望你能帮助我为我的课程提供一些漂亮的图形:)
答案1
使用最近的(v1.10)fillbetween
pgfplotslibrary并查看pgfplots文档或评论中的建议答案,很容易绘制类似的
使用下一个代码:
\documentclass[border=3mm,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,
xlabel=$x$,
ylabel=$y$,
enlargelimits,
ytick=\empty,
xtick={1,4},
xticklabels={a,b}]
\addplot[name path=F,blue,domain={-.2:5}] {0.5*x^2-2*x+5} node[pos=.8, above]{$f$};
\addplot[name path=G,green,domain={-.2:5}] {-0.1*x^2+2}node[pos=.1, below]{$g$};
\addplot[pattern=north west lines, pattern color=brown!50]fill between[of=F and G, soft clip={domain=1:4}]
;
\node[coordinate,pin=30:{$A$}] at (axis cs:3.8,3){};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,
xlabel=$x$,
ylabel=$y$,
enlargelimits,
ytick=\empty,
xtick={-2.19,2.19},
xticklabels={$x_1$,$x_2$}]
\addplot[name path=F,blue,domain={-4:4}] {-(1/6)*x^2+2} node[pos=1, below]{$f$};
\addplot[name path=G,green,domain={-4:4}] {0.25*x^2}node[pos=1, above]{$g$};
\addplot[pattern=north west lines, pattern color=brown!50]fill between[of=F and G, soft clip={domain=-2.19:2.19}]
;
\node[coordinate,pin=60:{$A$}] at (axis cs:1.1,1.6){};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
只是为了用 PSTricks 进行打字练习,所以我保留原有的代码,没有进行优化。
一
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot,pst-eucl}
\def\f{x -.5 sub x 2.5 sub mul 2 div }
\def\g{\f neg }
\begin{document}
\begin{pspicture}[dimen=m](-2,-2)(4.5,4.5)
\psaxes[ticks=none,labels=none]{->}(0,0)(-2,-2)(4,4)[$x$,0][$y$,90]
\pscustom[fillstyle=vlines,linestyle=none,hatchcolor=lightgray]{\psplot{-1}{3}{\f 2 add}\psplot{3}{-1}{\g 1 sub}}
\psplot{-1}{3}{\f 2 add}\psplot{-1}{3}{\g 1 sub}
\psxTick(-1){a}\psxTick(3){b}
\rput(1,.5){$A$}
\uput[45](!3 /x ED x \f 2 add){$f$}\uput[-45](!3 /x ED x \g 1 sub){$g$}
\end{pspicture}
\end{document}
二
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot,pst-eucl}
\def\f{x -.5 sub x 2.5 sub mul 2 div }
\def\g{\f neg }
\begin{document}
\begin{pspicture}[dimen=m,saveNodeCoors,hatchsep=2pt](-2,-2)(4.5,4.5)
\pscustom[fillstyle=vlines,linestyle=none,hatchcolor=lightgray]{\psplot{-1}{3}{\f 3 add}\psplot{3}{-1}{\g .5 sub}}
% additional procedures:
\bgroup
\psset{PointName=none,PointSymbol=none}
\pstInterFF{\g .5 sub}{0}{0}{A}
\pstInterFF{\g .5 sub}{0}{2}{B}
\egroup
\pscustom[fillstyle=vlines,linestyle=none,hatchcolor=red]{\psplot{N-A.x}{N-B.x}{\g .5 sub}\psplot{N-B.x}{N-A.x}{0}}
\psaxes[ticks=none,labels=none]{->}(0,0)(-2,-2)(4,4)[$x$,0][$y$,90]
\psplot{-1}{3}{\f 3 add}\psplot{-1}{3}{\g .5 sub}
\psxTick(-1){a}\psxTick(3){b}
\rput(1,1.25){$A$}
\uput[45](!3 /x ED x \f 3 add){$f$}\uput[-45](!3 /x ED x \g .5 sub){$g$}
\end{pspicture}
\end{document}
三
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot,pst-eucl}
\def\f{x -.5 sub x 2.5 sub mul 2 div }
\def\g{\f neg }
\begin{document}
\begin{pspicture}[dimen=m,saveNodeCoors,hatchsep=2pt](-2,-2)(4.5,4.5)
% additional procedures:
\bgroup
\psset{PointName=none,PointSymbol=none}
\pstInterFF{\g}{\f 1.2 add}{0}{A}
\pstInterFF{\g}{\f 1.2 add}{2}{B}
\egroup
\pscustom[fillstyle=vlines,linestyle=none,hatchcolor=lightgray]{\psplot{N-A.x}{N-B.x}{\f 1.2 add}\psplot{N-B.x}{N-A.x}{\g}}
\psaxes[ticks=none,labels=none]{->}(0,0)(-2,-2)(4,4)[$x$,0][$y$,90]
\psplot{-1}{3}{\f 1.2 add}\psplot{-1}{3}{\g}
\psxTick(-1){a}\psxTick(3){b}
\rput(!N-A.x N-B.x add 2 div .6){$A$}
\uput[45](!3 /x ED x \f 1.2 add){$f$}\uput[-45](!3 /x ED x \g){$g$}
\end{pspicture}
\end{document}
最新更新
如果您想使用中缀表示法而不是后缀表示法,但同时又因为未知原因不想使用algebraic
选项,请考虑以下内容。我只是使用上面的第一种情况作为示例。第二种和第三种情况留给您练习。
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot,pst-eucl}
\def\f{((x-.5)*(x-2.5)/5)}
\def\g{(-\f)}
\pstVerb{/I2P {exec AlgParser cvx exec} def}
\begin{document}
\begin{pspicture}[dimen=m](-2,-2)(4.5,4.5)
\psaxes[ticks=none,labels=none]{->}(0,0)(-2,-2)(4,4)[$x$,0][$y$,90]
\pscustom[fillstyle=vlines,linestyle=none,hatchcolor=gray]{\psplot{-1}{3}{{(\f+2)} I2P}\psplot{3}{-1}{{(\g-1)} I2P}}
\psplot{-1}{3}{{(\f+2)} I2P}\psplot{3}{-1}{{(\g-1)} I2P}
\psxTick(-1){a}\psxTick(3){b}
\rput*(*1 .5){$A$}
\uput[45](*3 {\f+2}){$f$}\uput[-45](*3 {\g-1}){$g$}
\end{pspicture}
\end{document}
答案3
为了使用 MetaPost 的乐趣,我利用了一个基于非常方便的宏的我自己的宏buildcycle
,以便创建限制两个曲线之间区域的路径。
这里应用于 OP 的第二个示例,这是一个特殊情况,因为两条曲线相交。如果今晚我有更多时间,我会添加一个更通用的应用程序。
input latexmp;
setupLaTeXMP(options="12pt", textextlabel=enable, mode = rerun);
vardef graph_of_function (suffix f) (expr xmin, xmax, xsep) =
for x = xmin step xsep until xmax: (x, f(x)) .. endfor (xmax, f(xmax))
enddef ;
vardef vline (suffix f, g) (expr x) = (x, min(f(x), g(x))) -- (x, max(f(x),g(x))) enddef;
vardef area_between_functions (suffix f, g)(expr a, b, xsep) =
buildcycle(graph_of_function(f, a, b, xsep), vline(f, g, b),
reverse graph_of_function(g, a, b, xsep), reverse vline(f, g, a))
enddef;
vardef xaxis (expr xmin, xmax) = (xmin, 0) -- (xmax, 0) enddef ;
vardef yaxis (expr ymin, ymax) = (0, ymin) -- (0, ymax) enddef ;
beginfig(1);
xmin:=-4.5; xmax := 4.5;
ymin := -1.5; ymax := 5;
u:=1cm;
vardef f(expr x)= (-1/6)*(x**2)+2 enddef;
vardef g(expr x)= 0.25*(x**2) enddef;
path C_f, C_g;
C_f = graph_of_function(f,xmin, xmax, .1);
C_g = graph_of_function(g, xmin, xmax, .1);
a := xpart(C_f intersectionpoint C_g); %a \approx -2.19;
b := xpart(reverse C_f intersectionpoint reverse C_g); %b \approx 2.19;
fill (area_between_functions(f, g, a, b, 0.1)) scaled u withcolor 0.8white;
draw C_f scaled u;
draw C_g scaled u;
draw ((a, 0)*u -- (a, g(a))*u) dashed evenly;
draw ((b, 0)*u -- (b, g(b))*u) dashed evenly;
drawarrow xaxis(xmin, xmax) scaled u ;
drawarrow yaxis(ymin, ymax) scaled u ;
label.bot("$a \approx" & decimal((round(100*a))/100) & "$", (a*u, 0));
label.bot("$b \approx" & decimal((round(100*b))/100) & "$", (b*u, 0));
label.bot("$x$", (xmax*u, 0));
label.lft("$y$", (0, ymax*u));
label.rt("$f$", (xmax, f(xmax))*u);
label.rt("$g$", (xmax, g(xmax))*u);
endfig;
end.
答案4
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\def\f{(x-.5)*(x-2.5)/5+2}
\def\g{-\f-3}
\begin{document}
\begin{pspicture}[algebraic](-2,-2)(4.5,4.5)
\psaxes[ticks=none,labels=none]{->}(0,0)(-2,-2)(4,4)[$x$,0][$y$,90]
\pscustom[fillstyle=crosshatch,linestyle=none,hatchcolor=red!50]{
\psplot{-1}{3}{\f}\psplot{3}{-1}{\g}}
\psplot{-1}{3}{\f}\psplot{3}{-1}{\g}
\psxTick(-1){a}\psxTick(3){b}
\rput*(1,.5){$A$}
\uput[45](*3 {\f}){$f$}\uput[-45](*3 {\g}){$g$}
\end{pspicture}
\end{document}