TransWikia.com

pgfplotstable: Newton's Method in pgfplotstable

TeX - LaTeX Asked on May 15, 2021

I use create on use and create col/assign/.code=... to display a Newton’s method.

The x_{n+1} column is created later as values of the x_n column, so I get an error if I want to have the x_{n+1}-value for the next x_n-value.

What do I have to do?

enter image description here

documentclass[]{article}
usepackage{amsmath}
usepackage{pgfplotstable}
pgfplotsset{compat=newest}

begin{document}

pgfmathsetmacroxStart{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}
ifnumn=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}

2 Answers

A solution with pgfplotstable:

enter image description here

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
pgfmathsetmacroxStart{xStart}
pgfmathsetmacrofxnStart{f(xStart)}
pgfmathsetmacrodfxnStart{df(xStart)}
pgfmathsetmacroxNewStart{xNew(xStart)}
pgfplotstableread[header=false, col sep=comma,
]{
0, xStart, fxnStart, dfxnStart,  xNewStart
}startrow

% Further rows
pgfmathsetmacroSteps{Steps}
pgfplotsforeachungrouped n in {1,...,Steps} {%%
ifnumn=1 pgfplotstablegetelem{0}{[index]4}ofstartrow else
pgfplotstablegetelem{0}{[index]4}ofnextrow fi
pgfmathsetmacroxOld{pgfplotsretval}
%
pgfmathsetmacrofxn{f(xOld)}
pgfmathsetmacrodfxn{df(xOld)}
pgfmathsetmacroxNew{xNew(xOld)}
%
edefcreatenextrow{
noexpandpgfplotstableread[
col sep=comma,      row sep=crcr, 
]{
n,   xOld,   fxn, dfxn, xNew noexpand
}noexpandnextrow
}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 intgers 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}

Correct answer by cis on May 15, 2021

If you only wish to create a table, you can try using LaTeX3:

enter image description here

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}

Answered by Alan Xiang on May 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP