递归函数

递归函数

我需要使用下面的代码来确定给定函数的第 n 个导数,这受到递归函数的启发。

\documentclass[tikz,border=5mm]{standalone}
\usepackage[utf8]{inputenc} 
\usepackage{etoolbox}
\usepackage{tikz,pgfplots}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}
\usetikzlibrary{math}
\usetikzlibrary{calc}
\usepackage{float}
\usepackage{amsmath}
\usepackage{xfp}
\usepackage{amsmath}
\usetikzlibrary{math}
\newcommand{\ar}[1]{\fpeval{round(#1,5)}}
\newcommand{\f}{0.5\cos(2t+\pi)}%Para escrever em LaTeX o nome da função


\begin{document}


 \tikzset{fixed point arithmetic} 
 \tikzmath{
  real \xzero; 
  real \h;
  %
  \xzero = 0.4;
  \h = 0.0001;
  %
  function f(\x) {
      \y =  0.5*cos(deg(2*\x+pi));
  return \y; 
  };
  %
  function derivate(\x,\h) {
      \y =  (f(\x+\h)-f(\x-\h))/(2*\h);
  return \y; 
  };
  %
  function Nthderivate(\x,\h,\n) {
      if \n == 1 then {
         \d = derivate(\x,\h);}
         else {
         \d =  (Nhderivate(\x+\h,\h,\n-1) -  Nhderivate(\x-\h,\h,\n-1))/(2*\h); 
      };
  return \d; 
  };
  %
  \n=1;
  \dum = derivate(\xzero,\h);
  \n=2;
  \ddois = Nthderivate(\xzero,\h,\n);
}


\begin{tikzpicture}[scale=0.8]
\node[above] (2) at (0,0)  {\parbox{15cm}{


Vamos determinar a derivada de $s(t)=\f$ no ponto $t_0=\xzero$ e com $h=\h$:
\\[0.5cm]
O valor da derivada primeira é:\\

$\displaystyle f'(\xzero) \approx \frac{f(x_0+h)-f(x_0-h)}{2h} = \dum$\\

O valor da segunda derivada é:\\

$\displaystyle f''(\xzero) \approx \frac{f'(x_0+h)-f'(x_0-h)}{2h} = \ddois$
}};
\end{tikzpicture}

\end{document}

但是,不起作用。我想使用 tikzmath。有人能帮我吗?

答案1

! Package PGF Math Error: Unknown function `Nhderivate'

如果我纠正函数名称中的拼写错误并删除有问题的内容,\\它就会运行而不会发出任何警告:

\documentclass[tikz,border=5mm]{standalone}
\usepackage[utf8]{inputenc} 
\usepackage{etoolbox}
\usepackage{tikz,pgfplots}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}
\usetikzlibrary{math}
\usetikzlibrary{calc}
\usepackage{float}
\usepackage{amsmath}
\usepackage{xfp}
\usepackage{amsmath}
\usetikzlibrary{math}
\newcommand{\ar}[1]{\fpeval{round(#1,5)}}
\newcommand{\f}{0.5\cos(2t+\pi)}%Para escrever em LaTeX o nome da função


\begin{document}


 \tikzset{fixed point arithmetic} 
 \tikzmath{
  real \xzero; 
  real \h;
  %
  \xzero = 0.4;
  \h = 0.0001;
  %
  function f(\x) {
      \y =  0.5*cos(deg(2*\x+pi));
  return \y; 
  };
  %
  function derivate(\x,\h) {
      \y =  (f(\x+\h)-f(\x-\h))/(2*\h);
  return \y; 
  };
  %
  function Nthderivate(\x,\h,\n) {
      if \n == 1 then {
         \d = derivate(\x,\h);}
         else {
         \d =  (Nthderivate(\x+\h,\h,\n-1) -  Nthderivate(\x-\h,\h,\n-1))/(2*\h); 
      };
  return \d; 
  };
  %
  \n=1;
  \dum = derivate(\xzero,\h);
  \n=2;
  \ddois = Nthderivate(\xzero,\h,\n);
}


\begin{tikzpicture}[scale=0.8]
\node[above] (2) at (0,0)  {\parbox{15cm}{


Vamos determinar a derivada de $s(t)=\f$ no ponto $t_0=\xzero$ e com $h=\h$:
\\[0.5cm]
O valor da derivada primeira é:

$\displaystyle f'(\xzero) \approx \frac{f(x_0+h)-f(x_0-h)}{2h} = \dum$

O valor da segunda derivada é:

$\displaystyle f''(\xzero) \approx \frac{f'(x_0+h)-f'(x_0-h)}{2h} = \ddois$
}};
\end{tikzpicture}

\end{document}

然而,tex 定点算法是为计算一些字体大小而设计的,进行密集的链式数值近似在数值上并不准确,因此任何结果都更多的是运气而不是设计。

在此处输入图片描述

相关内容