我使用create on use
和create col/assign/.code=...
来展示牛顿方法。
x_{n+1} 列稍后作为 x_n 列的值创建,因此如果我想要下一个 x_n 值的 x_{n+1} 值,就会出现错误。
我需要做什么?
\documentclass[]{article}
\usepackage{amsmath}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\begin{document}
\pgfmathsetmacro\xStart{0}
\tikzset{
declare function={
f(\x)=exp(\x)-2*\x*\x;
df(\x)=exp(\x)-4*\x;
},}
$f(x)=e^x-2x^2,~ f'(x)=e^x-4x,~~$\par
$x_0=\xStart, ~ x_{n+1}=x_n-\dfrac{f(x_n)}{f'(x_n)}$
\pgfplotstableset{
create on use/n/.style={
create col/set list={0,...,7}
},
%string type,
column type=l,
}
\pgfplotstableset{
% Problem here ======================
create on use/xn/.style={
create col/assign/.code={
\getthisrow{n}{\n}
\ifnum\n=0 \pgfkeyslet{/pgfplots/table/create col/next content}{\xStart}
\else%
\getprevrow{xnP}{\xnP}
%
\pgfmathsetmacro{\mynewentry}{\n+0.2}% wrong value
\pgfkeyslet{/pgfplots/table/create col/next content}{\mynewentry} \fi
}},
% =============================
create on use/fxn/.style={
create col/assign/.code={
\getthisrow{xn}{\xn}
\pgfmathsetmacro{\fxn}{f(\xn)}%
\pgfkeyslet{/pgfplots/table/create col/next content}{\fxn}
}},
create on use/dfxn/.style={
create col/assign/.code={
\getthisrow{xn}{\xn}
\pgfmathsetmacro{\dfxn}{df(\xn)}%
\pgfkeyslet{/pgfplots/table/create col/next content}{\dfxn}
}},
create on use/xnP/.style={
create col/assign/.code={
\getthisrow{xn}{\xn}
\getthisrow{fxn}{\fxn}
\getthisrow{dfxn}{\dfxn}
\pgfmathsetmacro{\xnP}{\xn-\fxn/\dfxn}%
\pgfkeyslet{/pgfplots/table/create col/next content}{\xnP}
}},
}
\pgfplotstablenew[columns={n,xn,fxn,dfxn,xnP}]{7}\cistable
\bigskip
\pgfplotstabletypeset[
columns/n/.style={column name=$n$},
columns/xn/.append style={column name=$x_n$},
columns/xnP/.append style={column name=$x_{n+1}$},
columns/fxn/.style={column name=$f(x_n)$},
columns/dfxn/.style={column name=$f'(x_n)$},
]{\cistable}
\end{document}
答案1
如果您只想创建一个表格,您可以尝试使用 LaTeX3:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{expl3}
\usepackage{amsmath, amssymb}
\usepackage{siunitx}
\begin{document}
% set precision
\sisetup{round-mode=places,round-precision=8}
\ExplSyntaxOn
% the function itself
\cs_set:Npn \f:n #1 {
\fp_eval:n {exp(#1) - 2.0 * (#1) * (#1)}
}
% the derivative
\cs_set:Npn \d_f:n #1 {
\fp_eval:n {exp(#1) - 4.0 * (#1)}
}
% n
\int_new:N \g_n_int
% x_n
\fp_new:N \g_x_n_fp
% x_{n+1}
\fp_new:N \g_x_n_n_fp
% initialize values
\int_set:Nn \g_n_int {0}
\fp_set:Nn \g_x_n_fp {0.0}
% function to generate one row
\newcommand{\getrow}{
% compute x_{n+1}
\fp_gset:Nn \g_x_n_n_fp { \g_x_n_fp - (\f:n {\g_x_n_fp}) / (\d_f:n {\g_x_n_fp}) }
% output values
\int_use:N \g_n_int &
\num{\fp_use:N \g_x_n_fp} &
\num{\f:n {\g_x_n_fp}} &
\num{\d_f:n {\g_x_n_fp}} &
\num{\fp_use:N \g_x_n_n_fp}
\tabularnewline
% update x_n
\fp_gset:Nn \g_x_n_fp {\g_x_n_n_fp}
% update n
\int_gincr:N \g_n_int
}
\ExplSyntaxOff
\begin{tabular}{ccccc}
$n$ & $x_n$ & $f(x_n)$ & $f'(x_n)$ & $x_{n+1}$\\
\getrow
\getrow
\getrow
\getrow
\getrow
\getrow
\end{tabular}
\end{document}
答案2
解决方案如下pgfplotstable
:
\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\begin{document}
\tikzset{
declare function={
xStart=0;
Steps=5;
f(\x)=exp(\x)-2*\x*\x;
% Calc
df(\x)=exp(\x)-4*\x;
%dx=0.001; df(\x)=( f(\x+dx) - f(\x) )/dx;
xNew(\x)=\x-f(\x)/df(\x);
},}
% Start row
\pgfmathsetmacro\xStart{xStart}
\pgfmathsetmacro\fxnStart{f(xStart)}
\pgfmathsetmacro\dfxnStart{df(xStart)}
\pgfmathsetmacro\xNewStart{xNew(xStart)}
\pgfplotstableread[header=false, col sep=comma,
]{
0, \xStart, \fxnStart, \dfxnStart, \xNewStart
}\startrow
% Further rows
\pgfmathsetmacro\Steps{Steps}
\pgfplotsforeachungrouped \n in {1,...,\Steps} {%%
\ifnum\n=1 \pgfplotstablegetelem{0}{[index]4}\of\startrow \else
\pgfplotstablegetelem{0}{[index]4}\of\nextrow \fi
\pgfmathsetmacro\xOld{\pgfplotsretval}
%
\pgfmathsetmacro\fxn{f(\xOld)}
\pgfmathsetmacro\dfxn{df(\xOld)}
\pgfmathsetmacro\xNew{xNew(\xOld)}
%
\edef\createnextrow{
\noexpand\pgfplotstableread[
col sep=comma, row sep=crcr,
]{
\n, \xOld, \fxn, \dfxn, \xNew \noexpand\\
}\noexpand\nextrow
}\createnextrow
%
% Concatenate in loop
\pgfplotstablevertcat{\temprow}{\nextrow}
}%%
% Concatenate with startrow
\pgfplotstablevertcat{\startrow}{\temprow}
% Output
$f(x)=e^x-2x^2,~~ f'(x)=e^x-4x.$\par
$x_0=\xStart,~~ x_{n+1}=x_n-\dfrac{f(x_n)}{f'(x_n)}.$
\bigskip
\pgfplotstabletypeset[column type=r,
% Show integers as integers and general number format:
every column/.style={postproc cell content/.style={
@cell content=\pgfmathifisint{##1}
{\pgfmathprintnumber[precision=0]{##1}}
{\pgfmathprintnumber[fixed, fixed zerofill, precision=4]{##1}}
}},
display columns/0/.style={column name=$n$},
display columns/1/.style={column name=$x_n$},
display columns/2/.style={column name=$f(x_n)$},
display columns/3/.style={column name=$f'(x_n)$},
display columns/4/.style={column name=$x_{n+1}$},
]{\startrow}
\end{document}