Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I WANT PYTHON CODE ONLY ((( hint ))) A car traveling along a straight road is cl

ID: 2902791 • Letter: I

Question

I WANT PYTHON CODE ONLY

((( hint )))

A car traveling along a straight road is clocked at a number of points. The data from the observations are given in the following table, where the time is in seconds, the distance is in feet, and the speed is in feet per second. Find a Hermite polynomial P7 with t0 = 0, t1 =5 and t2 = 8 and t3 = 13 to predict the position and speed of the car when t = 6 using corresponding data. Use a clamped cubic spline to predict the position of the car and its speed when t = 6. Use a natural cubic spline to predict the position of the car and its speed when t =6. Use the Hermite polynomial of position to approximate the speed of the car at t = 6. Use the clamped cubic spline of position to approximate the speed of the car at t = 6. Write a Python/Matlab code to check your answer. Try to use your best judgement to tell which one gives a better solution for the speed. To obtain the coefficients of the Hermite interpolating polynomial H(x) on the (n + 1) distinct numbers .x0 .xn for the function f: INPUT numbers x0, x1, xn; value f (x0), f(xn) and f(x0), f(xn). OUTPUT the numbers Q0,0, Q1,1, Q2n+1, 2n+1 where H(x) = Q0,0 + Q1,1 (x - x0) + Q2,2 (x - x0)2 + Q3,3 (x -x0)2 (x - x1) +Q4,4 (x - x0)2 (x - x1)2 + Q2n+1,2n+1 (x-x0)2(x-x1)2 (x - xn-1)2 (x - xn). Step 1 For i = 0, 1, n do Steps 2 and 3. Step 3 If i 0 then set Step 4 For i = 2, 3. 2n + 1 Step 5 OUTPUT (Q0,0, q1,1, Q2n+1, 2n+1);

Explanation / Answer

def test_function(expression):
  import sympy as sy

x = sy.Symbol('x')

f = eval(expression)

fx = sy.diff(f,x)

F = sy.lambdify(x,f,"numpy")
Fx = sy.lambdify(x,fx,"numpy")

  return F,Fx

Exact function and Hermite approximation with ni=7

if __name__ == '__main__':

  import matplotlib.pyplot as plt

ni = 7
nq = 40

  # Use Chebyshev-Gauss-Lobatto nodes for interpolation    
xi = -np.cos(np.arange(ni)*np.pi/(ni-1))

  # Legendre Gauss quadrature weights and nodes
xq,wq = L.leggauss(nq)

H,Hx,_ = hermite_interp(xi,xq)

f,fx = test_function("sy.tanh(3*x)")

  # Interpolating polynomial
p = np.dot(H[:,:ni],f(xi))+np.dot(H[:,ni:],fx(xi))

plt.plot(xq,f(xq),xq,p)
plt.show()

  # number of interpolation points on an element
ni = 2

  # number of quadrature points
nq = 20

# Number of elements
nel = 2

# degrees of freedom
dof = 2*ni*nel-2*(nel-1)

# Use Chebyshev-Gauss-Lobatto nodes for interpolation    
xi = -np.cos(np.arange(ni)*np.pi/(ni-1))

# Legendre Gauss quadrature weights and nodes
xq,wq = L.leggauss(nq)

# Rescale weights
wq /= 2

x2 = np.hstack(((xq-1)/2,(xq+1)/2))

# Construct interpolants on xi grid and evaluate them on xq grid
H,Hx,Hxx = hi(xi,xq)

# Reorder the basis functions
H = np.vstack([(H[:,k],H[:,k+ni]) for k in range(ni)]).T
Hx = np.vstack([(Hx[:,k],Hx[:,k+ni]) for k in range(ni)]).T*2
Hxx = np.vstack([(Hxx[:,k],Hxx[:,k+ni]) for k in range(ni)]).T*4

    # Elemental mass and stiffness matrices
Me = np.dot(H.T*wq,H)
Ke = np.dot(Hxx.T*wq,Hxx)

# Indices of ones in assembly matrix  
rows = range(2*nel*ni)
cols = [k-2*int(k/(2*ni)) for k in range(2*nel*ni)]  
e = np.ones(2*nel*ni)
A = csc_matrix((e,(rows,cols)))

    # Global stiffness matrix
K = A.T*np.kron(np.eye(nel),Ke)*A
  
f = np.zeros(dof)
f[-2*ni:] = np.dot(H.T,wq)

uhat = np.zeros(dof)
uhat[2:] = solve(K[2:,:][:,2:],f[2:])

H2 = np.kron(np.eye(nel),H)*A

u = np.dot(H2,uhat)

uex = exact_solution(x2)

plt.plot(x2,uex,'b-',lw=2)
plt.plot(x2,u,'r--',lw=2)
plt.show()

def test_function(expression):
  import sympy as sy

x = sy.Symbol('x')

f = eval(expression)

fx = sy.diff(f,x)

F = sy.lambdify(x,f,"numpy")
Fx = sy.lambdify(x,fx,"numpy")

  return F,Fx

Exact function and Hermite approximation with ni=7