Question

I need to plot the position of a particle at time t, given the following formulae: s(t) = -0.5*g(s)*t^2+v0*t, where g(s) = G*M/(R+s(t))^2 (G, M, and R are constants, s being a value, not the function s(t)). The particle is being shot up vertically, and I want to print its current position every second until it hits the ground. But I can't figure out how to define one function without using the other before it's defined. This is my code so far:

G =  6.6742*10^(-11)
M = 5.9736*10^24
R = 6371000
s0 = 0
v0 = 300
t = 0
dt = 0.005

def g(s):
    def s(t):
        s(t) = -0.5*g(s)*t^2+v0*t
    g(s) = G*M/(R+s(t))^2


def v(t):
    v(t) = v(t-dt)-g(s(t-dt))*dt


while s(t) >= 0:
    s(t) = s(t-dt)+v(t)*dt
    t = t+dt

if t == int(t):
    print s(t)

When I run the function, it says that it can't assign the function call.

Was it helpful?

Solution

The error means that you can't write s(t) = x, because s(t) is a function, and assignment on functions is performed with def .... Instead, you'll want to return the value, so you'd rewrite it like this:

def g(s):
    def s(t):
        return -0.5*g(s)*t^2+v0*t
    return G*M/(R+s(t))^2

However, there are other issues with that as well. From a computational standpoint, this calculation would never terminate. Python is not an algebra system and can't solve for certain values. If you try to call s(t) within g(s), and g(s) within s(t), you'd never terminate, unless you define a termination condition. Otherwise they'll keep calling each other, until the recursion stack is filled up and then throws an error.

Also, since you defined s(t) within g(s), you can't call it from the outside, as you do several times further down in your code.

You seem to be confused about several syntax and semantic specifics of Python. If you ask us for what exactly you'd like to do and provide us with the mathematical formulae for it, it might be easier to formulate an answer that may help you better.

Edit:

To determine the position of a particle at time t, you'll want the following code (reformatted your code to Python syntax, use ** instead of ^ and return statements):

G = 6.6742*10**(-11)
M = 5.9736*10**24
R = 6371000
s0 = 0
v0 = 300
t = 0
dt = 0.005

sc = s0 # Current position of the particle, initially at s0

def g(s):
    return -G*M/(R+s)**2

def s(t):
    return 0.5*g(sc)*t**2 + v0*t + s0

count = 0
while s(t) >= 0:
    if count % 200 == 0:
        print(sc)
    sc = s(t)
    count += 1
    t = dt*count

OTHER TIPS

Python functions can call each other, but that's not how a function returns a value. To make a function return a particular value, use return, e.g.,

def v(t):
   return v(t - dt) - g(s(t - dt)) * dt

Furthermore, I don't really understand what you're trying to do with this, but you'll probably need to express yourself differently:

while s(t) >= 0:
    s(t) = s(t-dt)+v(t)*dt
    t = t+dt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top