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 try to plot a piecewise linear interpolation of a function w on a grid \xn={x1,x2,...,xN} using tikz. I tried using a foreach loop and store the grid as a vector to do that, but it doesn't seem to work. Can anyone help?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, calc}
\begin{document}
\begin{tikzpicture}[scale=1.8] 
\pgfkeys{ /pgf/declare function={
   w(\x)=-.5+\x*\x*\x-\x+\x*\x;} 
}
\def \xn {-1.8,-1,-.5,.1,.9,1}; %% grid
\foreach \i in {1,2,...,5}{
   \draw({\xn(\i)},{w(\xn(\i))})--({\xn({\i+1})},{w(\xn({\i+1}))});
}
\end{tikzpicture}
\end{document}

This is how the output should look like (without the axes).

enter image description here

share|improve this question
    
Welcome to TeX.SE. –  Peter Grill Apr 28 at 20:57

2 Answers 2

up vote 5 down vote accepted

One way is to directly access the \xn array instead of indexing into it:

enter image description here

Notes:

  • I would recommend getting used to using (\x) instead of \x in function computations as it can sometimes make a difference.

Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, calc}
\begin{document}
\begin{tikzpicture}[scale=1.8] 
\pgfkeys{ /pgf/declare function={
   w(\x)=-.5+(\x)*(\x)*(\x)-(\x)+(\x)*(\x);} 
}
\def \xn {-1.8,-1,-.5,.1,.9,1}; %% grid
\draw [ultra thick, blue] (-1.8,{w(-1.8)})
    \foreach \i in \xn {
       --(\i,{w(\i)})
    };
\end{tikzpicture}
\end{document}
share|improve this answer
    
Thank you very much. –  Gregor de Cillia Apr 28 at 21:04

Also pgfplots can help.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,
          ytick=\empty,xtick=\empty,
          ylabel=$I(w)x$,xlabel=$x$]
\addplot[samples at={-1.8,-1,-.5,.1,.9,1}] {.5+x^3-x+x^2};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
Thank you but I have compiling issues for this code: ! Package pgfkeys Error: I do not know the key '/tikz/axis lines' and I am goin g to ignore it. Perhaps you misspelled it. –  Gregor de Cillia Apr 28 at 21:33
    
@GregordeCillia Is that the first error you get? Maybe you don't have pgfplots installed. –  percusse Apr 28 at 21:34
    
I get 2 errors: /pfgplots/compat and /tikz/axis lines don't seem to work. This is what i get link –  Gregor de Cillia Apr 28 at 21:48
    
@GregordeCillia Your pgfplots version is probably old. Change the line to \pgfplotsset{compat=newest} –  percusse Apr 28 at 21:52
    
I get similar errors and the same outputs when I change line 3: pgfkeys: I do not know the key '/pgfplots/compat' and I am going to ignore it. Perhaps you misspelled it. \pgfplotsset{compat=newest} –  Gregor de Cillia Apr 28 at 22:06

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.