Homework 20¶
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.integrate import quad
from scipy.interpolate import interp1d
Problem 1¶
Given the data x and y below. Find the linear interpolate yi corresponding to xi=0.54. Also find the cubic spline (read the notes) value of y at the same point. The xg and yg values are computed from $y=f(x)=\exp(4x)$. Find the relative error between your interpolants and the exact value. The relative error is given by $\epsilon = |(y-y_{exact})/y_{exact}|$. Are you happy with the result?
x = np.array([0,0.2,0.4,0.8,1.0])
y = np.exp(4.0*x)
xi = 0.54
yexact = np.exp(4.0*xi)
Problem 2¶
Data from a turbulent jet simulation is given in file hw_20_data.txt.
Download this data into a file called hw_20_data.txt and load it into a matrix.
Plot the column labeled
posversus the column labeleduvel(that is, column 0 versus column 4).- Label the axes as "position (m)" and "velocity (m/s)".
The x-data values are not uniform because this simulation was performed on an adaptive grid. However, for convenient data processing, we want the data on a uniform grid.
Find the minimum grid point spacing (the minimum spacing between x points). Then make a uniform grid of x points with that spacing.
Then, interpolate the original uvel array to the new uniform grid. Use linear interpolation.
On a new plot, plot the interpolated uvel versus position.