How can I make a graph of a function?

I'm trying to implement a function to LaTeX but i don't know how. I'm compiling using this page: docs.latexlab.org

-

Compile the following code with xelatex or a combo sequence latex-dvips-ps2pdf.

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

\begin{document}
\begin{pspicture}(-4.25,-1.25)(4.25,2.25)
\def\f(#1){sin(2*#1)+0.5}
\psaxes[labelFontSize=\scriptscriptstyle,ticksize=-2pt 2pt]{->}(0,0)(-4,-1)(4,2)[$x$,0][$y$,90]
\psplot[linecolor=blue,algebraic]{-\psPi}{\psPi}{\f(x)}
\rput[tl](*1 {\f(x)+0.5}){$y=\sin(2x)+\frac{1}{2}$}
\end{pspicture}
\end{document}


Explanation

1. Making diagrams (including function plotting) can be accomplished by using PSTricks (recommended because it is faster and easier to learn yet powerful) or TikZ or others. The code above is written in PSTricks, you need to load \usepackage{pst-plot}.
2. To get a tight page, use

\documentclass[pstricks,border=12pt]{standalone}

3. Define a canvas on which you draw.

\begin{pspicture}(-4.25,-1.25)(4.25,2.25)
... drawing codes go here ...
\end{pspicture}


(-4.25,-1.25) represents the bottom left point of your canvas and (4.25,2.25) is the top right point.

4. Define the function to plot.

\def\f(#1){sin(2*#1)+0.5}


In this example I chose y=sin(2x)+1/2.

5. Draw the coordinate axes.

\psaxes[labelFontSize=\scriptscriptstyle,ticksize=-2pt 2pt]{->}(0,0)(-4,-1)(4,2)[$x$,0][$y$,90]

6. Plot the graph.

\psplot[linecolor=blue,algebraic]{-\psPi}{\psPi}{\f(x)}

7. Put a label if necessary.

 \rput[tl](*1 {\f(x)+0.5}){$y=\sin(2x)+\frac{1}{2}$}


In PSTricks, we can specify a point in several ways. (*<x-value> {the value of expression in x for the given x}) is one of them. Thus (*1 {\f(x)+0.5}) mathematically means a point (x,y) where x=1 and y=f(1)+0.5.

8. Compile the input file with xelatex or the combo sequence latex-dvips-ps2pdf.

9. Done!

Miscellaneous

\psplot[linecolor=blue,algebraic,plotpoints=100]{Pi neg .5 sub}{Pi .5 add}{\f(x)}

• The plotpoints can be used to increase the number of points used to plot the graph. But be wise, the greater its value is, the smoother the plot is but the bigger the file size becomes. For most extreme case, plotpoints=1000 should be more than enough.

• The first and second arguments of \psplot can accept RPN notation. In the example above, I used -π-.5 and π+.5 for both args, respectively. PSTricks gives you many good features, right?

-
+1 if there are going to be a lot of plots, I'd put \psset{algebraic=true} in the preamble, which saves you from specifying algebraic every time. You could make the curve a little smoother by using, for example, plotpoints=100 – cmhughes Mar 11 '12 at 16:15

Here's an option using pgfplots, which can be compiled with pdflatex

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[>=stealth]
\begin{axis}[
xmin=-4,xmax=4,
ymin=-2,ymax=2,
axis x line=middle,
axis y line=middle,
axis line style=<->,
xlabel={$x$},
ylabel={$y$},
]
node[pos=0.65,anchor=south west]{$y=\sin(2x)+\frac{1}{2}$};
\end{axis}
\end{tikzpicture}
\end{document}


One of the main reasons I like this package, is that global styles can be specified easily in the preamble. So, if you're going to have more than one of these plots, it might be a good idea to use something like the following setup

\documentclass{article}
\usepackage{pgfplots}

% axis style
\pgfplotsset{every axis/.append style={
axis x line=middle,    % put the x axis in the middle
axis y line=middle,    % put the y axis in the middle
axis line style={<->}, % arrows on the axis
xlabel={$x$},          % default put x on x-axis
ylabel={$y$},          % default put y on y-axis
}}

% line style
\pgfplotsset{mystyle/.style={color=blue,no marks,line width=1pt,<->}}

% arrow style: stealth stands for 'stealth fighter'
\tikzset{>=stealth}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-4,xmax=4,
ymin=-2,ymax=2,
]
node[pos=0.65,anchor=south west]{$y=\sin(2x)+\frac{1}{2}$};

The keys xlabel and ylabel seem to be inconsistent when considering other keys. They should be x label and y label. What do you think? There must be a space between x/y and label. The same inconsistent title is also given to xmax, xmin, etc. – kiss my armpit Oct 31 '13 at 8:15
axis x line and xlabel seem to be inconsistent. If I were the author, I would define x label rather than xlabel. It is my personal opinion. I may be wrong. :-) I will discuss it someday. – kiss my armpit Oct 31 '13 at 15:30
Does it work with XeLaTeX as well? – polemon May 29 '14 at 12:20