尺寸太大 - Tikzmath

尺寸太大 - Tikzmath

我有以下代码:

\documentclass[tikz,border=5mm]{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{xfp}
\usetikzlibrary{math}
\usepackage{etoolbox}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}
\newcommand{\ar}[1]{\fpeval{round(#1,4)}}


\begin{document}


\tikzmath{
    \p=8;%número de pontos fornecidos
    \q=3;%número de parâmetros a serem ajustados
    \X{1}=33;
    \X{2}=31;
    \X{3}=29;
    \X{4}=27;
    \X{5}=25;
    \X{6}=23;
    \X{7}=21;
    \X{8}=19;
    %
    \Y{1}=13232;
    \Y{2}=11686;
    \Y{3}=10236;
    \Y{4}=8882;
    \Y{5}=7624;
    \Y{6}=6462;
    \Y{7}=5396;
    \Y{8}=4426;
    %
    function phi1(\x){%Funções da base
        \y = 1;
        return \y;
    };
    function phi2(\x){%Funções da base
        \y = \x;
        return \y;
    };
    %
    function phi3(\x){%Funções da base
        \y = \x^2;
        return \y;
    };
    %
    function Phi(\t,\betaum,\betadois,\betatres) {%Função linear dos parâmetros
       \y = \betaum*phi1(\t) + \betadois*phi2(\t) + \betatres*phi3(\t);
      return \y;
    };
    %
    function G(\x) {
       real \u; real \v;
       \u = phi1(\x); 
       \v = phi2(\x);
       \w = phi3(\x);
      return {\u,\v,\w};
    };
    real \Matriz; int \i; int \j;
    for \i in {1,...,\p}{
        for \j in {1,...,\q}{
            \Matriz{\i,\j}={G(\X{\i})}[\fpeval{\j-1}];
        };
    };
}

\tikzmath{
    int \i;
    int \j;
    int \k;
    real \m;
    %
    real \soma; real \somab; real \D;
    \n=\q;
    for \i in {1,...,\q}{
        for \j in {1,...,\q}{
            \soma = 0;
            for \k in {1,...,\p}{
                \soma = \soma + \Matriz{\k,\i}*\Matriz{\k,\j};
            };
            \A{\i,\j} = \soma;
        };
    };
    for \i in {1,...,\q}{
        \somab=0;
        for \k in {1,...,\p}{ 
            \somab = \somab + \Matriz{\k,\i}*\Y{\k};
        };
        \b{\i}=\somab;
    };
    %
    for \i in {1,...,\n}{
        for \j in {1,...,\n}{
            \Alinha{\i,\j} = \A{\i,\j}; 
        };  
        \blinha{\i}=\b{\i};
    };    
    for \k in {1,...,\fpeval{\n-1}}{
        for \i in {\fpeval{\k+1},...,\n}{
            \m=\Alinha{\i,\k}/\Alinha{\k,\k};   
            \blinha{\i}=\blinha{\i}-(\m)*\blinha{\k};
            \Alinha{\i,\k}=0.0;
            \M{\i,\k} = \m;
            for \j in {\fpeval{\k+1},...,\n}{
                \Alinha{\i,\j}=\Alinha{\i,\j}-\m*\Alinha{\k,\j};
            };
        };
        for \i in {1,...,\n}{
            for \j in {1,...,\n}{
                \L{\i,\j,\k} = \Alinha{\i,\j};              
            };
            \N{\i,\k} = \blinha{\i};
        };
    };
}

\tikzmath{
    real \s;
    int \i;
    int \j;
    real \x;
    \x{\n}=\blinha{\n}/\Alinha{\n,\n};
    for \i in {\fpeval{\n-1},...,1}{
        \s=0;
        for \j in {\fpeval{\i+1},...,\n}{
            \s=\s+\Alinha{\i,\j}*\x{\j};
        };
        \x{\i}=(\blinha{\i}-\s)/\Alinha{\i,\i};
    };
}

\tikzmath{
    int \i;
    int \j;
    real \ybarra; real \SSres; real \SStot;
    \ybarra=0;
    for \i in {1,...,\p}{
        \ybarra = \ybarra + \Y{\i};
    };
    \ybarra=\ybarra/\p;
    \SSres=0;
    for \i in {1,...,\p}{
        \SSres = \SSres + (\Y{\i} - Phi(\X{\i},\x{1},\x{2},\x{3}))^2;
    };
    \SStot=0;
    for \i in {1,...,\p}{
        \SStot = \SStot + (\Y{\i} - \ybarra)^2;
    };
    \R = 1 - (\SSres/\SStot);
}

  
\begin{tikzpicture}[scale=1]
\node[above] (2) at (0,-\i)  {\parbox{14cm}{
Queremos ajustar os pontos \foreach \i in {1,...,\p}{(\X{\i},\Y{\i}), } ao modelo $y=\phi(x)=\beta_1 \phi_1(x) + \beta_2 \phi_2(x) + \beta_3 \phi_3(x)$ onde $\phi_1(x) = 1$, $\phi_2(x) = x$ e $\phi_3(x) = x^2$.\\

Vamos resolver o seguinte sistema de equações normais $\mathbf{A}^T \mathbf{A} \mathbf{x}=\mathbf{A}^T \mathbf{y}$, onde:\\

$\mathbf{A} = \left[  
\begin{array}{rrr}
 \Matriz{1,1} & \Matriz{1,2}\\
 \Matriz{2,1} & \Matriz{2,2}\\
 \Matriz{3,1} & \Matriz{3,2}\\
 \Matriz{4,1} & \Matriz{4,2}\\
 \Matriz{5,1} & \Matriz{5,2}
\end{array}
\right], \quad \mathbf{x} = \left[  
\begin{array}{r}
 \beta_1 \\
 \beta_2 \\
 \beta_3 
\end{array}
\right] \quad \text{e} \quad \mathbf{y} = \left[  
\begin{array}{r}
 \Y{1} \\
 \Y{2} \\
 \Y{3} \\
 \Y{4} \\
 \Y{5} 
\end{array}
\right]$\\[0.5cm]
resulta no seguinte sistema $\q \times \q$:\\[0.5cm]
$
\left\{
\begin{array}{lcr}
(\ar{\A{1,1}})\beta_1 + (\ar{\A{1,2}})\beta_2  + (\ar{\A{1,3}})\beta_3  & = & \ar{\b{1}} \\
(\ar{\A{2,1}})\beta_1 + (\ar{\A{2,2}})\beta_2  + (\ar{\A{2,3}})\beta_3 & = & \ar{\b{2}} \\
(\ar{\A{3,1}})\beta_1 + (\ar{\A{3,2}})\beta_2  + (\ar{\A{3,3}})\beta_3 & = & \ar{\b{3}} \\
\end{array}
\right.
$
\\

Matriz a ser escalonada:\\

$\mathbf{M} =
\left[
\begin{array}{rrr|r}
\ar{\A{1,1}}  & \ar{\A{1,2}} & \ar{\A{1,3}} & \ar{\b{1}} \\
\ar{\A{2,1}}  & \ar{\A{2,2}} & \ar{\A{2,3}} & \ar{\b{2}} \\
\ar{\A{3,1}}  & \ar{\A{3,2}} & \ar{\A{3,3}} & \ar{\b{3}} 
\end{array}
\right]
$
}};
\end{tikzpicture}

\foreach \k in {1,...,\fpeval{\n-1}}{
\begin{tikzpicture}[scale=1]
\node (Te) at (0,0) {
\parbox{14cm}{Fazendo o escalonamento no passo $k=\k$ teremos:\\[0.5cm]}};
\ifnum\k=1
\node (5) at (0,-2)  {
\parbox{14cm}{Matriz atualizada:\\[0.5cm]
$
\left[
\begin{array}{rrr|r}
\ar{\A{1,1}} & \ar{\A{1,2}} & \ar{\A{1,3}} & \ar{\b{1}}\\
\ar{\A{2,1}} & \ar{\A{2,2}} & \ar{\A{2,3}} & \ar{\b{2}}\\
\ar{\A{3,1}} & \ar{\A{3,2}} & \ar{\A{3,3}} & \ar{\b{3}}
\end{array} 
\right]
\Longrightarrow
\left[
\begin{array}{rrr|r}
\ar{\L{1,1,\k}} & \ar{\L{1,2,\k}} & \ar{\L{1,3,\k}} & \ar{\N{1,\k}}\\
\ar{\L{2,1,\k}} & \ar{\L{2,2,\k}} & \ar{\L{2,3,\k}} & \ar{\N{2,\k}}\\
\ar{\L{3,1,\k}} & \ar{\L{3,2,\k}} & \ar{\L{3,3,\k}} & \ar{\N{3,\k}}
\end{array} 
\right]
$\\[0.5cm] 
com as seguintes operações:\\[0.5cm]
}};
\foreach \i in {\fpeval{\k+1},...,\n}{
\node at (0,{-(\i-\k+1)-1.5}) {\parbox{14cm}{$m_{\k \i} = \fpeval{round(\M{\i,\k},4)}$ e $L_{\i} \leftarrow L_{\i} - (\fpeval{round(\M{\i,\k},4)}) L_{\k}$}};
}
\else
\node (5) at (0,-2)  {
\parbox{14cm}{Matriz atualizada:\\[0.5cm]
$
\left[
\begin{array}{rrr|r}
\ar{\L{1,1,\fpeval{\k-1}}} & \ar{\L{1,2,\fpeval{\k-1}}} & \ar{\L{1,3,\fpeval{\k-1}}} & \ar{\N{1,\fpeval{\k-1}}}\\
\ar{\L{2,1,\fpeval{\k-1}}} & \ar{\L{2,2,\fpeval{\k-1}}} & \ar{\L{2,3,\fpeval{\k-1}}} & \ar{\N{2,\fpeval{\k-1}}}\\
\ar{\L{3,1,\fpeval{\k-1}}} & \ar{\L{3,2,\fpeval{\k-1}}} & \ar{\L{3,3,\fpeval{\k-1}}} & \ar{\N{3,\fpeval{\k-1}}}\\
\end{array} 
\right]
\Longrightarrow
\left[
\begin{array}{rrr|r}
\ar{\L{1,1,\k}} & \ar{\L{1,2,\k}} & \ar{\L{1,3,\k}} & \ar{\N{1,\k}}\\
\ar{\L{2,1,\k}} & \ar{\L{2,2,\k}} & \ar{\L{2,3,\k}} & \ar{\N{2,\k}}\\
\ar{\L{3,1,\k}} & \ar{\L{3,2,\k}} & \ar{\L{3,3,\k}} & \ar{\N{3,\k}}\\
\end{array} 
\right] 
$ \\[0.5cm] 
com as seguintes operações:\\[0.5cm]
}};
\foreach \i in {\fpeval{\k+1},...,\n}{
\node at (0,{-(\i-\k+1)-1.5}) {\parbox{14cm}{$m_{\k \i} = \ar{\M{\i,\k}}$ e $L_{\i} \leftarrow L_{\i} - (\ar{\M{\i,\k}}) L_{\k}$}};
}
\fi
\end{tikzpicture}
}



\begin{tikzpicture}
\node at (0,\n) {\parbox{14cm}{
Assim, a solução do sistema linear é calculada por substituição retroativa do seguinte modo:\\[0.5cm]
$\beta_\n =  \ar{\blinha{\n}}/(\ar{\Alinha{\n,\n}}) = \ar{\x{\n}}$
}}; 
\foreach \i in {\fpeval{\n-1},...,1}{
\node at (0,{\i-0.5}) {\parbox{14cm}{
$\beta_\i =  \left( \ar{\blinha{\i}} - \sum_{j=\fpeval{\i+1}}^{\n} a_{\i j} \cdot x_j \right)/(\ar{\Alinha{\i,\i}})  = \ar{\x{\i}}$
}}; 
}
\node at (0,{-\n-1}) {\parbox{14cm}{

De maneira que a solução do sistema de equações normais é:\\

$\beta^* = \left[
\begin{array}{rrrr}
 \ar{\x{1}} & \ar{\x{2}} & \ar{\x{3}} 
 \end{array}  \right]
$\\

ou seja, o modelo ajustado é\\ 

$\boxed{y= \phi(x) = \foreach \i in {1,...,\fpeval{\q-1}}{(\ar{\x{\i}})\phi_{\i}(x)+}(\ar{\x{\q}})\phi_{\q}(x)}$\\


Com $r^2$ dado por:\\

$\displaystyle r^2 = 1 - \frac{\text{SSres}}{\text{SStot}} = \ar{\R}$\\

onde $\displaystyle \text{SSres} = \sum_{i=1}^\p{} (y_i - \phi(x_i))^2$ e $\displaystyle \text{SStot} = \sum_{i=1}^{\p} (y_i - \bar{y})^2$ 

e com a soma dos residuos igual a:\\

$\text{SSres} = \ar{\SSres}$.
}};
\end{tikzpicture}
\end{document}

但是,编译器给出以下错误:dimension too large

在此处输入图片描述

对于较小的数字,代码运行完美。

我怎样才能解决这个问题?

相关内容