Euler loop list
Question:
I want to plot a rocket trajectory with a loop using Euler’s method. My code is(you can skip this, the error is below it):
mo = 1000
m = 900
q = 2.8
u = 3700
vo = 0
xo = 0.0001
g = 9.81
t = np.arange(0,int(1001))
x0 = 0.0001
v0 = 0
N = 1000
at = 1/1000
Now velocity and height equations:
def v(t):
return u*np.log(mo/(mo-q*t))-g*t
def az(t):
return xo + vo*t - 0.5*g*t**2 + u*t*np.log(mo) + (u/q)*((mo - q*t)*np.log(mo
- q*t) + q*t - mo*np.log(mo))
z = xo + vo*t - 0.5*g*t**2 + u*t*np.log(mo) + (u/q)*((mo - q*t)*np.log(mo
- q*t) + q*t - mo*np.log(mo))
And here is the acceleration which I turned into a list because of the error:
aj = -g + (q/(mo-q*t))*u
ajj = np.array(aj).tolist()
So here is the loop where I am trying to solve it in an analytic way with Euler’s method:
t = np.zeros(N + 1)
t[0] = 0
z = np.zeros(N + 1)
z[0] = x0
v = np.zeros(N + 1)
v[0] = 0
for k in range(0,N):
v[k + 1] = v[k] +ajj*z[k]*at
z[k + 1] = z[k] + v[k + 1]*at
And it gives me this error:
ValueError Traceback (most recent call last)
<ipython-input-193-d1513ed52aa0> in <module>()
11
12 for k in range(0,N):
---> 13 v[k + 1] = v[k] + ajj*z[k]*at
14 z[k + 1] = z[k] + v[k + 1]*at
15
ValueError: setting an array element with a sequence.
I know what it barely means. The error is because of the list ajj but I don’t know how to fix it in order to plot z along the time. Thanks for helping. Sorry for the long question I’m just starting programming.
Answers:
Your ODE system is m'(t)=-q, az'(t)=v(t), v'(t)=u*q/m(t)-g
, here with inserting m(t)=m0-q*t
. It always helps to separate the system dynamics from the solver via an ODE function,
def ODEfunc(t,y): az, v = y; return np.array([ v, -g+q*u/(m0-q*t) ]);
so that you can then iterate the Euler steps as per cook book formula
t, y = t+h, y + h*ODEfunc(t,y)
I want to plot a rocket trajectory with a loop using Euler’s method. My code is(you can skip this, the error is below it):
mo = 1000
m = 900
q = 2.8
u = 3700
vo = 0
xo = 0.0001
g = 9.81
t = np.arange(0,int(1001))
x0 = 0.0001
v0 = 0
N = 1000
at = 1/1000
Now velocity and height equations:
def v(t):
return u*np.log(mo/(mo-q*t))-g*t
def az(t):
return xo + vo*t - 0.5*g*t**2 + u*t*np.log(mo) + (u/q)*((mo - q*t)*np.log(mo
- q*t) + q*t - mo*np.log(mo))
z = xo + vo*t - 0.5*g*t**2 + u*t*np.log(mo) + (u/q)*((mo - q*t)*np.log(mo
- q*t) + q*t - mo*np.log(mo))
And here is the acceleration which I turned into a list because of the error:
aj = -g + (q/(mo-q*t))*u
ajj = np.array(aj).tolist()
So here is the loop where I am trying to solve it in an analytic way with Euler’s method:
t = np.zeros(N + 1)
t[0] = 0
z = np.zeros(N + 1)
z[0] = x0
v = np.zeros(N + 1)
v[0] = 0
for k in range(0,N):
v[k + 1] = v[k] +ajj*z[k]*at
z[k + 1] = z[k] + v[k + 1]*at
And it gives me this error:
ValueError Traceback (most recent call last)
<ipython-input-193-d1513ed52aa0> in <module>()
11
12 for k in range(0,N):
---> 13 v[k + 1] = v[k] + ajj*z[k]*at
14 z[k + 1] = z[k] + v[k + 1]*at
15
ValueError: setting an array element with a sequence.
I know what it barely means. The error is because of the list ajj but I don’t know how to fix it in order to plot z along the time. Thanks for helping. Sorry for the long question I’m just starting programming.
Your ODE system is m'(t)=-q, az'(t)=v(t), v'(t)=u*q/m(t)-g
, here with inserting m(t)=m0-q*t
. It always helps to separate the system dynamics from the solver via an ODE function,
def ODEfunc(t,y): az, v = y; return np.array([ v, -g+q*u/(m0-q*t) ]);
so that you can then iterate the Euler steps as per cook book formula
t, y = t+h, y + h*ODEfunc(t,y)