Python返回曲线函数上方数组中的元素数

2024-09-27 21:30:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我的绘图中提取虚线、虚线和虚线曲线上方的值的数量。你知道吗

enter image description here

我可以随时观察超过这些曲线范围的点的数量,但我不想在这种方法中冒任何错误的风险。你知道吗

首先,这些曲线由我在代码中调用的质量变量和距离变量定义Escape_Velocity。这是我制作的班级简介的一部分。你知道吗

import numpy as np
import NFW_Profile as NFW

h0 = 0.704
NFWc12 = NFW.Profile(c=12.5, z=0.0, h=h0)
nfwEsc = NFWc12.Escape_Velocity(Mass, Radius)

在我的绘图中,曲线是基于这个函数的,在这里我选择一个任意的Mass值,而Radius被输入一个np.linspace值。你知道吗

图中的值来自我对数据集的分析,我们可以调用 RadVel是y轴值“径向速度”,而Dist是x轴值“银河中心距离”。你知道吗

关键的一点是,这两个元素数组的维数相同,它们可以相互索引。例如,第20个索引值RadVel对应于第20个索引值Dist。你知道吗


所以我的计划是从

  1. 选取Mass的任意值
  2. Dist中获取元素
  3. 将该值输入到Escape_Velocity
  4. 如果RadVel值(对应于馈送给Escape_Velocity的元素)大于Escape_Velocity值,则它将计数。你知道吗
  5. 如果Escape_Velocity大于相应的RadVel值,则不计算。你知道吗

    import numpy as np
    import NFW_Profile as NFW
    
    h0 = 0.704
    NFWc12 = NFW.Profile(c=12.5, z=0.0, h=h0)
    
    
    def UnboundSubhalos(mass, Dist):
        Vesc = NFWc12.Escape_Velocity(mass, Dist)
    
        PosValues = [i for i in RadVel if i => Vesc.all()]
        NegValues = [i for i in RadVel if i <= -Vesc.all()]
    
        return  len(PosValues) + len(NegValues)
    

Tags: import元素distasnpprofile曲线mass
1条回答
网友
1楼 · 发布于 2024-09-27 21:30:21

就我所知,你想找到在非线性函数定义的区域之外的红点的数量,你称之为Escape_Velocity,红点有x-位置和y-位置,分别存储在数组DistRedVal中,按顺序排列,这样红点的位置(x,y)=(Dist[n], RedVel[n])。另外,它看起来像(你对UnboundSubhalos的定义表明,Escape_Velocity定义的虚线是围绕RedVel = 0对称的,也就是说,你实际上想要找到绝对值RedVel大于Escape_Velocity的红点的数量。你知道吗

在这种情况下,您可以简单地执行以下操作:假设Escape_Velocity是一个可以向其传递数组的函数,您可以为数组Dist的每个元素找到Escape_Velocity,并将其存储在数组esc_vel = Escape_Velocity(Mass, Dist)。否则,必须使用Dist元素上的循环来计算这些值。从您的代码中,我可以假设Escape_Velocity给出一个正值,即RedVal正值处的虚线。然后数组outside_dots = np.abs(RedVel) >= esc_vel包含True对于虚线包围的区域之外的每个点,以及False对于内部的每个点。然后np.sum(outside_dots)给出所需的外部点数。你知道吗

以下代码的工作方式如下所述:

import numpy as np
import matplotlib.pyplot as plt

number_dots = 100
x = np.random.random(number_dots)  # random numbers in the range [0, 1]
y = (np.random.random(number_dots) - 0.5) * 2  # random numbers in the range [-1, 1]

curved_function = lambda z: 1 - np.sqrt(z)/3

plt.plot(x, y, 'ro')
plt.xlabel('x')
plt.ylabel('y')

def count_dots_outside(x, y):
        outside_dots = np.abs(y) > curved_function(x)
        return np.sum(outside_dots)

plt.title('Number of dots outside the curved function: '+str(count_dots_outside(x,y)))
xx = np.linspace(0,1,100)
plt.plot(xx, curved_function(xx), 'black', linestyle='dashed')
plt.plot(xx, -curved_function(xx), 'black', linestyle='dashed')
plt.show()

相关问题 更多 >

    热门问题