Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I wanted to graph the function f(x)=(-1+4x^2)*e^2 in a coordinate system. Using "Export Graphics View as pgf/TikZ" in Geogebra I got the following code (here shown after I did some minor changes regarding color and arrow style).

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
[scale=2,line cap=round,line join=round,x=1.0cm,y=1.0cm]
\draw [color=black!70,dash pattern=on 1pt off 1pt, xstep=0.5cm,ystep=0.5cm] (-1.45,-1.28) grid (1.45,2.48);
\draw[->,line width=1,color=black] (-1.45,0) -- (1.45,0);
\foreach \x in {-1,1}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[color=black] (1.31,0.04) node [anchor=south west] { x};
\draw[->,line width=1,color=black] (0,-1.28) -- (0,2.48);
\foreach \y in {-1,1,2}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\draw[color=black] (0.05,2.28) node [anchor=west] { y};
\clip(-1.45,-1.28) rectangle (1.45,2.48);
\draw[line width=1,color=black!80,fill=black!80,fill opacity=0.6, smooth,samples=50,domain=0.0:0.5] plot(\x,{(4*\x^2-1)*2.7183^\x}) -- (0.5,0) -- (0,0) -- cycle;
\draw[line width=1,smooth,samples=100,domain=-1.5:1.5] plot(\x,{(-1+4*\x^2)*2.7183^\x});
\end{tikzpicture}
\end{document}

The code above gave me this result:

The way the graph looks before adding code to preamble

I tried installing TikZ CSV from this page following steps given here but this only flooded the log in "Undefined control sequence" messages.

Instead I tried using Jake's answer code to improve the exp-function. Then the result looked a bit better but still not correct:

The way the graph looks after adding Jake's code to preamble

Here is how the original graph is shown in Geogebra (Wolfram Alpha shows similar results):

The way the graph looks in Geogebra

Obviously the exp-code is not helping enough, so maybe there still is a bug in that? Can someone help me with this? Would it be possible to plot the graph correctly in some other way? If so, what would that code look like?

share|improve this question
    
Welcome to the site! –  cmhughes Oct 14 '13 at 18:37
1  
It seems the problem is the x^2 (which I'm sure was addressed ages ago). Using -1+4*(\x)^2))*2.7813^\x appears to work. –  Mark Wibrow Oct 14 '13 at 18:46
1  
Just to add to my previous comment, with TikZ plots each y-value is expanded for every value of \x so you get (-1+4*-1.5^2)*2.7183^-1.5 which is then evaluated as -2.23 (this result is the same as python and matlab). This means the \x^2 must be given as (\x)^2. PGFPlots (like numpy and matlab) does extra stuff to ensure that when x is (effectively) a vector then x^2 is equivalent to (x)^2. –  Mark Wibrow Oct 15 '13 at 7:12
    
Thank you very much! This both explained why the results were not correct (Marks answer) and how it could be done way better with pgfplots (cmhughes answer) –  Bea Oct 16 '13 at 16:43

1 Answer 1

up vote 8 down vote accepted

GeoGebra is a wonderful tool for creating interactive tools for students, and while the code it produces using its export feature is pretty impressive, it can usually not beat a hand-made solution, especially for readability and cleanness.

Here's a hand-made solution using pgfplots

screenshot

% arara: pdflatex
% !arara: indent: {overwrite: true, trace: on}
\documentclass{standalone}

\usepackage{pgfplots}

% axis style
\pgfplotsset{every axis/.append style={
              axis x line=middle,
              axis y line=middle,
              axis line style={<->},
              xlabel={$x$},
              ylabel={$y$}, 
              },
              framed/.style={axis background/.style ={draw=black}},
                    }

% arrow style
\tikzset{>=stealth}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[framed,
            xmin=-5,xmax=5,
            ymin=-5,ymax=5,
            minor xtick={-3,-1,...,3},
            minor ytick={-3,-1,...,3},
            grid=both
        ]
        \addplot[-] expression[domain=-2.3:2.3,samples=50]{(-1+4*x^2)*exp(x)};
        \addplot[fill] expression[domain=0:0.5]{(-1+4*x^2)*exp(x)}\closedcycle;
    \end{axis}
\end{tikzpicture}


\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.