# How to fill a rectangle with two corners on the function

I would like to be able to apply the same thing in the following figure for any function:

Right now my code is like this(which is an edited version of some post here in Tex.sx): Wich only fills under the curve completely!

\documentclass[11pt,bezier]{article}
\usepackage{xstring}
\usepackage{collcell}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\begin{document}
\begin{figure}[ht!]
\centering
\begin{tikzpicture}
\begin{axis}[xmin=-1,xmax=9,xlabel={Y},ymin=0,ymax=10]
{(-(x-3)^2+(x-3)^4-(x-1)^3+4)/40+5};
{(-(x-3)^2+(x-3)^4-(x-1)^3+4)/40+5} \closedcycle;
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

-
I prefer to have boundaries around the rectangle. Thanks again. – Naji Apr 6 '13 at 0:47
While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. – Peter Grill Apr 6 '13 at 0:53
@PeterGrill, I did but I don't know what I have to do to be able to make it run! – Naji Apr 6 '13 at 0:58
You need to add \usepackage{pgfplots} and eliminate the \$ signs. – Peter Grill Apr 6 '13 at 1:02

You can use relative coordinates and --cycle (instead of \closedcycle) to get:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-1,xmax=9,xlabel={Y},ymin=0,ymax=10]