Subscribed unsubscribe Subscribe Subscribe

TI-Nspire / 微積分 / 臺形積分 / タイマーの tick するたびに臺形の面積を 1 個ずつ足してゆく

TI-Nspire + 微積分



Press the Enter key to run the following script.
.lua

-- Trapezoidal Integration
DT   = 0.1
OLD  = nil
NEW  = nil
SUM  = 1    -- initial value
data = {    -- f(x) = e^x, x = from 0 to 2, dx = 0.1
1,
1.105170918,
1.221402758,
1.349858808,
1.491824698,
1.648721271,
1.8221188,
2.013752707,
2.225540928,
2.459603111,
2.718281828,
3.004166024,
3.320116923,
3.669296668,
4.055199967,
4.48168907,
4.953032424,
5.473947392,
6.049647464,
6.685894442,
7.389056099,
}

function integral(sum, old, new, dt)
   if old == nil then 
      return sum
   else
      return sum + (old + new) * dt / 2
   end
end
function on.enterKey()
   on.timer()
   timer.start(0.5)
end
function on.timer()
   if data[1] then
      NEW = data[1]
      SUM = integral(SUM, OLD, NEW, DT)
      print(SUM)
      OLD = NEW
      table.remove(data, 1)
   else
      print("no data")
      timer.stop()
   end   
end
Remove all ads