1
$\begingroup$

My question relates to looping in Python.

I have an original $6$-sized vector, $F(n,t=0)=[1,0,0,0,0,0]$

Hence, the $n$-values range from $0 \rightarrow 5$.

This output for the original vector is just a series of values such as $F(0,0)=1, F(1,0) = 0$ , $F(2,0) = 0 $ etc...

This relates to a formula:

$$F(n,t+1) = -(l+(kn)-1)F(n,t) + k(n+1)F(n+1,t) + lF(n-1,t)$$

I will set $n = [0,1,2,3,4,5]$ and $t=[0,1,2,3,4,5]$

I need to create a loop to produce additional vectors using the original condition above.

$k=0.05$ and $l=0.01$ are constants

I can calculate this easily by hand but for future expansion to larger ranges of $n$ and $t$ I will need a computer programme.

Below is the beginning of my idea of what is required. This loop may be trivial to someone experienced in python coding. Any help is appreciated.

import numpy as np
import matplotlib.pyplot as plt

#Setting constants
k = 0.05
l = 0.01

t = np.arange(0,5,1)
n = np.arange(0,5,1)

#initial conditions:
F = np.zeros(n)
F0 = (1,0,0,0,0,0)
F[0] = F0

for i in range(t):
    F[i+1] = -(k+(l*n)-1)*F[i]+l*(n+1)*F[i]+k*F[i]
$\endgroup$
2
  • $\begingroup$ Do you generally want to form all the vectors or just particular values, say F(5,5)? If you just want some values, a recursive function, possibly with caching of intermediate values, would be a nice way of doing things. For the whole vectors, your current approach attempts to make the vector for just the last t, but it has a couple problems. For one, you would need to do a loop over i to change all the values (along with accounting for i outside the range of your list). You also update the vector before filling in all the values, so you erase some of the t values that you need for t+1 values $\endgroup$ – Tyberius Mar 21 at 16:47
  • $\begingroup$ I want to produce a list of subsequent vectors, so I need the full vector $F$ when $t = 1, 2, 3 $ etc... $\endgroup$ – David Mar 23 at 14:24
1
$\begingroup$
import numpy as np

def vectors(max_n,max_t,k=0.05,l=0.01):
    times = np.arange(1,max_t,1) ##exclude t=0, set in initial condition
    indicies = np.arange(0,max_n,1)

    #initial conditions:
    F = np.zeros((max_n,max_t))
    F[0,0] = 1 

    for t in times:
        for n in indicies:
            F[n,t] = -(l+(k*n)-1)*F[n,t-1]
            if n!=max_n-1:
                F[n,t]+=k*(n+1)*F[n+1,t-1]
            if n!=0:
                F[n,t]+=l*F[n-1,t-1]
    return F

print(vectors(6,6).T)

This will make all the vectors of length max_n up to time max_t. The last two if statements in the code are to avoid moving out of the bounds of the array. There are probably smarter ways of doing this, but this way is conceptually simple. Wrapping it all in a function will allow you to more quickly change the various parameters.

$\endgroup$
3
  • $\begingroup$ Thanks for your help! Did you run this code? I tried running it and there is an error, saying that "index 5 is out of bounds for axis 1 with size 5". Is there a quick solution to this? $\endgroup$ – David Mar 23 at 15:42
  • $\begingroup$ @David I had a couple little typos left over, but I have tested it now and it seems to work. I also changed the print statement at the end to transpose it. This will make the rows of the array the different time steps. You could form the array this way to begin with by swapping all the t indexes with the n indexes. $\endgroup$ – Tyberius Mar 23 at 15:55
  • $\begingroup$ Thanks so much! You are a life saver :) $\endgroup$ – David Mar 23 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.