0
votes

Hi I want to plot a rocket trajectory and it gives me this error: float() argument must be a string or a number, not 'function'. I want to plot the whole trajectory of a rocket that is losing mass to gain thrust. When the fuel ends, it describes a parabolic trajectory. Data of the problem can be changed. Those are the values I put, where mo is the initial mass of the rocket, q is the flow of gasses (how mass changes over time), g is gravitational acceleration, xo is the initia position, and t is time.

My code is:

    import math
    import numpy as np
    import matplotlib.pyplot as plt
    %matplotlib inline

Data:

    mo = 1500
    q = 2.5
    u = 6000
    vo = 0
    g = 9.8
    x0 = 0
    t = np.arange(0,1001)
    t

velocity ecuation:

def v(t):
    return vo + u*(math.log(mo/(mo-q*t))-g*t)

Position ecuation:

def x(t):
    return x0 + vo*t - 0.5*g*t^2 + u*t*math.log(mo) + (u/q)*((mo - q*t)*math.log(mo - q*t) + q*t - mo*math.log(mo))

 for t in (0,100):
 plt.plot(x,t)
 plt.grid()

Thanks for helping me, I really appreciate it.

2

2 Answers

0
votes

There are four problems.

  • you need to call a function, not state it, x(t).
  • Don't use math when working with arrays. Instead use numpy.
  • A power in python is written as **, not ^.
  • Don't use negative values inside of logarithms.

The correct code may then look like:

import numpy as np
import matplotlib.pyplot as plt


mo = 1500
q = 2.5
u = 6000
vo = 0
g = 9.8
x0 = 0
t = np.arange(0,int(1500/2.5))

def v(t):
    return vo + u*(np.log(mo/(mo-q*t))-g*t)

def x(t):
    return x0 + 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))


plt.plot(x(t),t)
plt.grid()

plt.show()

producing

enter image description here

0
votes

It should be

plt.plot(x(t), t)

instead of

plt.plot(x, t)

What you're doing above is treating each (x,y) as a set of data. This isn't correct, since it's rather the collection of ((0, x(0)), (1, x(1))...) that is your set of data. A readable way is to have an array for your x-axis and y-axis:

x_ = np.arange(0,100)
y_ = x(x_) # available in newer versions of numpy.

plt.plot(x_, y_)