title: numerical prediction of a pendulums motion in python not predicting properly
body: <p>I'm working on a numerical prediction of a pendulums motion for a project. while running the program I've noticed the time between each peak (when the value reaches the same theta value as the beginning value of theta) is not the same as the following equation predicts(T = 2<em>pi</em>sqrt(l/g)). If there's anyone how could take a look I would be so grateful(I know its kind of a mess...lol)</p>
  <p>p.s only the blue graph is relevant, ignore the orang on also ignore the path of <code>&quot;graph_pick == &quot;2&quot;&quot;</code>.</p>
  <pre><code>import math
  import matplotlib.pyplot as plt
  import tabulate
  import pandas as pd
  
  time = list()
  amp = list()
  velocity = list()
  force = list()
  acceleration = list()
  step = list()
  velocity_change = list()
  step_change = list()
  time_change = list()
  amplitude_change = list()
  period = list()
  
  def print_plot(x, y):
      t_data = pd.read_excel(r'C:\Users\n\Desktop\עבודת גמר פיזיקה\data for python numeric model.xlsx', usecols=&quot;A:A&quot;,
                             engine='openpyxl')
      theta_data = pd.read_excel(r'C:\Users\n\Desktop\עבודת גמר פיזיקה\data for python numeric model.xlsx', usecols=&quot;B:B&quot;,
                                 engine='openpyxl')
      plt.figure()
      plt.scatter(x, y)
      plt.scatter(t_data, theta_data)
      plt.xlabel(&quot;time&quot;)
      plt.ylabel(&quot;amplitude&quot;)
      plt.show()
  
  
  def prediction(dt_step, mass, b_const, gravity, length, angle, drag):
      t = 0
      v = 0
      i = 0
      step.append(i)
      if drag == '1':
          f = round(-b_const * length * v - mass * gravity * math.sin(angle), 4)
      if drag == '2':
          f = round(-mass * gravity * length * math.sin(angle), 4)
      a = round(f / mass, 4)
  
      time.append(t)
      amp.append(angle)
      velocity.append(v)
      force.append(f)
      acceleration.append(a)
  
      while t &lt;= 30:
          i += 1
          t = round(t + dt_step, 4)
          v = round(v + a * dt_step, 4)
          angle = round(angle + v * dt_step, 4)
          if drag == '1':
              f = round(-b_const * length * v - mass * gravity * length * math.sin(angle), 4)
          if drag == '2':
              f = round(-mass * gravity * length * math.sin(angle), 4)
          a = round(f / mass, 4)
  
          time.append(t)
          amp.append(angle)
          velocity.append(v)
          force.append(f)
          acceleration.append(a)
          step.append(i)
  
      return time, amp, velocity, force, acceleration, step
  
  
  def print_table(t, a, v, f, acc, i):
      print(str(i) + &quot;   &quot; + str(t[i]) + &quot;    &quot; + str(a[i]) + &quot;    &quot; + str(f[i]) + &quot;    &quot; + str(v[i]) + &quot;    &quot; + str(
          acc[i]))
  
  # constants
  dt = 0.01
  m = 0.056
  b = 3.49 * pow(10, -5)
  g = 9.81
  l = 0.7
  theta = 0.172
  print(&quot;calculate --&gt; 1:with drag  2:no drag&quot;)
  use_drag = input(&quot;enter: &quot;)
  print(&quot;graph --&gt; 1:amplitude over time 2:period over time&quot;)
  graph_pick = input(&quot;enter: &quot;)
  
  time, amp, velocity, force, acceleration, step = prediction(dt, m, b, g, l, theta, use_drag)
  
  if graph_pick == &quot;1&quot;:
      print(tabulate.tabulate(
          {&quot;step&quot;: step, &quot;time&quot;: time, &quot;amplitude&quot;: amp, &quot;velocity&quot;: velocity, &quot;force&quot;: force, &quot;acceleration&quot;: acceleration},
          headers=[&quot;step&quot;, &quot;time&quot;, &quot;amplitude&quot;, &quot;velocity&quot;, &quot;force&quot;, &quot;acceleration&quot;]))
    
      print_plot(time, amp)
  
  if graph_pick == &quot;2&quot;:
      velocity_change.append(velocity[0])
      step_change.append(0)
      time_change.append(time[0])
      amplitude_change.append(amp[0])
      period.append(0)
      k = 1
      end = 30/dt
      n = 1
      while k &lt;= end:
          if abs(amp[k]) &gt;= abs(amp[k-1]) and abs(amp[k]) &gt;= abs(amp[k+1]):
              if abs(velocity[k]) &lt; abs(velocity[k-1]) and abs(velocity[k]) &lt; abs(velocity[k+1]):
                  velocity_change.append(velocity[k])
                  step_change.append(k)
                  time_change.append(time[k])
                  amplitude_change.append(amp[k])
                  n += 1
          k += 1
  
  
      print(tabulate.tabulate(
         {&quot;step&quot;: step_change, &quot;time&quot;: time_change, &quot;amplitude&quot;: amplitude_change, &quot;velocity&quot;: velocity_change},
         headers=[&quot;step&quot;, &quot;time&quot;, &quot;amplitude&quot;, &quot;velocity&quot;]))
  
  &quot;&quot;&quot;&quot;
   if velocity[k - 1] &gt; 0 and velocity[k + 1] &lt; 0:
      velocity_change.append(velocity[k])
      step_change.append(k)
      time_change.append(time[k])
      amplitude_change.append(amp[k])
      n += 1
  if velocity[k - 1] &lt; 0 and velocity[k + 1] &gt; 0:
      velocity_change.append(velocity[k])
      step_change.append(k)
      time_change.append(time[k])
      amplitude_change.append(amp[k])
      n += 1
  k += 1
  </code></pre>