Responsive Navbar with Google Search
☰ Menu
Home
Python
LaTeX
GNUPlot
Arduino
Feedback
Contact Us
Introduction to Scipy: Interpolation
Introduction to Scipy
Integration
Interpolation
Linear Algebra
Optimization
Interpolation: Program 1
import numpy as np from scipy.interpolate import interp1d import matplotlib.pyplot as plt # Define the x and y data directly in the script (lists of length 20) x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) y = np.array([0, 1.5, 2.5, 3.8, 5.2, 7.5, 9.0, 12.1, 14.8, 15.6, 16.5, 18.3, 19.0, 20.5, 22.1, 23.4, 24.8, 25.2, 26.9, 28.1]) # Create an interpolating function (cubic interpolation) interp_func = interp1d(x, y, kind='linear') interp_func1 = interp1d(x, y, kind='cubic') # Generate new x values for interpolation x_new = np.linspace(min(x), max(x), 100) # Use the interpolating function to estimate y values at new x values y_new = interp_func(x_new) y_new1 = interp_func1(x_new) # Plot the original data points and the interpolated curve plt.scatter(x, y, color='red', label='Original Data (20 points)') plt.plot(x_new, y_new, label='Linear Interpolation', color='blue') plt.plot(x_new, y_new1, '--', label='Cubic Interpolation', color='red') plt.legend() plt.xlabel('x') plt.ylabel('y') plt.title('Interpolation with 20 Data Points') plt.savefig('plot.png')
Run Code
Output 1