IndexError: index 1 is out of bounds for axis 0 with size 1/ForwardEuler

后端 未结 2 1439
长发绾君心
长发绾君心 2021-01-17 08:19

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 implement

相关标签:
2条回答
  • 2021-01-17 08:31

    The problem is with your line

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

    Here you define x as a single-item array of -200.0. You could do this:

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

    or this:

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

    Note: your imports are quite confused. You import numpy modules three times in the header, and then later import pylab (that already contains all numpy modules). If you want to go easy, with one single

    from pylab import *
    

    line in the top you could use all the modules you need.

    0 讨论(0)
  • 2021-01-17 08:45

    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 element x[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] )).

    0 讨论(0)
提交回复
热议问题