7

I was asked to find the integral between x = -4 and x = 4 of the intersection of y_1 = x^2 and y_2 = -x^2. So I recreated it using LaTeX. Please consider the following MWE:

\documentclass{article}
\usepackage[spanish]{babel}
\selectlanguage{spanish}
\decimalpoint
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{pgfplots}
\usetikzlibrary{babel,arrows.meta, patterns}
\pgfplotsset{compat=1.8}    
\usepackage{mathtools}

\begin{document}

\begin{center}
\begin{tikzpicture}[scale=1.5]
        \begin{axis}[
            legend pos=outer north east,
            axis lines = center,
            clip=false,
            label style={font=\tiny},
            legend style={font=\tiny},
            xticklabel style = {font=\tiny},
            yticklabel style = {font=\tiny},
            xlabel = $x$,
            ylabel = $y$,
            xmin=-4.5,
            ymin=-16.5,
            xmax=4.5,
            ymax=20.5,
            xtick={-4,4},
            ytick={-16,16}
        ]
        \addplot[thick,red,samples=60,restrict y to domain=0:16] {x*x} node[below right] {\tiny $y_1=x^2$};     % x^2
        \addplot[thick,red,samples=60,restrict y to domain=-16:0] {-x*x} node[above right] {\tiny $y_2=-x^2$};  % -x^2
        \addplot[thick,red,samples=60,domain=0:4,restrict y to domain=0:16,fill=gray!60] {x*x}\closedcycle;       % Area in 1º C
    \end{axis}
\end{tikzpicture}
\end{center}

\end{document}

Area

I appeal to your creativity to complete the way I did for calculating the integral: integrating from x = 0 to x = 4 and the result multiplied by 4. I would like to highlight in some way the other three sections, each one in a different quadrant of plane.

Can you think of a nice way? I am not sure about this:

Doubts

I also realize that for each section I have to add an addplot command because the parabolas are already drawn, and it is a bit redundant. Is there any easy way to simplify the code?

And finally I do not know why the line above 16 is longer than the line below -16:

yAxisProblem

If you could fix that, it would also be fantastic! EDIT: I could solve it by changing ymin value to ymin=-17.5 and ymax to ymax=17.5.

I hope good answers! :).

2 Answers 2

8

Here is a proposal. It uses the flexible hatch pattern from Pgfplots: how to fill the area under a curve with oblique lines (hatching) as a pattern?

enter image description here

\documentclass{article}
\usepackage[spanish]{babel}
\selectlanguage{spanish}
\decimalpoint
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{pgfplots}
\usetikzlibrary{babel,arrows.meta, patterns}
\pgfplotsset{compat=1.8}    
\usepackage{mathtools}


   \tikzset{
        hatch distance/.store in=\hatchdistance,
        hatch distance=15pt,
        hatch thickness/.store in=\hatchthickness,
        hatch thickness=2pt
    }
    \makeatletter
    \pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
    {\pgfqpoint{0pt}{0pt}}
    {\pgfqpoint{\hatchdistance}{\hatchdistance}}
    {\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
    {
        \pgfsetcolor{\tikz@pattern@color}
        \pgfsetlinewidth{\hatchthickness}
        \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
        \pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
        \pgfusepath{stroke}
    }
    \makeatother


\begin{document}

\begin{center}
\begin{tikzpicture}[scale=1.5]
        \begin{axis}[
            legend pos=outer north east,
            axis lines = center,
            clip=false,
            label style={font=\tiny},
            legend style={font=\tiny},
            xticklabel style = {font=\tiny},
            yticklabel style = {font=\tiny},
            xlabel = $x$,
            ylabel = $y$,
            xmin=-4.5,
            ymin=-17.5,
            xmax=4.5,
            ymax=17.5, %<--- ymax was bigger than ymin
            xtick={-4,4},
            ytick={-16,16}
        ]
        \addplot[draw=none,samples=60,domain=0:4,restrict y to domain=0:16,fill=orange!60] {x*x}\closedcycle;       % Area in 1º C
        \addplot[draw=none,samples=60,domain=0:4,pattern=flexible hatch,pattern color=gray] {-x*x}\closedcycle;       
              \begin{scope}[xscale=-1]
                  \addplot[draw=none,red,samples=60,domain=0:-4,pattern=flexible hatch,pattern color=gray] {-x*x}\closedcycle; 
                  \addplot[draw=none,samples=60,domain=0:-4,restrict y to domain=0:16,pattern=flexible hatch,pattern color=gray] {x*x}\closedcycle;      
              \end{scope}
        \addplot[thick,red,samples=60,restrict y to domain=0:16] {x*x} node[below right] {\tiny $y_1=x^2$};     % x^2
        \addplot[thick,red,samples=60,restrict y to domain=-16:0] {-x*x} node[above right] {\tiny $y_2=-x^2$};  % -x^2
    \end{axis}
\end{tikzpicture}
\end{center}

\end{document}
4
  • Thank you! I was hesitant to add the red edges because it seems that the function has the form of two parabolas with a two flat tops of equations x = -4 and x = 4 and it is not like that. I would like to delete it. I also painted in gray but it can be another more vivid color (I can not think of another one)! If possible, also keep the numbers above the drawing too, so that they can be seen well.
    – manooooh
    Aug 12, 2018 at 1:22
  • 1
    @manooooh How about orange? ;) Added the changes you requested.
    – Milo
    Aug 12, 2018 at 1:27
  • Much better! How about removing the red borders of the orange part?
    – manooooh
    Aug 12, 2018 at 1:30
  • 1
    Wah... I forgot the draw=none option...
    – manooooh
    Aug 12, 2018 at 1:36
8

Some additional ideas (but Milo if you want to take them I'll be happy to delete this afterwards). I double the version of pgfplots, use domain instead of the restriction of the y coordinates, add use the pgfplots library fillbetween. The hatched lines are admittedly much worse than what Milo has to offer. I also add enlargelimits=lower, to make the y axis more symmetric.

\documentclass{article}
\usepackage[spanish]{babel}
\selectlanguage{spanish}
\decimalpoint
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{pgfplots}
\usetikzlibrary{babel,arrows.meta, patterns}
\pgfplotsset{compat=1.16}  
\usepgfplotslibrary{fillbetween}  
\usepackage{mathtools}

\begin{document}

\begin{center}
\begin{tikzpicture}[scale=1.5]
        \begin{axis}[
            legend pos=outer north east,
            axis lines = center,
            clip=false,
            label style={font=\tiny},
            legend style={font=\tiny},
            xticklabel style = {font=\tiny},
            yticklabel style = {font=\tiny},
            xlabel = $x$,
            ylabel = $y$,
            xmin=-4.5,
            ymin=-16.5,
            xmax=4.5,
            ymax=20.5,
            xtick={-4,4},
            ytick={-16,16},
            domain=-4:4,
            enlargelimits=lower,
        ]
        \addplot[name path=A,thick,red,samples=60] {x*x} node[below right] {\tiny $y_1=x^2$};     % x^2
        \addplot[name path=B,thick,red,samples=60] {-x*x} node[above right] {\tiny $y_2=-x^2$};  % -x^2
        \addplot+[pattern=north east lines] fill between[of=A and B,soft clip={domain=-16:16}];
        \addplot[name path=x-axis,draw=none] {0};  % -x^2
        \addplot+[fill=gray!60]  fill between[of=x-axis and B,soft clip={domain=0:4}];       % Area in 1º C
        \draw[thick,red] (4,16) -- (4,-16) (-4,16) -- (-4,-16);
    \end{axis}
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

And with \addplot+[fill=gray!60] fill between[of=x-axis and A,soft clip={domain=0:4}]; instead:

enter image description here

1
  • +1 dear @marmot.
    – manooooh
    Aug 12, 2018 at 1:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .