# # Euler.py # # # Created by Jason Francis on 3/16/08. # Solves First Order ODEs using Euler's method # import math #needed for the floor function #first, we need to get the differential equation deq = raw_input("y' = ") #and the initial x and y x = float(raw_input("x(0) = ")) y = float(raw_input("y(0) = ")) #this sets up our IVP, but we still need some more information step = float(raw_input("Step size: ")) #spacing from x_n to x_n+1 iterations = int(math.floor(float(raw_input("Iterations: ")))) #how many steps do we want to take? m = eval(deq) #executing a string like this is a terrible idea #We need to store the values of x and y, so open a file stream fout = open('output.eul','w') fout.write(str(x) + "\t" + str(y) + "\n") #time for the actual solving for n in range(0,iterations): x = x + step y = y + step*m m = eval(deq) fout.write(str(x) + "\t" + str(y) + "\n") n = n+1 ##### fout.close() exit