# How to fill the limits of the two curves?

I want to fill the limits of the two curves (as shown). This is my code

\documentclass[10pt]{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\definecolor{ttqqcc}{rgb}{0.2,0,0.8}
\definecolor{ffqqtt}{rgb}{1,0,0.2}
\definecolor{ttqqff}{rgb}{0.2,0,1}
\definecolor{uququq}{rgb}{0.25,0.25,0.25}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw[->,color=black] (-3.63,0) -- (3.67,0);
\foreach \x in {-3,-2,-1,1,2,3}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[color=black] (3.48,0.07) node [anchor=south west] { $x$};
\draw[->,color=black] (0,-1.93) -- (0,5.57);
\foreach \y in {-1,1,2,3,4,5}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\draw[color=black] (0.06,5.19) node [anchor=west] { $y$};
\draw[color=black] (0pt,-10pt) node[right] {\footnotesize $0$};
\clip(-3.63,-1.93) rectangle (3.67,5.57);
\draw[color=ttqqff,fill=ttqqff,fill opacity=0.1, smooth,samples=50,domain=-1.7320508075688772:-1.0] plot(\x,{\x^4-4*\x^2+3}) -- (-1,0) -- (-1.73,0) -- cycle;
\draw[color=ffqqtt,fill=ffqqtt,fill opacity=0.1, smooth,samples=50,domain=-1.0:1.0] plot(\x,{\x^4-4*\x^2+3}) -- (1,0) -- (-1,0) -- cycle;
\draw[color=ttqqcc,fill=ttqqcc,fill opacity=0.1, smooth,samples=50,domain=1.0:1.7320508075688772] plot(\x,{\x^4-4*\x^2+3}) -- (1.73,0) -- (1,0) -- cycle;
\draw[smooth,samples=100,domain=-3.6342419080068153:3.665758091993186] plot(\x,{(\x)^4-4*(\x)^2+3});
\begin{scriptsize}
\fill [color=uququq] (-1.73,0) circle (1.5pt);
%\draw[color=uququq] (-1.87,-0.33) node {$A$};
\fill [color=uququq] (-1,0) circle (1.5pt);
%\draw[color=uququq] (-0.85,-0.35) node {$B$};
\fill [color=uququq] (1,0) circle (1.5pt);
%\draw[color=uququq] (1.1,0.25) node {$C$};
\fill [color=uququq] (1.73,0) circle (1.5pt);
%\draw[color=uququq] (1.84,0.25) node {$D$};
\end{scriptsize}
\end{tikzpicture}
\end{document}


-
your code doesn't give the first picture- was it supposed to? – cmhughes Oct 29 '12 at 2:07
My computer out put so. – minthao_2011 Oct 29 '12 at 2:09

The code provided produces this output in my system:

which shows some problem with negative values for the x-coordinate. I decided then to work only for positive values of the x-coordinate and then use a reflection. For the desired patterns, you can use the patterns library:

\documentclass[10pt]{article}
\usepackage{pgf,tikz}
\usetikzlibrary{arrows,patterns}
\pagestyle{empty}

\definecolor{ttqqcc}{rgb}{0.2,0,0.8}
\definecolor{ffqqtt}{rgb}{1,0,0.2}
\definecolor{ttqqff}{rgb}{0.2,0,1}
\definecolor{uququq}{rgb}{0.25,0.25,0.25}

\begin{document}

\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]

\draw[->] (-3.63,0) -- (3.67,0);
\foreach \x in {-3,-2,-1,1,2,3}
\draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt)
node[below] {\footnotesize $\x$};
\draw[color=black] (3.48,0.07) node [anchor=south west] { $x$};

\draw[->] (0,-1.93) -- (0,5.57);
\foreach \y in {-1,1,2,3,4,5}
\draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt)
node[left] {\footnotesize $\y$};
\draw[color=black] (0.06,5.19) node [anchor=west] { $y$};

\draw (0pt,-10pt) node[right] {\footnotesize $0$};

\clip(-3.63,-1.93) rectangle (3.67,5.57);
\draw[pattern color=ffqqtt,pattern=north east lines,fill opacity=0.1, smooth,samples=50,domain=0:1.0]
plot(\x,{\x^4-4*\x^2+3}) -- (1,0) -- (0,0) -- cycle;
\draw[pattern color=ttqqcc,pattern=crosshatch,fill opacity=0.1, smooth,samples=50,domain=1.0:1.7320508075688772]
plot(\x,{\x^4-4*\x^2+3}) -- (1.73,0) -- (1,0) -- cycle;

