IndexError:索引1超出轴1的大小为1 / ForwardEuler的范围 [英] IndexError: index 1 is out of bounds for axis 0 with size 1/ForwardEuler
问题描述
我对一阶微分方程组的x(t)进行数值求解。该系统是:
$ b $
我已经实现了前向欧拉方法来解决这个问题,如下所示:
这是我的代码:
import matplotlib
从numpy导入导入numpy为np
* numpy导入linspace
从matplotlib导出
将pyplot输入为plt
C = 3
K = 5
M = 2
A = 5
#----- -------------------------------------------------- -----------------------
def euler(f,x0,t):
n = len(t)
x = np.array([x0 * n])
for 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)
#数字解决方案
x_euler = euler(f,x0,t)
#和不等距的情况
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。在xrange(n-1)中为i的阵列([x0 * n])
:
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):
返回(C)* [( - K * x)+ M * A]
a,b =(0.0,10.0)
n = 200
x0 = -1.0
t = linspace(a,b,n)
#数字解决方案
x_euler = euler(f,x0,t)
#以相等间隔和不等间距的情况计算真实解值
x = -C * K
#figure
plt.plot (t,x_euler,b)
xlabel()
ylabel()
legend(Euler)
show()
我得到以下Traceback:
Traceback(最近一次调用的最后一个):
在< module>文件中的C:/Python27/testeuler.py,第50行。
x_euler = euler(f,x0,t)
文件C:/Python27/testeuler.py,第28行,以euler
x [i + 1] = x [i] + (t [i + 1] - t [i])* f(x [i],t [i])
IndexError:索引1超出轴0的边界,大小为1
我不明白什么可能是错误的。我已经解决了问题后已经抬起头来,但它并没有帮助我。你能找到我的错误吗?
我使用以下代码作为方向:
def euler(f,x0,t):
$ $ $ $ $ $ $ $ > n = len(t)
x = numpy.array([x0] * n)
用于xrange(n - 1)中的i:
x [i + 1] = x [i] +(t [i + 1] - t [i])* f(x [i],t [i])
return x
if __name__ ==__main__:
$ 10.0)
x0 = -1.0
n = 51
t = numpy.linspace(a,b,n)
x_euler = euler(f,x0 ,t)
我的目标是绘制函数。
- x是一个等于[x0 * n]的数组,因此其长度为1
- 你从0到n-2(n在这里没有关系),我是索引。在开始时,一切都很好(这里显然没有开始...... :(),但是只要
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]))
。
I am numerically solving for x(t) for a system of first order differential equations. The system is:
dy/dt=(C)\*[(-K\*x)+M*A]
I have implemented the Forward Euler method to solve this problem as follows: Here is my code:
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()
I get following 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
I don´t understand what is probably wrong.I already looked up after solved questions, but it doesn´t help me along.Can you find my error? I am using following code as an orientation: 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 )
My goal is to plot the function.
The problem, as the Traceback says, comes from the line x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )
. Let's replace it in its context:
- x is an array equal to [x0 * n], so its length is 1
- you're iterating from 0 to n-2 (n doesn't matter here), and i is the index. In the beginning, everything is ok (here there's no beginning apparently... :( ), but as soon as
i + 1 >= len(x)
<=>i >= 0
, the elementx[i+1]
doesn't exist. Here, this element doesn't exist since the beginning of the for loop.
To solve this, you must replace x[i+1] = x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] )
by x.append(x[i] + ( t[i+1] - t[i] ) * f( x[i], t[i] ))
.
这篇关于IndexError:索引1超出轴1的大小为1 / ForwardEuler的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!