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

后端 未结 2 1450
长发绾君心
长发绾君心 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: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] )).

提交回复
热议问题