1

import matplotlib.pyplot as plt
import numpy as np
import os
import sys
import time


MissionName = "Mars"
savename = "Mission"
start_time = time.time()
t = np.arange(0.0, 200.0, 10)
M0 = 2970000
mps = 12857.1429
mT = (mps * t)
m = (M0 - mT)
Fstuw = 35100000
a = Fstuw / m
for time in t:
 if time >= 50:
  vE = 0
for time in t:
 if time < 50:
  vE = 2580
h1 = (vE * M0/mps)
h2 = (1-(m / M0))
h3 = (np.log(M0 / m) / np.log(2.718281828)) + 1
h = h1 * h2 * h3
v = vE * (np.log(M0 / m) / np.log(2.718281828))

plt.plot(t,v)
plt.xlabel('time (s)')
plt.ylabel('Velocity (m/s)')
plt.title('Model raketmissie ' + str(MissionName))
plt.grid(True)
plt.savefig(savename + ".png")
plt.show()

Okay so the problem i have is that it does not change vE to 0 when the time is bigger or equal to 50, the result i get is this:

enter image description here

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264

1 Answers1

2

Your problem is in this part of the code:

for time in t:
    if time >= 50:
        vE = 0
for time in t:
    if time < 50:
        vE = 2580

Right now your vE is just a value, not a list or some other collection. You iterate twice. The first time you set vE=0, the second time you set it to vE=2580, overwriting the zero, you set before.

If you want a value for each timepoint, you could do something like:

vE=[0.0]*len(t)
for i, time in enumerate(t):
    if time < 50:
        v[i] = 2580.0

So you initialize a list of the same length as t with only zeros and change the value to 2580 for each element corresponding to a time <50.

An even better way, as suggested by Mad Physicist is to use a numpy array:

t = np.arange(0.0, 200.0, 10)
vE = np.zeros_like(t);
vE[t < 50] = 2580

or in one line

vE = np.where(t<50, 2580, 0)

So you don't have to use a loop to populate the list.

CodeZero
  • 1,649
  • 11
  • 18
  • yea but it checks if time >= 50, and it will only go to 2580 when it's smaller than 50, right? so how would i fix it? –  Feb 08 '18 at 07:57
  • 1
    vE is only one single value. I assume you want it to be a list, so have a value for each timepoint, right? – CodeZero Feb 08 '18 at 07:59
  • uhhh yes, that would be nice –  Feb 08 '18 at 08:00
  • 1
    Fix: `vE = np.zeros_like(time); vE[time < 50] = 2580` – Mad Physicist Feb 08 '18 at 08:07
  • 1
    You really shouldn't be slowly compiling a list when you can make a numpy array in one or two lines. I think you can use either `where` or `choice` to make a one liner. – Mad Physicist Feb 08 '18 at 08:10
  • okay i got 1 more question. say i'd want to make it increment again when t > 100, how would i do that? would i just add another condition? or what –  Feb 08 '18 at 10:46
  • Have a look at this link https://stackoverflow.com/questions/39109045/numpy-where-with-multiple-conditions/39109969 Basically you can use nested calls to np.where – CodeZero Feb 08 '18 at 10:51