I have the implementation of Bissection method in tikzmath environment below:



\a = 0.5;
\b = 1.5; 

    function f(\x) {
    \y =   \x - cos(\x r);
    return \y; 
    int \i;
    real \p;
    real \P;
    real \lb;
    real \up;
    \p = 0.5*(\a + \b);
    for \i in {0,1,2,...,\n}{
        \lb{\i} = \a;
        \ub{\i} = \b;
         \P{\i} = \p;
        if f(\a)*f(\p) < -1e-8 then {
         \b = \p;
        if f(\a)*f(\p) > 1e-8  then {
           \a = \p;
       \p = \fpeval{round(0.5*(\a + \b),12)};
\foreach \i in {0,1,2,3,...,\fpeval{-1+\n}}{
\begin{tikzpicture}[scale=2,font=\scriptsize,declare function = {
func(\x) = \x - cos(\x r);}]
\clip (-1,-2) rectangle (4.5,2);
\draw[teal] plot [smooth,domain=0:1.5] ({2*\x},{func(\x)}) node[right]{$f(x)$};
\draw[->,-stealth] (-1,0) -- (3.8,0) node[right]{$x$};
\draw[->,-stealth] (0,-1.5) -- (0,1.5) node[above]{$y$}; 
\foreach \i in {0,0.5,...,1.5}{
\draw (2*\i,2pt) -- (2*\i,-2pt) node[below]{$\i$};
\foreach \i in {-1,-0.5,...,1}{
\draw (2pt,\i) -- (-2pt,\i) node[left,fill=white]{$\i$};
\draw[densely dotted] (2*\lb{\i},0) -- (2*\lb{\i},{func(\lb{\i})}) -- (0,{func(\lb{\i})});
\draw[densely dotted] (2*\ub{\i},0) -- (2*\ub{\i},{func(\ub{\i})}) -- (0,{func(\ub{\i})});
\fill[blue] (2*\P{\i},0) circle (1pt);
\fill[blue] (2*\P{\i},{func(\P{\i})}) circle (1pt);
\draw[densely dashed] (2*\P{\i},0) node[below right]{$p_{\i}$} -- (2*\P{\i},{func(\P{\i})});
\fill[red] (2*\lb{\i},0) circle (1pt);
\fill[red] (2*\ub{\i},0) circle (1pt);
\fill[teal] (2*\lb{\i},{func(\lb{\i})}) circle (1pt);
\fill[teal] (2*\ub{\i},{func(\ub{\i})}) circle (1pt);
\node (T) at (2,1.5) {$p_{\i}=\fpeval{round(\P{\i},8)}$};


Works perfectly. However, the precision is to bad. The approximate root is 0.73908514 but in the 50th iteration I have only 0.73828125. How can I improve these precision in this calculation?

  • TikZ features an FPU, which has much better precision (but should only be activated for a local scope, you can search the manual for it). Else, the package xfp has good precision. – Skillmon Dec 4 '20 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.