简体   繁体   English

Python中的Runge-Kutta方法

[英]Method of Runge-Kutta in Python

I wrote a code about runge-kutta method in python, but every time when the program realizes any calculus the program require the differential equation. 我在python中编写了有关runge-kutta方法的代码,但是每次程序实现任何微积分时,程序都需要微分方程。

this is my code: 这是我的代码:

from math import *
import numpy as np

#Initial Values
n=input("Enter the number of equations n:")
n=int(n)
x=np.array([])
for i in range(0,n):
    x0=input("Enter the initial value of x{}:".format(i))
    x=np.append(x,[x0])
t=input("Enter the initial value of t:")
tf=input("Enter the final value of t:")
h=input("Enter the time interval h:")
m=int((tf-t)/float(h))

#Definition of differential equations
def f(t,x):
    f=np.array([])
    for i in range(0,n):
        f0=input("Enter the equation f{}:".format(i))
        f=np.append(f,[f0])
    return f


a=1.0/6
for i in range(0,m): 
    k1=f(t,x)
    k2=f(t+0.5*h,x+0.5*h*k1)
    k3=f(t+0.5*h,x+0.5*h*k2)
    k4=f(t+h,x+h*k3)
    x=x+a*h*(k1+2*(k2+k3)+k4)
    t=t+h

print t, x

Example using the equation dx/dt=x, x(0)=1, xf=1, h=0.1: 使用公式dx / dt = x,x(0)= 1,xf = 1,h = 0.1的示例:

Enter the number of equations n:1
Enter the initial value of x0:1
Enter the initial value of t:0
Enter the final value of t:1
Enter the time interval h:0.1
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
Enter the equation f0:x[0]
1.0 [ 2.71827974]

What should i do to enter only once the differential equation to program calculate all? 一旦编程要计算所有的微分方程,我该怎么做才能输入?

You have to move the the equation input code out of your function f(x,y) 您必须将方程式输入代码移出函数f(x,y)

Ask for the equations to be input in the same block as you are asking for all the other input. 要求将方程式输入到要输入所有其他输入的同一块中。

As it stands your code calls the function at every time interval so will ask for input at every time interval. 就目前而言,您的代码会在每个时间间隔调用该函数,因此会在每个时间间隔要求输入。

Correct me if I don't have understood. 如果我听不懂,请纠正我。 Are you trying to get an expression manually-typed by the user and treat that as a function? 您是否要获取用户手动键入的表达式并将其视为函数? I found this topic where it explains how to parse a formula using sympy. 我在该主题中找到了解释如何使用sympy解析公式的地方。 You can try to modify your program using sympy . 您可以尝试使用sympy修改程序。 In this manner you should be able to get your equation once for all. 通过这种方式,您应该能够一次获得方程式。

EDIT: if your problem is just "how to get the entire formula once from input..." you can just try to use the raw_input() method. 编辑:如果您的问题只是“如何从输入中获取整个公式一次...”,则可以尝试使用raw_input()方法。 Have also a look here : 在这里也看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM