0

我需要使用 linspace 函数创建更高分辨率的绘图,但我不知道如何在我的代码中实现它。也许有人对此有更好的理解并可以帮助我。

import numpy as np
import matplotlib.pyplot as plt
N = np.array([1, 2, 3, 4])
c = np.array([1359,2136.6,2617.74,2630.16]) 
ct = c/1000                                            
ct0 = 103.8348/1000
cmax = 2630.16/1000
n = N.size
A = np.zeros(n)                                         
k = np.zeros(n)                                        
for j in range(0,n):
    A[j] = (ct[j] - ct0)/(cmax - ct0)
for j in range(0, n):
    if j < 3:
        k[j] = -(np.log(1 - A[j])) / N[j]
    else:
        k[j] = 1
MWk = np.mean(k)
Amod = np.zeros(n)
for j in range(0,n):
    Amod[j] = 1 - np.exp((-N[j]) * MWk)    
    
     
print(ct)
print(A)
print(k)
plt.xlabel("N")
plt.ylabel("Aufschlussgrad ")
plt.plot(N, A, "g", label = "Aufschlussgrad")
plt.plot(N, Amod, "k", label = "Modelfunktion")
plt.title("Hochdruckhomogenisator")
plt.legend()
plt.show()
4

1 回答 1

0

无需 interpolate Amod,因为它是您定义的函数。另一方面,有必要执行插值(在原始数据上A或在原始数据上c)以便向图中添加更多点。只有 4 或 5 个点,插值不会很有意义。在这种情况下,我选择插值 A。

代码没有从numpy's 数组中获利,所以我对其进行了一点 Python 化(它看起来像 C)

import numpy as np
import matplotlib.pyplot as plt

def Amod(x, MWk):
    return 1 - np.exp((-x) * MWk) 

def k(x, A):
    rv = -np.log(1 - A) / x
    rv[np.nonzero(A==1)] = 1
    return rv

# No real changes here: only a little 'pythonization'
N = np.array([1, 2, 3, 4])
c = np.array([1359,2136.6,2617.74,2630.16]) 
ct = c/1000                                            
ct0 = 103.8348/1000
cmax = 2630.16/1000
n = N.size
A = (ct - ct0) / (cmax-ct0)
MWk = np.mean(k(N, A))

print(ct)
print(A)
print(k)

# we now interpolate A
# numpy's interpolation is linear... it is not useful for this case
from scipy.interpolate import interp1d
# interp1d returns a function that interpolates the data we provide
# in this case, i choose quadratic interpolation
A_interp_fun = interp1d(N, A, 'quadratic')

# Let's increase the number of points
new_x = np.linspace(N[0], N[-1])

A_xtra = A_interp_fun(new_x)

plt.xlabel("N")
plt.ylabel("Aufschlussgrad ")
plt.scatter(N, A, label = "Aufschlussgrad - data")
plt.plot(new_x, A_xtra, "g", label="Aufschlussgrad - interpolation")
plt.scatter(N, Amod(N, MWk), label="Modelfunktion - data")
plt.plot(new_x, Amod(new_x, MWk), "k", label="Modelfunktion - function")
plt.title("Hochdruckhomogenisator")
plt.legend()
plt.show()
于 2022-01-07T21:33:09.430 回答