成对减去数组的行并在for循环中分割

时间:2018-06-11 10:04:03

标签: python-3.x

我有三个数组叫做flow_stress(4x3)和strain_rate(1x4)和T(1x3)。我已经插入了flow_stress的日志,相对于[Temp = 1000 /(T + 273),这是行间隔为50个术语]和srate(它是应变率的对数线间隔为100个术语),使得flow_stress3是一个(100x50)的数组 我正在尝试创建一个数组 m ,它等于(flow_stress3的连续行的差异)除以(srate的连续项的差异)。 虽然数组flow_stress3和数组srate具有正确的值。 m的值是错误的。

>import numpy as np

>import math

>from scipy import interpolate as sp

>import matplotlib as plt

>T = np.array([750,800,850])           
  ## Enter temperature value in degree##

>strain_rate = np.array([0.0003,0.001,0.01,0.1])   

>flow_stress = np.array([[95.96, 49.46,28.16],\
                        [126.62,80.51,46.45],\
                        [235.14,151.46,107.94],\
                        [319.15,228.77,165.63]])


>Temp = 1000/(273+T)

>k = (max(T)-min(T))/2

>TT = np. linspace(max(Temp), min(Temp),k)          
 ## divide the temp into k##

>S = np.log10(flow_stress)

>flow_stress1 = np.empty(shape=[len(strain_rate),len(TT)]) 

 ## makes an empty array of dim ##

>(len(strain_rate),len(TT))

>SR= np.log10(strain_rate)

>n = (max(SR)-min(SR))/0.025        
 ## divide SR by 0.025 to get number of terms in matrix##

>l = n//1                           
 ## operator // converts the fraction into integer##

>srate= np.linspace(min(SR), max(SR),l)     

## divides SR into l equal no of parts##

>len_srate = len(srate)

>for i in range(len(strain_rate)):          
 ## first interpolate between temp and log flow stress ##
 >>   f_linear = sp.interp1d(Temp,S[i,:])
 >>  flow_stress1[i,:] = f_linear(TT)                
 ## interpolate at values given by TT ##

>flow_stress2 = np.empty(shape=[len(TT),len(srate)])

>for i in range(len(TT)):
>>f_linear = sp.interp1d(SR,flow_stress1[:,i])
>>flow_stress2[i,:] = f_linear(srate)

>flow_stress3 = flow_stress2.T

>print(len(flow_stress3))
>print(len(flow_stress3[0,:]))
>print(len(srate))
>print(len(TT))

>srate = srate.T


>m = np.zeros(shape=[len(srate),len(TT)],dtype=np.ndarray)

>for i in range(len(srate)-1):
>>m[i,:]= np.array((flow_stress3[i+1,:]-flow_stress3[i,:])/(srate[i+1]-srate[i]))

>m[len(srate)-1,:] = m[len(srate)-2,:]

我得到m相对于srate和T的等高线图,如图1所示。 在MatLab中完成相同数据的图也显示在下图2中。我们确信MatLab数据是正确的。使用Python,可以看出许多原始数据和列的值是相同的,不应该是这种情况。 fig1

fig2

1 个答案:

答案 0 :(得分:2)

如果我理解正确,你的要求只是计算flow_stress3 wrt srate的一阶导数。代码看起来相当复杂。特别是,我不明白最后一行的用途是什么。

由于你已经在使用scipy,我建议使用UnivariateSpline function。您的代码将缩小为:

from scipy.interpolate import UnivariateSpline

splinecoeff=UnivariateSpline(srate,flow_stress3)
m=splinecoeff.derivative()