自动生成数学函数值表

自动生成数学函数值表

有没有简单的方法可以获取数学函数的值表?

考虑以下示例和伪代码:

\documentclass{article}

\usepackage{pgfplots}

\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\usepackage{xstring}% http://ctan.org/pkg/xstring
\usepackage{xparse}% http://ctan.org/pkg/xparse

\usepackage[locale=DE]{siunitx}

\begin{document}

\def\f(#1){#1^2} %%It should also be possible to use gnuplots x**2 syntax for the value table

\sisetup{zero-decimal-to-integer}
\def\pf(#1){\pgfmathparse{\f(#1)}\num{\pgfmathresult}}

\begin{center}
  \begin{tikzpicture}
    \begin{axis}[domain=-2:10]
      \addplot+[no markers] gnuplot {\f(x)};
      \addplot+[mark=x,samples at={1,...,10}] gnuplot {\f(x)};
    \end{axis}
  \end{tikzpicture}
\end{center}

Then I want a value table, something like generated automatically.

%% \gen_value_table[samples at={1,...,10},orientation=horizontal]{\f}

\begin{tabular}{*{10}{c|}c}
 $x$&   1      & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10  \\\hline
  $f(x) $ & \pf(1) & \pf(2)  & \pf(3)  & \pf(4)  &  \pf(5) & \pf(6)  &\pf(7)   & \pf(8)  & \pf(9)  & \pf(10)
\end{tabular}

%% \gen_value_table[samples at={1,2,3.5,10},orientation=horizontal]{\f}


\begin{tabular}{*{4}{c|}c}
  $x$ & 1      & 2 & 3.5  & 10  \\\hline
  $f(x)$ & \pf(1) & \pf(2)  & \pf(3.5)  & \pf(10)
\end{tabular}


%% \gen_value_table[samples at={1,2,3.5,10},orientation=]{\f}

\begin{tabular}{c|c}
  $x$ & $f(x)$ \\\hline
  1 & \pf(1) \\
  2 & \pf(2) \\
  3,5 & \pf(3.5) \\
  10 & \pf(10)
\end{tabular}

\end{document}

答案1

正如 percusse 和 cmhughes 在他们的评论中指出的那样,你可以尝试使用该pgfplotstable包来满足你的需求。这里有一些代码作为起点,我认为它对于垂直表格来说效果很好,但对于水平表格来说效果不佳。虽然解决方案并不完美,但希望它是一个很好的起点,值得尝试。

请注意,您不能将密钥samples at与 一起使用\addplot gnuplot。这始终使用domainsamples来生成值。

\documentclass[
    ngerman,
    english,    % <-- comment me to use German language
    DIV=12,
        ]{scrartcl}
\usepackage{babel}
\usepackage{caption}
\usepackage{booktabs}
\usepackage{siunitx}
    \sisetup{
        % just to show where the `\num' command is used
        color=green,
    }
    % this adds the German and UK number format styles to the `siunitx'
    % as well as to the tikz/pgf settings and uses them according to
    % the actual active language
    \addto\extrasngerman{\sisetup{locale = DE}\SendSettingsToPgf}
    \addto\extrasenglish{\sisetup{locale = UK}\SendSettingsToPgf}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
    % define maximum number of entries in the table
    \def\MaxItems{13}
    % define which samples should be used (in the plot and table)
    \def\Samples{1,1.5,2,3,...,10}

    % define stuff that should be used all over the document
    \pgfplotstableset{
        % to show when there are defined too much cells
        empty cells with={NaN},
        % -----
        % define how the `new' column shall be filled:
        create on use/x/.style={
%            % unfortunately using `\Samples' here causes an error;
%            % don't know why
%            create col/set list=\Samples
            % so the list entries have to be given here explicitly
            create col/set list={1,1.5,2,3,...,10}
        },
        % create columns that uses the function you need the values for
        create on use/x square/.style={
            % this fills cells with `0' (zero), when `\MaxItems' is
            % larger than the `set list' (or `\Samples')
            create col/expr={\thisrow{x}^2}
        },
        create on use/minus x cube/.style={
            create col/expr={-1*\thisrow{x}^3}
        },
        % -----
        % define headers for the corresponding columns
        columns/x/.style={
            column name=$x$,
            dec sep align,
        },
        columns/x square/.style={
            column name=$x^2$,
        },
        columns/minus x cube/.style={
            column name=$-x^3$,
            dec sep align,
        },
% ----------------------
% I cannot find the following key in the manual
% it doesn't cause an error but also does not  do anything
% --> is it intended that one isn't able to set a style for all columns
%     or am I missing something in the manual that this can be done?
        every column/.append style={
            dec sep align,
        },
% ----------------------
%        % if only vertical tables would be used,
%        % one could fix the column name
%        every first column/.style={
%            column name=$x$,
%        },
        % -----
        % use booktabs to make tables look nicer
        every head row/.style={
            before row=\toprule,
            after row=\midrule,
        },
        every last row/.style={
            after row=\bottomrule,
        },
    }
    % create a new table with `\MaxItem' rows and column 'new':
    \pgfplotstablenew[
        columns={
            x,
            x square
        },
    ]{\MaxItems}{\loadedtable}
%    % ---------------------------------
%    % save table to disc, if you want
%    \pgfplotstablesave[
%        columns={
%            x,
%            x square
%        },
%    ]{\loadedtable}{table-out.txt}
%    % ---------------------------------


    \sisetup{zero-decimal-to-integer}
    \def\pf(#1){\pgfmathparse{\f(#1)}\num{\pgfmathresult}}
    \def\f(#1){#1^2} %%It should also be possible to use gnuplots x**2 syntax for the value table

\begin{document}

\section{Create plot}

\begin{center}
    \begin{tikzpicture}
        \begin{axis}[
            domain=-2:10,
        ]

            % define the function to plot
            % (it can also be included directly to the argument after
            %  `expression'. It is only defined here so you don't have to
            %  change it at several places when the function changes.)
            \def\Function{x^2}

            % plot the (continuous) function
            % (in the `domain' with `samples')
            \addplot+ [no markers]
                expression {\Function};
            % plot points at `samples at'
            \addplot+ [mark=x,samples at=\Samples]
                expression {\Function};

            % plot points from table
            % (the points were stated at `set list')
            \addplot+ [mark=o]
                table {\loadedtable};
%            % you can also plot the points from saved table.
%            \addplot+ [mark=o]
%                table {table-out.txt};

        \end{axis}
    \end{tikzpicture}
\end{center}


\section{Create tables ``automatically''}

Then I want a value table, something like generated automatically.


\begin{center}
    \captionof{table}{Original manual vertical table (\emph{left}) and new
                      ``automatic'' table (\emph{right})}
    %% \gen_value_table[samples at={1,2,3.5,10},orientation=]{\f}
    \begin{tabular}{c|c}
      $x$ & $f(x)$   \\\hline
      1   & \pf(1)   \\
      2   & \pf(2)   \\
      3,5 & \pf(3.5) \\
      10  & \pf(10)  \\
    \end{tabular}
    %
    % show (``automatic'') table
    \pgfplotstabletypeset{\loadedtable}
    %
    \pgfplotstabletypeset[
        columns={
            x,
            x square
        },
    ]{
        x
        1
        2
        3.5
        10
    }
\end{center}


\begin{center}
    \captionof{table}{Original manual horizontal table (\emph{top}) and new
                      ``automatic'' table (\emph{down})}
    %% \gen_value_table[samples at={1,...,10},orientation=horizontal]{\f}
    \begin{tabular}{*{10}{c|}c}
     $x$&   1      & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10  \\\hline
      $f(x) $ & \pf(1) & \pf(2)  & \pf(3)  & \pf(4)  &  \pf(5) & \pf(6)  &\pf(7)   & \pf(8)  & \pf(9)  & \pf(10)
    \end{tabular}
        \vspace{2ex}

% -----
% up to here everything is fine, but when transposing the table an
% additional header row is created (--> don't know how it can be suppressed).
% Also I don't know why the ``LaTeX'' column headers get lost and only the
% column header from the table is printed
\pgfplotstabletranspose{\loadedtableTransposed}{\loadedtable}
\pgfplotstabletypeset[
    every first column/.append style={
        string type,
    },
    input colnames to=,
        ]{\loadedtableTransposed}
\end{center}

\end{document}

显示上述代码结果的图像

答案2

我发现这个包解决了我的问题:

\documentclass{article}
\usepackage{array}
\usepackage{amsmath}
\usepackage{tabularcalc} % tabellen van functiewaarden
\begin{document}
\tcsetcoltype{|r|}{>{\centering\arraybackslash}m{1.5cm}|}
\htablecalc [1]{$x$}{x=-3,-2,-1,0,1,2,3} {$f(x)$}{2*x+3}
\end{document}

在此处输入图片描述

相关内容