对于赋值,我必须插入数据[线性插值和三次插值]并创建两个图形,将数据显示为点,将插值显示为一条线。我还必须绘制数据的最大值。我把所有这些都用到了工作,但被分配了最后一部分以获得额外的功劳,但无法理解。 V代表电压,I代表电流。
额外分配:扩展程序,以便使用称为“根查找”的不同方法确定最大电流和相应电压。要做到这一点,您需要在数字上区分功能。 (名为“diff”的numpy函数将很有用。)然后,为了执行根查找,您可以使用scipy.optimize库中的“brentq”函数。
from pylab import *
from scipy.interpolate import interp1d
from scipy.optimize import brentq
#load text into two variables V[Voltage] and I[Current]
V, I = loadtxt('data1(1).txt', unpack = True, skiprows = 1)
#voltage and current interpolated[linear]
f_line = interp1d(V, I, 'linear')
new_V = linspace(0, 12, 1000) #array of new voltages created
new_I = f_line(new_V) #array of new currents created from interpolated data
#voltage, current, new voltage, and new current plotted.
plot(V, I, 'ro', new_V, new_I, 'b-')
plot(max(new_I), 'go') #max current plotted
title("Linear Interpolation")
xlabel("Voltage (V)")
xlim(0,12)
ylabel("Current (mA)")
legend(['Data', 'linear Interp', 'Max Current'], loc = 'best')
show()
print "The maximum current is", max(new_I), "mA"
#voltage and current interpolated[cubic]
f_cube = interp1d(V, I, 'cubic')
new_V = linspace(0, 12, 1000) #array of new voltages created
new_I = f_cube(new_V) #array of new currents created from interpolated data
index = argmax(new_I) #index of max current
#voltage, current, new voltage, and new current plotted.
plot(V, I, 'ro', new_V, new_I, 'b-')
plot(new_V[index], max(new_I), 'go') #max current and voltage plotted
title("Cubic Interpolation")
xlabel("Voltage (V)")
xlim(0,12)
ylabel("Current (mA)")
ylim(0,1.4)
legend(['Data', 'linear Interp', 'Max Current'], loc = 'best')
show()
print "The maximum current is", max(new_I), "mA"
print "The corresponding maxium voltage is", new_V[index], "V"
以上所有代码都有效,但这是我不确定如何开始的最后一部分。我很感激任何建议或帮助。我确实尝试了它,但它抛出了一个错误,我真的不确定如何处理它,因为我不太了解正在使用的函数(diff,brentq),或者如何使用它们来查找最大电流和电压。
#load text into two variables V[Voltage] and I[Current]
V, I = loadtxt('data1(1).txt', unpack = True, skiprows = 1)
f_line = interp1d(V, I, 'linear')
new_V = linspace(0, 12, 1000) #array of new voltages created
d = diff(new_V, 1)
r = brentq(d)
print r
10 d = diff(new_V, 1)
11
---> 12 r = brentq(d)
13 print r
TypeError: brentq() takes at least 3 arguments (1 given)
我理解错误说的是什么,但我真的不知道给出它的正确参数,以便通过“查找根”找到最大电流和电压。任何建议都表示赞赏。