四阶龙格-库塔问题 f=alpha*y-beta*(y*y) with Python
Runge–Kutta of 4th order with the problem of f=alpha*y-beta*(y*y) with Python
所以我最近一直在一个项目中工作,该项目需要编写一个 python 程序,该程序作为输出给出一个 Runge-Kutta 四阶图,问题是 f=alpha*y-beta*(y*y)
。
谁能帮帮我,因为我发现我的程序有很多问题,而 YouTube 帮不上什么忙,
所以我的想法是将 y
值保存在列表中,然后从该列表中尝试使用 plot 绘制图形,但列表未显示
import numpy as np
from matplotlib import pyplot as plt
def f(x):
alpha = input("Enter the value of ⍺: ")
beta = input("Enter the value of β: ")
return alpha*y[0]-beta*(y[0]*y[0])
def runge_k(y, t, h):
list = []
i = 0
while i < t:
k1 = h*f(y)
t = t
k2 = h*f(y+k1/2)
t = t+h
k3 = h*f(y+k2/2)
t = t+h
k4 = h*f(y+k3)
t = t+h
y = y+(k1+2*k2+2*k3+k4)/6
list.append(y)
i=i+1
return list
H = float(input("Enter the value of H: "))
Y = float(input("Enter the first value of Y: "))
x = 100.0
l = runge_k(Y,100,H)
print(l)
大东西
现在您需要删除 t=t+h
命令,因为 t
是固定的最终时间,而 i
是需要更新的 运行 时间 i=i+h
。或者如果你想保持 i
整数,循环条件应该是 while i*h < t:
您不想在对 f
的数百次调用中每次都输入参数 alpha
和 beta
。无需将全局变量值作为参数传递即可使用。
小东西
使用像 list
这样的关键字作为变量名通常不是一个好主意。
如果不需要x=100.0
,请在最小示例中去掉
我只是将我的代码更改为这个,它显示了 y 列表,现在我只需要一个时间列表,我就可以绘制图表了:
import numpy as np
from matplotlib import pyplot as plt
def f(y):
return float(alpha) * float(y) - float(beta) * (float(y) * float(y))
def runge_k(y, t, h):
list_y = []
#list_t = []
i = 0
while i < t:
k1 = h * f(y)
k2 = h * f(y + k1 / 2)
k3 = h * f(y + k2 / 2)
k4 = h * f(y + k3)
y = y + (k1 + 2 * k2 + 2 * k3 + k4) / 6
list_y.append(y)
#t = t + h
i = i + 1
return list_y
alpha = input("Enter the value of ⍺: ")
beta = input("Enter the value of β: ")
H = float(input("Enter the value of H: "))
Y = float(input("Enter the first value of Y: "))
x = 100.0
l = runge_k(Y, 100, H)
print(l)
所以我最近一直在一个项目中工作,该项目需要编写一个 python 程序,该程序作为输出给出一个 Runge-Kutta 四阶图,问题是 f=alpha*y-beta*(y*y)
。
谁能帮帮我,因为我发现我的程序有很多问题,而 YouTube 帮不上什么忙,
所以我的想法是将 y
值保存在列表中,然后从该列表中尝试使用 plot 绘制图形,但列表未显示
import numpy as np
from matplotlib import pyplot as plt
def f(x):
alpha = input("Enter the value of ⍺: ")
beta = input("Enter the value of β: ")
return alpha*y[0]-beta*(y[0]*y[0])
def runge_k(y, t, h):
list = []
i = 0
while i < t:
k1 = h*f(y)
t = t
k2 = h*f(y+k1/2)
t = t+h
k3 = h*f(y+k2/2)
t = t+h
k4 = h*f(y+k3)
t = t+h
y = y+(k1+2*k2+2*k3+k4)/6
list.append(y)
i=i+1
return list
H = float(input("Enter the value of H: "))
Y = float(input("Enter the first value of Y: "))
x = 100.0
l = runge_k(Y,100,H)
print(l)
大东西
现在您需要删除 t=t+h
命令,因为 t
是固定的最终时间,而 i
是需要更新的 运行 时间 i=i+h
。或者如果你想保持 i
整数,循环条件应该是 while i*h < t:
您不想在对 f
的数百次调用中每次都输入参数 alpha
和 beta
。无需将全局变量值作为参数传递即可使用。
小东西
使用像 list
这样的关键字作为变量名通常不是一个好主意。
如果不需要x=100.0
,请在最小示例中去掉
我只是将我的代码更改为这个,它显示了 y 列表,现在我只需要一个时间列表,我就可以绘制图表了:
import numpy as np
from matplotlib import pyplot as plt
def f(y):
return float(alpha) * float(y) - float(beta) * (float(y) * float(y))
def runge_k(y, t, h):
list_y = []
#list_t = []
i = 0
while i < t:
k1 = h * f(y)
k2 = h * f(y + k1 / 2)
k3 = h * f(y + k2 / 2)
k4 = h * f(y + k3)
y = y + (k1 + 2 * k2 + 2 * k3 + k4) / 6
list_y.append(y)
#t = t + h
i = i + 1
return list_y
alpha = input("Enter the value of ⍺: ")
beta = input("Enter the value of β: ")
H = float(input("Enter the value of H: "))
Y = float(input("Enter the first value of Y: "))
x = 100.0
l = runge_k(Y, 100, H)
print(l)