如何填充正弦波下方的区域?

如何填充正弦波下方的区域?

我想画一个 alpha 和 beta 错误图,就像

在此处输入图片描述

但是,我没有得到截止颜色附近的区域......

我已完成的工作:

\begin{tikzpicture}
% Achsen zeichnen
\draw[->,thick] (0,0) -- (10,0) node[below] {$x$};
\draw[->,thick] (0,0) -- (0,3.5) node[left] {$y$};
% Achsen beschriften
\draw (1,-.2) -- (1,0) node[below=4pt] {$\scriptstyle1$};
\draw (3.141,-.2) -- (3.141,0) node[below=4pt] {$\scriptstyle \pi$};
\draw (6.283,-.2) -- (6.283,0) node[below=4pt] {$\scriptstyle 2\pi$};   
\foreach \y in {0,1,2}
\draw (-.1,\y) -- (.1,\y) node[left=4pt] {$\scriptstyle\y$};

% Cut-off
\draw[-,thick] (5,0) -- (5,2.5) node[above] {cut off};


% solvent
\draw plot[domain=-.5*pi:1.5*pi] (\x+0.5*pi,{sin(\x r)+1});

% insolvent
\draw plot[domain=-.5*pi:1.5*pi] (\x+1.7*pi,{sin(\x r)+1});

\draw[gray,->] (0,0) -- (pi,0);
\end{tikzpicture}

答案1

这是一个解决方案。它使用交集库,因此我使用内置的 sin 和 cos 路径绘制正弦曲线;交集库在函数图方面效果不佳。

交叉点库计算两条曲线的交叉点;您不必自己摸索着去弄清楚。

代码并不优雅,路径经常重复,但它完成了工作。

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}
% Achsen zeichnen
\draw[->,thick] (0,0) -- (10,0) node[below] {$x$};
\draw[->,thick] (0,0) -- (0,3.5) node[left] {$y$};
% Achsen beschriften
\foreach \x/\xlabel in {1/1,3.141/\pi,6.283/{2\pi}}
\draw (\x,-.2) -- ++(0,0.2) node[below=4pt] {$\scriptstyle\xlabel$};   
\foreach \y in {0,1,2}
\draw (-.1,\y) -- ++(0.1,0) node[left=4pt] {$\scriptstyle\y$};

% solvent
\path[name path = solvent] (0,0) cos (0.5*pi,1) sin (pi,2) cos (1.5*pi,1) sin (2*pi,0);

%unsolvent
\path[shift={(1.2*pi,0)},name path= unsolvent] (0,0) cos (0.5*pi,1) sin (pi,2) cos (1.5*pi,1) sin (2*pi,0);

\begin{scope}
\clip[name intersections={of=solvent and unsolvent}] (intersection-1 |- {(0,0)}) rectangle (2*pi,2);
\fill[blue] (0,0) cos (0.5*pi,1) sin (pi,2) cos (1.5*pi,1) sin (2*pi,0);
\end{scope}

\begin{scope}
\clip[name intersections={of=solvent and unsolvent}] (0,0) rectangle (intersection-1 |- {(0,2)});
\fill[shift={(1.2*pi,0)},red] (0,0) cos (0.5*pi,1) sin (pi,2) cos (1.5*pi,1) sin (2*pi,0);
\end{scope}

% solvent
\draw[blue] (0,0) cos (0.5*pi,1) sin (pi,2) cos (1.5*pi,1) sin (2*pi,0);

%unsolvent
\draw[shift={(1.2*pi,0)},red] (0,0) cos (0.5*pi,1) sin (pi,2) cos (1.5*pi,1) sin (2*pi,0);


%cutoff
\draw [thick,green, name intersections={of=solvent and unsolvent}]
    (intersection-1 |- {(0,0)}) -- ++(0,2) node[above] {cut off};

\end{tikzpicture}

\end{document}

结果是:

在此处输入图片描述

答案2

MetaPost 版本,包含在 LuaLaTeX 程序中。我使用 MetaPost 的内置功能来查找路径交叉点及其之间的区域,最值得注意的是buildcycle确定该区域周围循环路径的宏。请参阅MetaPost 手册,第 30 页。

\documentclass[12pt, border = 1bp]{standalone}
\usepackage{unicode-math}
\usepackage{luamplib}
    \mplibtextextlabel{enable}
    \mplibsetformat{metafun}
\begin{document}
\begin{mplibcode}

input mpcolornames;

% Drawing a function curve
vardef function(expr xmin, xmax, xstep)(text f_x) =
    save x; x := xmin;
    (x, f_x)
    forever:
        hide(x := x + xstep)
        exitunless x <= xmax;
        .. (x, f_x)
    endfor
    if x - xstep < xmax: hide(x := xmax) .. (x, f_x) fi
enddef;

beginfig(1);

    % Scale and axes: definitions
    numeric u, v, xmin, xmax, ymin, ymax;
    u = cm; v = 2.5cm; xmin = 0; xmax = 3pi+1; ymin = 0; ymax = 2.25;
    path xaxis, yaxis;
    xaxis = origin -- (xmax*u, 0);
    yaxis = origin -- (0, ymax*v);

    % Axes, sin waves, areas: definitions
    numeric t[], xintersect; pair intersect;
    path wave[], midstraight;
    wave1 = (function(-.5pi, 1.5pi, .05)(1 + sin x)) xyscaled (u, v) xshifted .5pi*u;
    wave2 = wave1 xshifted pi*u;
    intersect = wave1 intersectionpoint wave2;
    xintersect = xpart intersect;
    midstraight = (xintersect, -.5v) -- intersect;


    % Drawing sin waves, areas below and relevant labels
    fill buildcycle(xaxis, midstraight, wave1) withcolor .8white;
    fill buildcycle(midstraight, reverse wave2, xaxis) withcolor LightSteelBlue;
    draw wave1; draw wave2; draw midstraight;
    dotlabel.top("$\mu_0$", point 0.5 along wave1);
    label.rt("$\alpha$", point .3 along (wave1 cutbefore wave2));
    dotlabel.top("$\mu_1$", point 0.5 along wave2);
    label.lft("$\beta$", point 0.7 along (wave2 cutafter wave1));

    % Axes drawings and relevant labels
    drawarrow xaxis; drawarrow yaxis;
    label.bot("$x$", (xmax*u, 0));
    label.lft("$p(x)$", (0, ymax*v));
    label.bot("Annahme", point 0 of wave2);
    label.bot("Ablehnung", point infinity of wave1);
    label.bot("$+k_\alpha$", point 0 of midstraight);

    % Comments
    path comment_line;
    comment_line = ((xintersect - 1.25pi*u, 0)-- (xintersect + 1.25pi*u, 0)) 
        yshifted -.3v;
    drawdblarrow comment_line;
    labeloffset := .5v;
    label.bot(btex 
        \begin{tabular}{c} 
            $\beta$-Fehler kleiner,\\ $\alpha$-Fehler gr\"o\ss er
        \end{tabular} etex, point .25 of comment_line);
    label.bot(btex 
        \begin{tabular}{c} $\alpha$-Fehler kleiner,\\ $\beta$-Fehler gr\"o\ss er 
        \end{tabular} etex, point .75 of comment_line);
endfig;

\end{mplibcode}
\end{document}

输出:

在此处输入图片描述

相关内容