Метод Ньютона-Рафсона LaTeX - PullRequest
1 голос
/ 11 июля 2020

Как реализовать метод Ньютона-Рафсона в LaTeX?

1 Ответ

2 голосов
/ 11 июля 2020
\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{xfp}
\usepackage{tikz}

\newcount\MaxIter
\newcount\Counter
% the function
\newcommand{\f}[1]{%
    #1%
}
% first derivation of the function
\newcommand{\df}[1]{%
    1%
}
% the command
% 1. variable: max iterations
% 2. variable: start value x_0
\newcommand{\NewtonRaphson}[2][5]{
    \Counter=0%
    \MaxIter=#1%
    \pgfmathsetmacro{\x}{#2}%
    \Loop%
}
% the loop
\newcommand{\Loop}{%
    \let\next= \Loop%
    \pgfmathsetmacro{\tempf}{\fpeval{\f{\x}}}%
    \pgfmathsetmacro{\tempdf}{\fpeval{\df{\x}}}%
    \pgfmathsetmacro{\x}{\fpeval{\x-\tempf/\tempdf}}%
    \ifnum\Counter<\MaxIter \advance\Counter by 1 \else \let\next=\relax \fi%
    \next%
}

\begin{document}
    % set function
    \renewcommand{\f}[1]{%
        #1^2-2%
    }
    % set first derivation
    \renewcommand{\df}[1]{%
        2*#1%
    }
    % calculate
    \NewtonRaphson[10]{0.5}
    % print
    \pgfmathprintnumber[precision=10]{\x}
\end{document}
...