0

I want to calculate resursive terms of a sequence. The following code do this task with fibonacci sequence (sure, could be other sequence).

\documentclass[varwidth,border=5]{standalone}
\usepackage{tikz}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}
\usetikzlibrary{math}

\begin{document}
  \tikzset{fixed point arithmetic}
  \tikzmath{
    function printfib(\i,\f){print {$f_{\i} = \f$\newline};};
    function fibonacci(\n) {
      int \a, \b, \res;
      \a = 0; \b = 1;
      if \n == 0 then { \res = \a; };
      if \n == 1 then { \res = \b; };
      if \n > 1 then {
        for \i in {2,...,\n}{
          \res = \a + \b;
          \a = \b;
          \b = \res;
        };
      };
      return \res;
    };
    int \f, \i;
    for \i in {0,1,...,10}{
      printfib(\i,fibonacci(\i));
    };
  }


\end{document}

However, I would like to print this results into a table, with two columns. The first one are the n values and the second ones the nth term of my sequence. Anyone could help me please to insert instructions to do this? In other words, I provide the value for n and I would like to obtain a table with the n values for my sequence. Thank you very much!

0

You can use tikzmath to define macros. Such macros, as \fib in the example below, can be used to fill a table. This is a very basic table, you can make it more fancy, and you could use pgfplotstable to fill it.

\documentclass[varwidth,border=5]{standalone}
\usepackage{etoolbox}
\usepackage{tikz}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}
\usetikzlibrary{math}

\begin{document}
  \tikzset{fixed point arithmetic}
  \tikzmath{
    function printfib(\i,\f){print {$f_{\i} = \f$\newline};};
    function fibonacci(\n) {
      int \a, \b, \res;
      \a = 0; \b = 1;
      if \n == 0 then { \res = \a; };
      if \n == 1 then { \res = \b; };
      if \n > 1 then {
        for \i in {2,...,\n}{
          \res = \a + \b;
          \a = \b;
          \b = \res;
        };
      };
      return \res;
    };
    int \f, \i;
    for \i in {0,1,...,10}{
      \fib{\i}=fibonacci(\i);
    };
  }

\def\tmp{$n$ & $f(n)$\\ $0$ & $0$\\}%
\edef\iloop{0}%
\loop
\edef\iloop{\the\numexpr\iloop+1}%
\xappto\tmp{$\iloop$ & $\fib{\iloop}$}%
\gappto\tmp{\\}%
\ifnum\iloop<10\repeat

\begin{tabular}{cc}
\tmp
\end{tabular}

\end{document}
2
  • Perfect! Thank you so much! Other question: If I want to plot each point (i, fibonacci(i)), how can I do this? – Angelo Aliano Filho Oct 31 '20 at 18:05
  • @AngeloAlianoFilho A very basic option: \begin{tikzpicture} \draw[stealth-stealth] (0,6) node[left]{$f$} |- (10.5,0) node[below]{$n$}; \path foreach \x in {1,...,5} {(-0.1,\x) node[left]{$\x0$} edge ++ (0.2,0)} foreach \x in {1,...,10} {(\x,-0.1) node[below]{$\x$} edge ++ (0,0.2)}; \draw[only marks,mark=*] plot[samples at={1,...,10}] (\x,0.1*\fib{\x}); \end{tikzpicture}. (Unfortunately on this site one cannot preserve line breaks in comments. For people from certain countries it is also hard to upload screen shots...) Of course there are fancier ways, e.g. pgfplots. – user227987 Oct 31 '20 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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