让函数调用一个方程

1 投票
1 回答
44 浏览
提问于 2025-04-12 03:12

我正在创建一个函数,用于使用欧拉法来求解微分方程的近似解。我能让代码正常运行,但在尝试把它转换成一个函数时遇到了麻烦。具体来说,我在让我的函数正确调用公式时遇到了困难。

下面这段原始代码运行得很好:

#define your inital values
y = 1
x = 0
#h represents step size (incremental shift over the directional field)
h = 0.1

solutions = []
x_values = []

#generates a list of 10 solutions, increase the number in the range to get more solutions
for s in range (0, 10):
    #enter your differential equation 
    diff_eq = x+y
    #Euler's method to approximate the solutions
    s = y + h*(diff_eq)
    solutions.append(s)
    #replace initial values with the new values
    y = s
    x = x + h
    #create a list of the x values
    x_values.append(x)

#creating a dataframe from the solutions
Euler_values = pd.DataFrame(list(zip(x_values, solutions)), columns = ['xₙ', 'yₙ'])
#create an index for the dataframe starting at one and increasing by one 
Euler_values.index = Euler_values.index + 1
Euler_values.rename_axis('n', inplace = True)
Euler_values

这是将上面的代码转变为函数的开始:

#eulers method to approximate the solutions function
def eulers_method(x, y, diff_eq, h, n):
    #empty solution and x_value lists to be used in the euler function
    solutions = []
    x_values = []
    
    for s in range (0, n):
        # eq = diff_eq <-- this does not work
        # if I call the equation here it does work, but I want it to be entered into the function
        eq = x + y
        s = y + h*(eq)
        #replace initial values with the new values and adds them to the solution and x_value lists
        y = s
        solutions.append(s)
        x = x + h
        x_values.append(x)
    #creates a dataframe from the solutions
    Euler_values = pd.DataFrame(list(zip(x_values, solutions)), columns = ['xₙ', 'yₙ'])
    #creates an index for the dataframe starting at one and increasing by one 
    Euler_values.index = Euler_values.index + 1
    Euler_values.rename_axis('n', inplace = True)
    return Euler_values    

#enter an initial x value, initial y value, the differential equation, the step size, and the number of solutions to generate
# the diff_eq entry is giving me trouble
eulers_method(0,1, x+y, 0.1, 10)

1 个回答

2

解决你问题的方法有几种。第一种(我觉得更好)就是直接把一个函数传给你的 eulers_method 函数,也就是说:

def eulers_method(x, y, diff_eq, h, n):
    #empty solution and x_value lists to be used in the euler function
    solutions = []
    x_values = []
    
    for s in range (0, n):
        # compute the function
        eq = diff_eq(x, y)
        s = y + h*(eq)
        #replace initial values with the new values and adds them to the solution and x_value lists
        y = s
        solutions.append(s)
        x = x + h
        x_values.append(x)
    #creates a dataframe from the solutions
    Euler_values = pd.DataFrame(list(zip(x_values, solutions)), columns = ['xₙ', 'yₙ'])
    #creates an index for the dataframe starting at one and increasing by one 
    Euler_values.index = Euler_values.index + 1
    Euler_values.rename_axis('n', inplace = True)
    return Euler_values    

#enter an initial x value, initial y value, the differential equation, the step size, and the number of solutions to generate
eulers_method(0, 1, lambda x, y:x+y, 0.1, 10)

输出结果:

     xₙ        yₙ
n
1   0.1  1.100000
2   0.2  1.220000
3   0.3  1.362000
4   0.4  1.528200
5   0.5  1.721020
6   0.6  1.943122
7   0.7  2.197434
8   0.8  2.487178
9   0.9  2.815895
10  1.0  3.187485

第二种方法是传一个函数的字符串版本,然后在 eulers_method 函数里用 eval 来执行这个字符串,这种方法并不太推荐

def eulers_method(x, y, diff_eq, h, n):
    #empty solution and x_value lists to be used in the euler function
    solutions = []
    x_values = []
    
    for s in range (0, n):
        # compute the function
        eq = eval(diff_eq)
        s = y + h*(eq)
        #replace initial values with the new values and adds them to the solution and x_value lists
        y = s
        solutions.append(s)
        x = x + h
        x_values.append(x)
    #creates a dataframe from the solutions
    Euler_values = pd.DataFrame(list(zip(x_values, solutions)), columns = ['xₙ', 'yₙ'])
    #creates an index for the dataframe starting at one and increasing by one 
    Euler_values.index = Euler_values.index + 1
    Euler_values.rename_axis('n', inplace = True)
    return Euler_values    

#enter an initial x value, initial y value, the differential equation, the step size, and the number of solutions to generate
eulers_method(0, 1, 'x+y', 0.1, 10)

输出结果是一样的。

撰写回答