-- Euler's Method -- Steve Kifowit -- June 16, 2009 -- User input function f(x,y) return x*y^2 end x0 = 0 y0 = 1 Xend = 1 steps = 20 -- No user input required beyond this point, -- -- unless you want to edit the format strings -- h = ( Xend - x0 ) / steps print("Starting values:") form1 = "When x = %.6f, y = %.6f" io.write( string.format( form1, x0, y0 ), "\n" ) file = io.open("euler.dat", "w+") file:write( x0, ", ", y0, "\n" ) for i = 1, steps do y0 = y0 + h * f(x0,y0) x0 = x0 + h file:write( x0, ", ", y0, "\n" ) end file:close() print("\nEnding values:") form2 = "When x = %.6f, y is approximately %.6f" io.write( string.format( form2, x0, y0 ), "\n" ) print("\nData from individual steps written to euler.dat")