我正在创建一个函数来创建一个微分方程的近似解,使用欧拉方法. 我可以让代码工作,但遇到了麻烦时,试图将其转换为一个函数. 也就是说,我很难让我的函数正确调用公式.

This original code worked well:

#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

This is the start of turning the above code into a function:

#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)

推荐答案

有几种方法可以解决你的问题.第一个(IMO更可取的)解决方案是只是传递一个函数到你的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

第二种方法,and not really desirable是传递函数的字符串版本,并在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)

输出是一样的.

Python相关问答推荐

Python 3.12中的通用[T]类方法隐式类型检索

具有多个选项的计数_匹配

根据不同列的值在收件箱中移动数据

有症状地 destruct 了Python中的regex?

优化pytorch函数以消除for循环

pandas:排序多级列

在Django admin中自动完成相关字段筛选

如何在turtle中不使用write()来绘制填充字母(例如OEG)

Python列表不会在条件while循环中正确随机化'

将scipy. sparse矩阵直接保存为常规txt文件

Pandas:计算中间时间条目的总时间增量

python sklearn ValueError:使用序列设置数组元素

在Python中从嵌套的for循环中获取插值

Polars map_使用多处理对UDF进行批处理

浏览超过10k页获取数据,解析:欧洲搜索服务:从欧盟站点收集机会的微小刮刀&

在pandas中,如何在由两列加上一个值列组成的枢轴期间或之后可靠地设置多级列的索引顺序,

Polars时间戳同步延迟计算

Pythonquests.get(Url)返回Colab中的空内容

仅取消堆叠最后三列

在不中断格式的情况下在文件的特定部分插入XML标签