[英]Maximum and minimum in a range of independent variable
Based on the answer from @Peaceful James, I am attempting to reduce the confusion.根据@Peaceful James 的回答,我试图减少混淆。 Thus, editing the question.
因此,编辑问题。
Edited已编辑
I am trying to find a maximum (and minimum) in the range of an independent variable, ie X .我试图在自变量的范围内找到最大值(和最小值),即X 。 My code looks like the following.
我的代码如下所示。 Note, this is just a representative function.
注意,这只是一个代表 function。
import numpy as np
import matplotlib.pyplot as plt
from pandas import *
X = np.arange(2, 11, 0.2)
Z = np.zeros((len(X),1))
for i in range(0,len(X)):
Z[i] = 0.1*np.sin(X[i]-5)
print(DataFrame(Z))
A = np.argmax(Z, axis = 0)
B = np.argmin(Z, axis = 0)
C = print("Maximum =",Z[A[0]])
D = print("Minimum =", Z[B[0]])
plt.plot(X,Z,'r-', linewidth = 2)
plt.xlabel('$X$ (-)')
plt.ylabel('$Z$ (-)')
1: A = np.argmax(Z, axis = 0)
the maximum is 0.09995736 (index: (23,0)) which is between the X values 6 and 8
. 1:
A = np.argmax(Z, axis = 0)
最大值为0.09995736 (索引:(23,0)),介于X值6 and 8
之间。
2: A = np.argmin(Z, axis = 0)
the min is -0.09995736 (index: (7,0)) which is between the X values 2 and 4
. 2:
A = np.argmin(Z, axis = 0)
最小值为-0.09995736 (索引:(7,0)),介于X值2 and 4
之间。 However, there is another minimum between the X values 8 and 10
.但是, X值
8 and 10
之间还有另一个最小值。 I am wondering if there is a way to pass some kind of upper and lower limit values of X to np.argmin
(or to similar command) to get the second minimum of function Z .我想知道是否有办法将某种上下限值X传递给
np.argmin
(或类似命令)以获得 function Z的第二个最小值。
Any help is appreciated.任何帮助表示赞赏。 Thanks !
谢谢 !
Use numpy.argsort
: https://numpy.org/devdocs/reference/generated/numpy.argsort.html使用
numpy.argsort
: https://numpy.org/devdocs/reference/generated/numpy.argsort.html
import numpy as np
X = np.arange(2, 11, 0.2)
Z = np.zeros((len(X),1))
for i in range(0,len(X)):
Z[i] = 0.1*np.sin(X[i]-5)
C = np.argsort(Z, axis=0)
C = C.flatten() # flatten because it is currently an array of 1-dim arrays.
print("Maximum =",Z[C[-1]])
print("Second Maximum =",Z[C[-2]])
print("Second Minimum =",Z[C[1]])
print("Minimum =",Z[C[0]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.