2015-05-05 73 views
6

我对数值求解x(t)的一阶微分方程组。该系统是: 这里是我的代码:IndexError:索引1超出轴1的大小1/ForwardEuler

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

我获得以下回溯:

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 

dy/dt=(C)\*[(-K\*x)+M*A]

我已经实现了向前欧拉方法如下解决这个问题

我不明白什么可能是错误的。我已经解决了问题后,已经抬头看,但它并没有帮助我。你可以找到我的错误? 我使用下面的代码为取向: 高清欧拉(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) 

我的目标是绘制功能。

+1

'x = np.array([x0 * n])'产生一个元素的数组。你想做什么? – mdurant

回答

5

这个问题,正如Traceback所说的,来自x[i+1] = x[i] + (t[i+1] - t[i]) * f(x[i], t[i])这一行。让我们来取代它在它的上下文:

  • x等于[X0 * N]数组,因此它的长度是1
  • 你迭代从0到n-2(N此处无关紧要),我是索引。在一开始,一切都很好(这里没有开始明显...... :(),但只要i + 1 >= len(x) < =>i >= 0,元素x[i+1]不存在。在这里,这个元素自从开始就不存在。for循环

为了解决这个问题,必须通过x.append(x[i] + (t[i+1] - t[i]) * f(x[i], t[i]))更换x[i+1] = x[i] + (t[i+1] - t[i]) * f(x[i], t[i])

9

问题是伴您行

x=np.array ([x0*n]) 

在这里定义x作为一个单项阵列 - 200.0。你可以这样做:

x=np.array ([x0,]*n) 

或本:

x=np.zeros((n,)) + x0 

注:您的进口量相当混乱。您在头文件中导入numpy模块三次,然后再导入pylab(已包含所有numpy模块)。如果你想变得容易,只需一个单一的

from pylab import * 

行在顶部,你可以使用所有你需要的模块。

相关问题