Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could use a list comprehension: </p> <pre><code>def position(n): v=velocity(n) return [v*i for i in range(n)] </code></pre> <p>Or, since you are using numpy:</p> <pre><code>v=np.array([1,2,3]) # array([1, 2, 3]) </code></pre> <p>you can use numpy broadcasting to express the entire calculation in one blow:</p> <pre><code>i=np.arange(5) # array([0, 1, 2, 3, 4]) v[:]*i[:,np.newaxis] # array([[ 0, 0, 0], # [ 1, 2, 3], # [ 2, 4, 6], # [ 3, 6, 9], # [ 4, 8, 12]]) </code></pre> <p>In the above calculation, the scalar values in <code>i</code> (e.g. 0,1,2,3,4) are each multiplied against the array v. The results are collected in a 2-d numpy array. Each row corresponds to a different value of <code>i</code>. See <a href="http://www.scipy.org/EricsBroadcastingDoc" rel="nofollow noreferrer">http://www.scipy.org/EricsBroadcastingDoc</a> for an introduction to numpy broadcasting.</p> <p>@OP: To address your question regarding a "logic loop": Typically, what you do is define a "state" of the system. Perhaps in your case a state would consist of a tuple (time,position,velocity). You would then define a function which is given a state-tuple as an input and return a new state-tuple as output.</p> <p>Given a (time,position,velocity), the force could be computed (mainly from the old position). From the force, you then compute the new velocity. From the velocity, you compute a new position. </p> <p>Don't write code first.</p> <p>In this case, sit down with paper and pencil and grind out the calculation by hand with a concrete example. Do enough iterations until you see clearly the pattern of how the calculation is done. What is the order of the steps? What parts get repeated? Once you see how to do it by hand, it will be much clearer how to write the python code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload