Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Chapter 2

February 2, 2021

[1]: import numpy as np


import matplotlib.pyplot as plt
from sympy import *
from chebpy import chebfun
from numpy import trapz

%matplotlib inline

1 Problem 1

The velocity of an object is given by


vx (t) = e−t
2 /100
(t + 10 sin(πt))
Both vx and x are zero when t = 0. (a) Numerically find the first time when vx equals zero after
the start and find the distance traveled from the origin. (b) Find the final position of the object as
t → ∞.
[2]: def vx(t):
return np.exp(-t**2/100)*(t + 10*np.sin(np.pi*t))

[3]: def xx(t):


return -t*(t + 10*np.sin(np.pi*t))*np.exp(-t**2/100)/50 + \
(10*np.pi*np.cos(np.pi*t) + 1)*np.exp(-t**2/100)

[4]: # first zero


f = vx(chebfun('x', [0, 20]))
r = f.roots()
print(r[1])

1.0329381949400263

[5]: t = np.linspace(0, 20, 10000)

fig, ax = plt.subplots(figsize=(12,6))

plt.plot(t, vx(t), label="$v_x(t)$")

1
plt.plot(t, xx(t), label="$x(t)$")
plt.plot(r, [0 for _ in range(len(r))], 'ro')

plt.title("Position and velocity as a function of time")


plt.xlabel('t')

ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

plt.legend()
plt.grid()

plt.show()

[6]: # total distance traveled during that time


idx = (np.abs(t-r[1])).argmin()
print(trapz(np.abs(xx(t)[:idx])))

10434.34685215483

[7]: x = Symbol('x')
V = exp(-x**2/100)*(x + 10*sin(pi*x))
X = diff(V)
print(X)

-x*(x + 10*sin(pi*x))*exp(-x**2/100)/50 + (10*pi*cos(pi*x) + 1)*exp(-x**2/100)

2
[8]: # final position and velocity as t goes to infinity
limX = limit(X, x, oo, '+').evalf()
limV = limit(V, x, oo, '+').evalf()
print("limit of x as t tends to infinity:", limX)
print("limit of v as t tends to infinity:", limV)

limit of x as t tends to infinity: 0


limit of v as t tends to infinity: 0

[ ]:

You might also like