TI-Nspire & Lua / シンプソンの公式による積分

.lua

function integraSimps(f, from, to, numOfDiv)
   local sum = 0
   local step = (to - from) / (numOfDiv or 2^5)
   ---[[
   for i = (from + 2 * step), (to + step / 10), (2 * step) do
      sum = sum + (f(i - 2 * step) + 4 * f(i - step) + f(i)) * step / 3   
   end--]]
   --[[
   for i = (from), (to -  2 * step + step / 10), (2 * step) do
      sum = sum + (f(i) + 4 * f(i + step) + f(i + 2 * step)) * step / 3   
   end--]]
   return sum
end

-- 確かめてみる
print(integraSimps(math.exp, 0, 2))

print(integraSimps(math.sin, 0, math.pi))

function sq(x) return x^2 end
print(integraSimps(sq, 0, 10))

f:id:ti-nspire:20170401104220p:plain


Nspire で検算:
f:id:ti-nspire:20170331101315p:plain:w450


参考: Excelでわかる数学の基礎, p.95-98