微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有 scipy.interpolate.RBFInterpolator 的 Python 2D 插值

如何解决带有 scipy.interpolate.RBFInterpolator 的 Python 2D 插值

上周我问了一个问题,关于找到一种从多条曲线(来自多个 Excel 文件的数据)插入曲面的方法,有人向我推荐了一个问题,该问题解释了如何使用 scipy.interpolate.RBFInterpolator (How can I perform two-dimensional interpolation using scipy? ).

我试过这种方法,但我得到了一个糟糕的表面拟合(见下图)。有谁明白我的代码有什么问题?我试图更改内核参数,但“线性”似乎是最好的。我在使用 np.meshgrid 时出错了吗?感谢您的帮助。

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
from scipy.interpolate import RBFInterpolator



fig = plt.figure(figsize=(15,10),dpi=400)
ax = fig.gca(projection='3d')

# List all the results files in the folder (here 'Sress_Strain') to plot them. 
results_list = os.listdir(r"C:/Users/bdhugu/Desktop/Strain_Stress")

for i in range(len(results_list)):
    if i == 0:
        
        results = pd.read_excel(r"C:/Users/bdhugu/Desktop/Strain_Stress/"+results_list[i])
        strain = results["Strain (mm/mm)"]
        stress = results["Stress (MPa)"]
        strain_rate = results["Strain rate (s^-1)"]
    
    if i>0:
        
        new_results = pd.read_excel(r"C:/Users/bdhugu/Desktop/Strain_Stress/"+results_list[i])
        new_strain = new_results["Strain (mm/mm)"]
        new_stress = new_results["Stress (MPa)"]
        new_strain_rate = new_results["Strain rate (s^-1)"] 
        
        strain = strain.append(new_strain,ignore_index=False)
        stress = stress.append(new_stress,ignore_index=False)
        strain_rate = strain_rate.append(new_strain_rate,ignore_index=False)

# RBFINTERPOLATOR METHOD

# ----------------------------------------------------------------------------

 
x_scattered = strain
y_scattered = strain_rate
z_scattered = stress
scattered_points = np.stack([x_scattered.ravel(),y_scattered.ravel()],-1)
x_dense,y_dense = np.meshgrid(np.linspace(min(strain),max(strain),20),np.linspace(min(strain_rate),max(strain_rate),21))
dense_points = np.stack([x_dense.ravel(),y_dense.ravel()],-1)
interpolation = RBFInterpolator(scattered_points,z_scattered.ravel(),smoothing = 0,kernel='linear',epsilon=1,degree=0)
z_dense = interpolation(dense_points).reshape(x_dense.shape)

fig = plt.figure(figsize=(15,dpi=400)
ax = plt.axes(projection='3d')

ax.plot_surface(x_dense,y_dense,z_dense,cmap='viridis',edgecolor='none')
ax.invert_xaxis()
ax.set_title('Surface plot')
plt.show()

Data to interpolate

Surface fitting with RBFInterpolator

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。