简体   繁体   English

自变量范围内的最大值和最小值

[英]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)),介于X6 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)),介于X2 and 4之间。 However, there is another minimum between the X values 8 and 10 .但是, X8 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.argsorthttps://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.

相关问题 打印出列表中某个范围内的最大值/最小值 - Print out the maximum/minimum within a range in a list 如何在Python中从最大范围绘制到最小范围 - How can I plot from a maximum range to a minimum range in Python 有没有办法在 PySimpleGui spinboxes 中限定 integer 输入范围的最小值和最大值,以便最小值不会超过最大值? - Is there any way to delimit minimum and maximum of an integer input range in PySimpleGui spinboxes, so that minimum doesn't surpases maximum? Python DataFrame:根据最小和最大范围添加两列? - Python DataFrame: Add two columns based on minimum and maximum range? 获取DataFrame中特定日期范围内的最小值和最大值 - Get the Minimum and Maximum value within specific date range in DataFrame 在 TXT 文件的每一行中查找字符数的最小值、最大值和范围 - Finding minimum, maximum and range of character count in every line of a TXT file 最大独立设定重量 - maximum independent set weight 在优雅地处理字符串列的同时找到所有列的范围(最大值和最小值之间的差异) - Find the range of all columns (difference between maximum and minimum) while gracefully handling string columns 在范围之间创建随机的整数列表,并在列表中找到平均值,最小值和最大值 - Create a random list of integers between range, and find average, minimum and maximum value in list 最大,最小和总数 - Maximum, minimum and total numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM