Mll*_*che 12 python function python-2.7
我正在数值求解一阶微分方程组的x(t).该系统是:
dy/dt=(C)\*[(-K\*x)+M*A]
我已经实现了Forward Euler方法来解决这个问题,如下所示:这是我的代码:
import matplotlib
import numpy as np
from numpy import *
from numpy import linspace
from matplotlib import pyplot as plt
C=3
K=5
M=2
A=5
#------------------------------------------------------------------------------
def euler (f,x0,t):
n=len (t)
x=np.array ([x0*n])
for i in xrange (n-1):
x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )
return x
#---------------------------------------------------------------------------------
if __name__=="__main__":
from pylab import *
def f(x,t):
return (C)*[(-K*x)+M*A]
a,b=(0.0,10.0)
n=200
x0=-1.0
t=linspace (a,b,n)
#numerical solutions
x_euler=euler(f,x0,t)
#compute true solution values in equal spaced and unequally spaced cases
x=-C*K
#figure
plt.plot (t,x_euler, "b")
xlabel ()
ylabel ()
legend ("Euler")
show()
`
M=2
A=5
#----------------------------------------------------------------------------
def euler (f,x0,t):
n=len (t)
x=np.array ([x0*n])
for i in xrange (n-1):
x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )
return x
#---------------------------------------------------------------------------
if __name__=="__main__":
from pylab import *
def f(x,t):
return (C)*[(-K*x)+M*A]
a,b=(0.0,10.0)
n=200
x0=-1.0
t=linspace (a,b,n)
#numerical solutions
x_euler=euler(f,x0,t)
#compute true solution values in equal spaced and unequally spaced cases
x=-C*K
#figure
plt.plot (t,x_euler, "b")
xlabel ()
ylabel ()
legend ("Euler")
show()
Run Code Online (Sandbox Code Playgroud)
我得到了跟踪Traceback:
Traceback (most recent call last):
File "C:/Python27/testeuler.py", line 50, in <module>
x_euler=euler(f,x0,t)
File "C:/Python27/testeuler.py", line 28, in euler
x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )
IndexError: index 1 is out of bounds for axis 0 with size 1
Run Code Online (Sandbox Code Playgroud)
我不明白什么可能是错的.我已经解决了问题,但它对我没有帮助.你能找到我的错误吗?我使用以下代码作为方向:def euler(f,x0,t):
n = len( t )
x = numpy.array( [x0] * n )
for i in xrange( n - 1 ):
x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )
return x
if __name__ == "__main__":
from pylab import *
def f( x, t ):
return x * numpy.sin( t )
a, b = ( 0.0, 10.0 )
x0 = -1.0
n = 51
t = numpy.linspace( a, b, n )
x_euler = euler( f, x0, t )
Run Code Online (Sandbox Code Playgroud)
我的目标是绘制函数.
lee*_*dam 11
问题在于您的生产线
x=np.array ([x0*n])
Run Code Online (Sandbox Code Playgroud)
在这里,您将x定义为-200.0的单项数组.你可以这样做:
x=np.array ([x0,]*n)
Run Code Online (Sandbox Code Playgroud)
或这个:
x=np.zeros((n,)) + x0
Run Code Online (Sandbox Code Playgroud)
注意:你的输入很混乱.您在标头中导入numpy模块三次,然后导入pylab(已包含所有numpy模块).如果你想轻松一个单一的话
from pylab import *
Run Code Online (Sandbox Code Playgroud)
在顶部,你可以使用你需要的所有模块.
Spi*_*ine 10
正如Traceback所说,这个问题来自于这条线 x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] ).让我们在它的上下文中替换它:
i + 1 >= len(x)<=> i >= 0,元素x[i+1]就不存在了.这里,从for循环的开始就不存在这个元素.要解决此问题,您必须替换x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )为x.append(x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )).