title: Implementing Euler's Method in python to solve ODE
body: <p>I am working on a little science project to simulate a mechanical system with ODE (Ordinary Differential Equation). </p>
  
  <p><img src="https://i.stack.imgur.com/pFfcg.jpg" alt="differential equations of the mechanical system"></p>
  
  <p>I would like to use Euler's Method to do so, however I believe that I am doing something work, because the plot that I create of the output data isn't what it is supposed to be and I am 100% sure that the <strong>equation</strong> itself <strong>is correct</strong>. Could you look at my code and tell me what I am doing wrong? This code will output CSV values into console and into output.csv file.</p>
  
  <p>Test:</p>
  
  <pre><code>class Calculus:
      def __init__(self):
          print "object created"
  
      @staticmethod
      def euler(f,y0,a,b,h):
          """y0 - temperatura poczatkowa, a-chwila poczatkowa czasu(warunek brzegowy) , b-chwila koncowa, h - krok"""
          t,y = a,y0
          time = [t]
          value = [y]
  
          while t &lt;= b:
              print "%6.3f,%6.3f" % (t,y)
  
              t += h
              y += h * f(t,y, h, value)
              time.append(t)
              value.append(y)
  
          data = {'time' : time, 'value' : value}
          return data
  
  class Mechanical_system:
      #Constructor
      def __init__(self, momentum1, momentum2, b1, b2, N1, N2, w1, Tau, k):
          self.system_parameters = {'momentum1': momentum1, 'momentum2': momentum2,
                                                 'b1': b1, 'b2' : b2, 
                                                 'N1': N1, 'N2' : N2, 
                                                 'w1' : w1,
                                                 'Tau' : Tau,
                                                 'k' : k};
  
      def ODE(self, time, w, h, value):
          """ODE - ordinary differential equation describing our system"""
  
          #first calculation will only have one value in the list, so we can't calculate delt_value = current - last
          #thus, we need to assume that if there is no penault value (index error) let penault = 0
          try:
              penault = value[len(value) -2]
          except IndexError:
              penault = 0
  
  
          momentum1 = self.system_parameters['momentum1']
          momentum2 = self.system_parameters['momentum2']
          b1 =        self.system_parameters['b1']
          b2 =        self.system_parameters['b2']
          N1 =        self.system_parameters['N1']
          N2 =        self.system_parameters['N2']
          Tau =       self.system_parameters['Tau']
          k =         self.system_parameters['k']
  
          dOmega = w - penault
          dt = h
          Omega = float(dOmega) / float(dt)
  
          return (1.0 / (momentum1 + ((N1/N2)**2)*momentum2))*(Tau - (Omega)*(b1 + ((N1/N2)**2)*b2) - w * ((N1/N2)**2)*k)
  
  if __name__ == "__main__":
          """reads values from input boxes and then calculates the value and plot value(time)"""
          momentum1 = 1
          momentum2 = 2
          b1 =        0.5
          b2 =        0.6
          N1 =        10
          N2 =        20
          a =         0
          b =         100
          h =         0.1
          w1 =        0
          Tau =       100
          k =         0.5
  
          system1 = Mechanical_system(momentum1, momentum2, b1, b2, N1, N2, w1, Tau, k)
          data = Calculus.euler(system1.ODE, w1, a, b, h)
  
          ##writing output to CSV file
          f = open('output.csv', 'w')
          for index in range(len(data['time'])):
              f.write("%s,%s\n" % (data['time'][index], data['value'][index]))
  
          f.close()
  
          del system1
          del data
  </code></pre>