2

I have an array with values. I wish to do some calculation on it and find than the maximum.

See the code:

\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}
  %value array with 80 values
  \def\Rtable{{100, 100.391, 100.781, 101.172, 101.562, 101.953, 102.343, 102.733, 103.123, 103.513, 103.903, 104.292, 104.682, 105.071, 105.46, 105.849, 106.238, 106.627, 107.016, 107.405, 107.794, 108.182, 108.57, 108.959, 109.347, 109.735, 110.123, 110.51, 110.898, 111.286, 111.673, 112.06, 112.447, 112.835, 113.221, 113.608, 113.995, 114.382, 114.768, 115.155, 115.541, 115.927, 116.313, 116.699, 117.085, 117.47, 117.856, 118.241, 118.627, 119.012, 119.397, 119.782, 120.167, 120.552, 120.936, 121.321, 121.705, 122.09, 122.474, 122.858, 123.242, 123.626, 124.009, 124.393, 124.777, 125.16, 125.543, 125.926, 126.309, 126.692, 127.075, 127.458, 127.84, 128.223, 128.605, 128.987, 129.37, 129.752, 130.133, 130.515, 130.897}}
  %initializes the result value
  \def\RiValue{0}
  Test the result value: \RiValue.\\[\baselineskip]
  %calculation
  \foreach \i in {0, ..., 80}
  {
    \pgfmathparse{abs(\Rtable[\i] - 100 - 3.0897 / 8 * \i)}%this line works (see link below)
    \pgfmathsetmacro{\RiValue}{max(\RiValue, \pgfmathresult)}
    %the max routine seems not doing the right thing, because \RiValue stores every value of the abs routine
    %also \RiValue and \pgfmathresult have both the same value in each iteration
    \i: \RiValue\ and \pgfmathresult\newline
  }\\[\baselineskip]
  %\RiValue seems not changed at all
  The result value at the end: \RiValue.
\end{document}

The link to my other question (where I wish use this calculation).

What do I wrong?

  • Do you do anything wrong? What makes you think so? – cfr Apr 5 '17 at 22:11
  • Hello @cfr! Thank you for your comment and answer! Sorry for my bad explanation. You are right in this case the abs routine does nothing (I have other cases in my task, there I need the abs). I didn't knew that in TeX a loop don't change a global variable without special commands (\global\let). So thank you for this information! You have intuitive solved my question despite my bad explanation. Your answer is exactly what I wanted! Thank you and shame on me... – Su-47 Apr 10 '17 at 3:50
3

It isn't clear what you want to happen. \RiValue is only redefined locally, though, so it is zero at the start of each loop. Since none of the values produced are negative, the calculated value is always the maximum.

Do you expect something like this?

possible desiderata

\documentclass[twocolumn]{article}
\usepackage{tikz}
\begin{document}
  %value array with 80 values
  \def\Rtable{{100, 100.391, 100.781, 101.172, 101.562, 101.953, 102.343, 102.733, 103.123, 103.513, 103.903, 104.292, 104.682, 105.071, 105.46, 105.849, 106.238, 106.627, 107.016, 107.405, 107.794, 108.182, 108.57, 108.959, 109.347, 109.735, 110.123, 110.51, 110.898, 111.286, 111.673, 112.06, 112.447, 112.835, 113.221, 113.608, 113.995, 114.382, 114.768, 115.155, 115.541, 115.927, 116.313, 116.699, 117.085, 117.47, 117.856, 118.241, 118.627, 119.012, 119.397, 119.782, 120.167, 120.552, 120.936, 121.321, 121.705, 122.09, 122.474, 122.858, 123.242, 123.626, 124.009, 124.393, 124.777, 125.16, 125.543, 125.926, 126.309, 126.692, 127.075, 127.458, 127.84, 128.223, 128.605, 128.987, 129.37, 129.752, 130.133, 130.515, 130.897}}
  %initializes the result value
  \def\RiValue{0}
  Test the result value: \RiValue.\par
  %calculation
  \foreach \i in {0, ..., 80}
  {
    \pgfmathsetmacro\myresult{abs(\Rtable[\i] - 100 - 3.0897 / 8 * \i)}%this line works (see link below)
    \pgfmathsetmacro{\RiValue}{max(\RiValue, \myresult)}
    %the max routine seems not doing the right thing, because \RiValue stores every value of the abs routine
    %also \RiValue and \pgfmathresult have both the same value in each iteration
    \i: \RiValue\ and \myresult\par
    \global\let\RiValue\RiValue
  }
  %\RiValue seems not changed at all
  The result value at the end: \RiValue.
\end{document}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.