Tell me more ×
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'm trying to implement a function to LaTeX but i don't know how. I'm compiling using this page: docs.latexlab.org

share|improve this question

2 Answers

up vote 9 down vote accepted

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

enter image description here

\documentclass{article}
\usepackage{pstricks-add}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}
\PreviewBorder=10pt


\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{pstricks-add}.
  2. To get a tight page, use

    \usepackage[active,tightpage]{preview}
    \PreviewEnvironment{pspicture}
    \PreviewBorder=10pt
    
  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!

share|improve this answer
+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

enter image description here

\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$},
        ]
        \addplot[no marks,blue,<->] expression[domain=-pi:pi,samples=100]{sin(deg(2*x))+1/2} 
                    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,
        ]
        \addplot[mystyle] expression[domain=-pi:pi,samples=100]{sin(deg(2*x))+1/2} 
                    node[pos=0.65,anchor=south west]{$y=\sin(2x)+\frac{1}{2}$}; 
    \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.