0

I have a task where I have to find at what time a runner passes the 100m mark. I made a plot, but how can I choose out a point on the y-axis and make the program tell me what value on the x-axis corresponds to it?

import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0,7, 71)
dt = t[1] - t[0]
n = len(t)
a = np.zeros(n, float)
x = np.zeros(n, float)
v = np.zeros(n, float)
a[0] = 0.0
x[0] = 0.0
v[0] = 0.0

for i in range(0, n-1):
    a[i] = 5 - (0.34911*v[i]**2)/80
    v[i+1] = v[i] + a[i]*dt
    x[i+1] = x[i] + v[i]*dt

plt.subplot(3,1,1)
plt.plot(t,x)
plt.xlabel('t [s]')
plt.ylabel('x [m]')
plt.subplot(3,1,2)
plt.plot(t,v)
plt.xlabel('t [s]')
plt.ylabel('v [m/s]')
plt.subplot(3,1,3)
plt.plot(t,a)
plt.xlabel('t [s]')
plt.ylabel('a [m/s^2]')

plt.show
Poz
  • 13
  • 2

2 Answers2

0

If x[i-1] is < 100m, and x[i] is >= 100m, then the time is between t[i-1] seconds and t[i] seconds. You can find a close value using linear interpolation:

tt = t[i-1] + (t[i] - t[i-1]) * (100 - x[i-1]) / (x[i] - x[i-1)

If the first value (x[0]) is already > 100m, then t[i-1] = x[i-1] = 0

stark
  • 12,615
  • 3
  • 33
  • 50
0

This is my solution that relies on the work you have already done.

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

t = np.linspace(0,7, 71)
dt = t[1] - t[0]
n = len(t)
a = np.zeros(n, float)
x = np.zeros(n, float)
v = np.zeros(n, float)

a[0] = 0.0
x[0] = 0.0
v[0] = 0.0

# Addition
z = None

for i in range(0, n-1):
    a[i] = 5 - (0.34911*v[i]**2)/80
    v[i+1] = v[i] + a[i]*dt
    x[i+1] = x[i] + v[i]*dt

    # Addition
    if x[i+1] > 100 and z is None:
        z = t[i]
        print(f"Specified distance is reached at time point {z}")

plt.subplot(3,1,1)
plt.plot(t,x)
plt.xlabel('t [s]')
plt.ylabel('x [m]')
# Addition
plt.axvline(z, color='red')
plt.subplot(3,1,2)
plt.plot(t,v)
plt.xlabel('t [s]')
plt.ylabel('v [m/s]')
plt.subplot(3,1,3)
plt.plot(t,a)
plt.xlabel('t [s]')
plt.ylabel('a [m/s^2]')

plt.show()
plt.tight_layout()
Emil Osvald
  • 1,857
  • 2
  • 10
  • 8