Advertisement
incinirate

Calculating e

Oct 7th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.73 KB | None | 0 0
  1. --[[
  2.  
  3. This program attempts to approximate the value of
  4. the mathematical constant e, which is about 2.718
  5.  
  6. This method uses Euler's method for solving
  7. differential equations to approximate the function
  8. defined by the differential equation dy/dx = y,
  9. which if solved analytically is e^x. So, as x
  10. approaches 1, we get closer and closer to e.
  11.  
  12. With xStep at 0.00001, it is accurate to 4 decimals
  13.  
  14. While there are much better and more accurate ways
  15. to calculate e, this is most likely the simplest.
  16.  
  17. ]]
  18.  
  19. local xStep = 0.00001 -- Smaller = more accurate
  20.  
  21. -- Give the initial condition, when x = 0, y = 1
  22. local ypos = 1
  23. local lslope = 1
  24.  
  25. for i=1, 1/xStep do
  26.     ypos = ypos + (lslope)*xStep
  27.     lslope = ypos
  28. end
  29.  
  30. print(ypos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement