0

I have this code with the following instructions (I'm doing numerical integration and was trying to see how to do it all)

--[[

    Calculates a table of successive trapezoidal estimates
    for the integral of f from a to b.  It creates a table
    "estimates" such that
      estimates[1] is the trapezoidal estimate for one sub interval
      estimates[2] is the trapezoidal estimate for two sub intervals
      estimates[3] is the trapezoidal estimate for four sub intervals
      estimates[4] is the trapezoidal estimate for eight sub intervals
    and so on to
      estimates[n+1] is the trapezoidal estimate for 2^n sub intervals
    where n is the value of max_n_doublings (25 by default)

    In all cases, sub intervals have equal width.

    It does this by calling the init_trapezoidal and half_trapezoidal
    functions and combining this with the previous estimate.

    The function returns the table estimates.

--]]
function compute_trapezoidal_table(f,a,b,max_n_doublings)
    max_n_doublings = max_n_doublings or 25
    estimates = {}
    estimates[1] = init_trapezoidal(f,a,b)
    for i = 2, max_n_doublings do
        local intervals = 1
        estimates[i] = half_trapezoidal(f,a,b,intervals)
        intervals = intervals * 2
    end
    print(table.concat(estimates," "))
end

My question is -- when running the actual programme, here I'm running it in overleaf, as it has to be presented as a report - how do I edit the code that I wrote to ensure that the code allows me to present the table too?

As additional information - init_trapezoidal computes the 1 sub interval and half_trapezoidal computes it for n_subint intervals

Thank you for your help

  • 1
    Can you include an example input, the current output and the desired output? – Nifim Nov 25 '20 at 19:33
  • I think what you're asking is, how to dump the contents of your table. There are a few ways -- https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console -- it's often called prettyprint, or serialize -- http://lua-users.org/wiki/TableSerialization – Doyousketch2 Nov 25 '20 at 20:04
  • we cannot tell you how to output someting if you don't tell us how it is supposed to look like. – Piglet Nov 26 '20 at 17:29
  • May be you need something like dumptable: https://stackoverflow.com/questions/9168058/how-to-dump-a-table-to-console or output data as JSON: http://lua-users.org/wiki/JsonModules – Darius Nov 27 '20 at 14:07

0 Answers0