将参数传递给Python中的函数

时间:2022-12-05 19:12:08

I started programming two weeks ago, so I realize this might be a stupid question. I want to find the y values for the t values in the interval [0,25], without using a for loop. I want a list of y values, but instead I get <function y at 0x01D4D5D0>:

我两个星期前开始编程,所以我意识到这可能是一个愚蠢的问题。我想在区间[0,25]中找到t值的y值,而不使用for循环。我想要一个y值列表,但我得到 <函数y在0x01d4d5d0> :

from math import cos, e, sqrt
import numpy as np
m = 9          
A = 0.3         
k = 4          
gamma = 0.15

t_array = np.linspace(0,25)
y_array = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
def y(t_array):
    y = []
    y.append(A*(e**(-gamma)**(t_array))*cos((sqrt(k/m))*(t_array)))
print(y)

2 个解决方案

#1


1  

Your original problem arises due to the fact that you don't call the function you define. You cannot expect a function to know what arguments it must work with. Python is procedural; you must pass those arguments yourself.

由于您没有调用您定义的函数,因此产生了原始问题。您不能指望函数知道它必须使用哪些参数。 Python是程序性的;你必须自己传递这些论点。

Alternatively (and I recommend this), you could just use numpy's ufuncs and vectorise everything:

或者(我推荐这个),你可以使用numpy的ufuncs并将所有东西都矢量化:

y_array = A * (np.exp(-gamma) ** (t_array)) * np.cos((np.sqrt(k / m)) * (t_array))

print(y_array)
array([ 0.3       ,  0.26197643,  0.20012117,  0.12471753,  0.04610095,
       -0.02650152, -0.08584334, -0.12718585, -0.14847109, -0.1501709 ,
       -0.13487525, -0.10670268, -0.07062388, -0.03178614,  0.00508595,
        0.03615761,  0.05878548,  0.07164338,  0.07468336,  0.06895966,
        0.05635461,  0.03925115,  0.0201959 ,  0.00159182, -0.01454951,
       -0.02677677, -0.03428127, -0.03689604, -0.0350233 , -0.02950888,
       -0.02148485, -0.01220259, -0.00287629,  0.0054473 ,  0.01198185,
        0.0162522 ,  0.01810327,  0.0176719 ,  0.01533013,  0.01161037,
        0.00712318,  0.00247811, -0.00178419, -0.00524252, -0.00762514,
       -0.00881889, -0.00885931, -0.00790559, -0.00620522, -0.00405386]

Disclaimer: I cannot verify this answer because you have not explained what you are trying to do. This should hopefully get you started in the right direction working with vectorisation.

免责声明:我无法验证此答案,因为您没有解释您要做的事情。这应该有助于您开始使用矢量化的正确方向。

#2


0  

i think the problem is that you have a function called 'y' and printing y prints the function location. Also the function y is not ran only defined and it does not return anything as well. So to print the newly created y list inside the y function you can do:

我认为问题是你有一个名为'y'的函数,而打印y打印功能位置。此外,函数y不仅仅定义了运行,也没有返回任何内容。因此,要在y函数内打印新创建的y列表,您可以执行以下操作:

def y(t_array):
    y_values = []
    y_values.append(A*(e**(-gamma)**(t_array))*cos((sqrt(k/m))*(t_array)))
    return y_values
print(y(t_array))

#1


1  

Your original problem arises due to the fact that you don't call the function you define. You cannot expect a function to know what arguments it must work with. Python is procedural; you must pass those arguments yourself.

由于您没有调用您定义的函数,因此产生了原始问题。您不能指望函数知道它必须使用哪些参数。 Python是程序性的;你必须自己传递这些论点。

Alternatively (and I recommend this), you could just use numpy's ufuncs and vectorise everything:

或者(我推荐这个),你可以使用numpy的ufuncs并将所有东西都矢量化:

y_array = A * (np.exp(-gamma) ** (t_array)) * np.cos((np.sqrt(k / m)) * (t_array))

print(y_array)
array([ 0.3       ,  0.26197643,  0.20012117,  0.12471753,  0.04610095,
       -0.02650152, -0.08584334, -0.12718585, -0.14847109, -0.1501709 ,
       -0.13487525, -0.10670268, -0.07062388, -0.03178614,  0.00508595,
        0.03615761,  0.05878548,  0.07164338,  0.07468336,  0.06895966,
        0.05635461,  0.03925115,  0.0201959 ,  0.00159182, -0.01454951,
       -0.02677677, -0.03428127, -0.03689604, -0.0350233 , -0.02950888,
       -0.02148485, -0.01220259, -0.00287629,  0.0054473 ,  0.01198185,
        0.0162522 ,  0.01810327,  0.0176719 ,  0.01533013,  0.01161037,
        0.00712318,  0.00247811, -0.00178419, -0.00524252, -0.00762514,
       -0.00881889, -0.00885931, -0.00790559, -0.00620522, -0.00405386]

Disclaimer: I cannot verify this answer because you have not explained what you are trying to do. This should hopefully get you started in the right direction working with vectorisation.

免责声明:我无法验证此答案,因为您没有解释您要做的事情。这应该有助于您开始使用矢量化的正确方向。

#2


0  

i think the problem is that you have a function called 'y' and printing y prints the function location. Also the function y is not ran only defined and it does not return anything as well. So to print the newly created y list inside the y function you can do:

我认为问题是你有一个名为'y'的函数,而打印y打印功能位置。此外,函数y不仅仅定义了运行,也没有返回任何内容。因此,要在y函数内打印新创建的y列表,您可以执行以下操作:

def y(t_array):
    y_values = []
    y_values.append(A*(e**(-gamma)**(t_array))*cos((sqrt(k/m))*(t_array)))
    return y_values
print(y(t_array))