\begin{scope}[xscale=-1]
\draw[pattern color=ffqqtt,pattern=north east lines,fill opacity=0.1, smooth,samples=50,domain=0:1.0]
plot(\x,{\x^4-4*\x^2+3}) -- (1,0) -- (0,0) -- cycle;
\draw[pattern color=ttqqcc,pattern=crosshatch,fill opacity=0.1, smooth,samples=50,domain=1.0:1.7320508075688772]
plot(\x,{\x^4-4*\x^2+3}) -- (1.73,0) -- (1,0) -- cycle;
\end{scope}

\draw[smooth,samples=100,domain=-3.6342419080068153:3.665758091993186] plot(\x,{(\x)^4-4*(\x)^2+3});

\fill [color=uququq] (-1.73,0) circle (1.5pt);

\fill [color=uququq] (-1,0) circle (1.5pt);

\fill [color=uququq] (1,0) circle (1.5pt);

\fill [color=uququq] (1.73,0) circle (1.5pt);

\end{tikzpicture}
\end{document}


For this kind of plots, the pgfplots package seems a more natural choice:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
xmin=-3,xmax=3,
ymin=-1.5,ymax=5,
axis on top]

domain=-1:1,
samples=100,
pattern=north east lines,
pattern color=red]{(\x)^4-4*(\x)^2+3} \closedcycle;

domain=1:1.7320508075688772,
samples=100,
pattern=crosshatch,
draw=red,
pattern color=blue!60]{(\x)^4-4*(\x)^2+3} \closedcycle;

domain=-1.7320508075688772:-1,
samples=100,
pattern=crosshatch,
draw=red,
pattern color=blue!60]{(\x)^4-4*(\x)^2+3} \closedcycle;
\end{axis}
\end{tikzpicture}

\end{document}


-
both your solution and mine no longer seem to work :) ..... no idea what to do :) – cmhughes May 26 '14 at 3:51
@cmhughes Using the LaTeX system (TeX Live2013, but not cutting edge updated) at my office they still work; I'll do some tests at home later with my most updated installation. – Gonzalo Medina May 26 '14 at 16:37

I'd do this using pgfplots as follows, note the use of styles

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{patterns}

% set style for the axis
\pgfplotsset{mystyle/.style={
axis x line=middle,
axis y line=middle,
axis on top,
}}

% set styles for the patterns
\pgfplotsset{first pattern/.style={pattern=crosshatch,pattern color=blue}}
\pgfplotsset{second pattern/.style={pattern=north east lines, pattern color=red}}

\begin{document}
\begin{tikzpicture}[/pgf/declare function={f=x^4-4*x^2+3;}]
\begin{axis}[mystyle,
xmin=-4,xmax=4,
ymin=-2,ymax=6]
\end{axis}
\end{tikzpicture}
\end{document}

-
Instead of -1.7 and 1.7 you need more accurate values (for example -1.7320508075688772 and 1.7320508075688772); otherwise there will be some "imperfections" (visible when zooming in) near (-1.7,0) and (1.7,0). – Gonzalo Medina Oct 29 '12 at 3:49
@GonzaloMedina I think that's 5 or 6 decimals are enough because I'm not sure that TeX works with more decimals. – Alain Matthes Oct 29 '12 at 8:55
@GonzaloMedina thanks, updated – cmhughes Oct 29 '12 at 15:13

Just 4 fun with PSTricks!

\documentclass[pstricks,border=5pt]{standalone}
\usepackage{pst-plot,pst-eucl}
\psset{saveNodeCoors}
\def\f{x 2 exp dup 4 sub mul 3 add}

\begin{document}
\begin{pspicture}(-2.5,-1)(2.5,4)
\psset{PointName=none,PointSymbol=none}
% Determine the intersection points
\pstInterFF{\f}{0}{-2}{A}
\pstInterFF{\f}{0}{-1}{B}
\pstInterFF{\f}{0}{1}{C}
\pstInterFF{\f}{0}{2}{D}
% Fill the bounded regions
\pscustom[fillstyle=solid,fillcolor=yellow!50]{\psplot{N-A.x}{N-D.x}{\f}\psline(!N-D.x 0)(!N-A.x 0)}
% Plot the curve
\psplot[plotpoints=100]{-2}{2}{\f}
% Draw the coordinate axes
\psaxes{->}(0,0)(-2.5,-1)(2.5,4)[$x$,-90][$y$,180]
% Draw the intersection points
\psdots(A)(B)(C)(D)
\end{pspicture}
\end{document}


## Explanations:

• \psset{saveNodeCoors} is needed to allow us to use the node's x and y values out of the box. For example, if a node A has been defined then we can refer to its x value by invoking N-A.x. The prefix N- is mandatory even though it adds a bit complexity (at least for me)!
• \def\f{x 2 exp dup 4 sub mul 3 add} defines the function in Reverse Polish Notation (RPN). x 2 exp dup 4 sub mul 3 add represents x^4-4x^2+3. The expression uses stack operations that are a bit complicated for untrained human beings. However, you can learn it in several minutes to grasp the essence by googling "RPN".
• \psset{PointName=none,PointSymbol=none} is used to disable point labels and point symbols that are generated automatically by pst-eucl whenever you specify points (aka nodes).
• \pstInterFF{<function 1 in RPN>}{<function 2 in RPN>}{<starting point>}{<intersection point name>} will determine the intersection point between 2 functions (in RPN) near the starting point you specified.
• \pscustom[fillstyle=solid,fillcolor=yellow!50]{\psplot{N-A.x}{N-D.x}{\f}\psline(!N-D.x 0)(!N-A.x 0)} fill the bounded regions by the curve and the x axis.
• the remaining codes are self-explanatory.

## Update

With infix form.

\documentclass[pstricks,border=5pt]{standalone}
\usepackage{pst-plot,pst-eucl}

\def\f{(x^2*(x^2-4)+3)}
\pstVerb{/I2P {AlgParser cvx exec} def}

\begin{document}
\begin{pspicture}[saveNodeCoors,algebraic,PointName=none,PointSymbol=none](-2.5,-1)(2.5,4)
% Determine the intersection points
\pstInterFF{\f I2P}{0}{-2}{A}
\pstInterFF{\f I2P}{0}{-1}{B}
\pstInterFF{\f I2P}{0}{1}{C}
\pstInterFF{\f I2P}{0}{2}{D}
% Fill the bounded regions
\pscustom[fillstyle=solid,fillcolor=yellow!50]{\psplot{N-A.x}{N-D.x}{\f}\psline(!N-D.x 0)(!N-A.x 0)}
% Plot the curve
\psplot[plotpoints=100]{-2}{2}{\f}
% Draw the coordinate axes
\psaxes{->}(0,0)(-2.5,-1)(2.5,4)[$x$,-90][$y$,180]
% Draw the intersection points
\psdots(A)(B)(C)(D)
\end{pspicture}
\end{document}

-

A short and simple solution with tkz-fct

\documentclass[10pt]{article}
\usepackage{tkz-fct}
\usetikzlibrary{arrows,patterns}
\pagestyle{empty}

\begin{document}

\begin{tikzpicture}[scale=1.5]
\tkzInit[xmin=-3,xmax=3,ymin=-2,ymax=4]
\tkzGrid
\tkzAxeXY
\tkzFct[domain = -2.2:2.2]{x**4-4*x**2+3}
\tkzDrawArea[pattern=north west lines, pattern color=red!60,domain = -1:1]
\tkzDrawArea[pattern=north west lines, pattern color=blue!60,domain = 1.0:1.7320]
\tkzDrawArea[pattern=north west lines, pattern color=blue!60,domain = -1.7320:-1]
\tkzDefPointByFct[draw,ref=A](1)
\tkzLabelPoint[above right](A){$x_1$}
\tkzDefPointByFct[draw,ref=B](1.732)
\tkzLabelPoint[above right](B){$x_2$}
\end{tikzpicture}

\end{document}


-

Another pgfplots solution, but using recent (pgfplots v1.10) fillbetween library.

Next code shows one possible solution. There are some comments into it but it's worth to read section 5.6 Fill between from pgfplots documentation.

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{patterns}
\usepgfplotslibrary{fillbetween}

% set style for the axis
\pgfplotsset{mystyle/.style={
axis x line=middle,
axis y line=middle,
axis on top,blue
}}

% set styles for the patterns
\pgfplotsset{first pattern/.style={pattern=crosshatch,pattern color=blue}}
\pgfplotsset{second pattern/.style={pattern=north east lines, pattern color=red}}

\begin{document}
\begin{tikzpicture}[/pgf/declare function={f=x^4-4*x^2+3;}]
\begin{axis}[mystyle,
xmin=-4,xmax=4,
ymin=-2,ymax=6]

%Function f is plotted and named A.
%This name will be used to plot areas between paths.

%Second path (horizontal axis) is named B.
%It's just defined, not drawn
\path[name path=B](axis cs:-3,0)--(axis cs:3,0);

% Area between path A and B within domain -2:2 is plotted.
% The area is segmented (split option).
% It's possible to define special styles for every segment.
% This area consists in five segments (numbers 0 to 4).
% Only segments 1, 2 and 3 are filled.