0

I wrote follwing code using Python

import numpy as np

x1=np.array([1,2,3,4])
p1=np.array([.1,.2,.3,.4])

def fun1 (x_vec,p_vec):
    x11=np.zeros(len(x_vec))
    p11=np.zeros(len(p_vec))

    for i in range (0,len(x_vec)):
        x11[i] =x_vec[i]**2
        p11[i]=x11[i]*p_vec[i]

    return x11 ,p11

First Iteration

x2=np.array(len(x1))
p2=np.array(len(p1))

x2 ,p2 = fun1(x1,p1)

Second Iteration

x3=np.array(len(x1))
p3=np.array(len(p1))

x3 ,p3 = fun1(x1,p2)

So in the second iteration, I used p2 which was obtained from the previous iteration.

Third Iteration

x4=np.array(len(x1))
p4=np.array(len(p1))

    x4 ,p4 = fun1(x1,p3)

    print("p",p2)
    print("x",x2)

    print("p",p3)
    print("x",x3)

    print("p",p3)
    print("x",x4)

Based on this, My desired output is, (for 3 iterations)

p [0.1 0.8 2.7 6.4]
x [ 1.  4.  9. 16.]
p [1.000e-01 3.200e+00 2.430e+01 1.024e+02]
x [ 1.  4.  9. 16.]
p [1.000e-01 3.200e+00 2.430e+01 1.024e+02]
x [ 1.  4.  9. 16.]

Since the above code manually updating the values, I need to do the same thing using a for loop or some iterator in Python.

Since I am new to Python, I dont have a clue how to do that. Can anyone suggest an approach to do that?

2 Answers 2

1

Simply reassign back to the same variables in a loop.

If you need to, you can also keep track of all the iterations in a list (results here).

import numpy as np


def fun1(x_vec, p_vec):
    x11 = np.zeros(len(x_vec))
    p11 = np.zeros(len(p_vec))

    for i in range(0, len(x_vec)):
        x11[i] = x_vec[i] ** 2
        p11[i] = x11[i] * p_vec[i]

    return x11, p11


x = np.array([1, 2, 3, 4])
p = np.array([0.1, 0.2, 0.3, 0.4])

results = [(x, p)]

for i in range(5):
    x, p = fun1(x, p)
    print(i, x, p)
    results.append((x, p))

outputs

0 [ 1.  4.  9. 16.] [0.1 0.8 2.7 6.4]
1 [  1.  16.  81. 256.] [1.0000e-01 1.2800e+01 2.1870e+02 1.6384e+03]
2 [1.0000e+00 2.5600e+02 6.5610e+03 6.5536e+04] [1.00000000e-01 3.27680000e+03 1.43489070e+06 1.07374182e+08]
3 [1.0000000e+00 6.5536000e+04 4.3046721e+07 4.2949673e+09] [1.00000000e-01 2.14748365e+08 6.17673396e+13 4.61168602e+17]
4 [1.00000000e+00 4.29496730e+09 1.85302019e+15 1.84467441e+19] [1.00000000e-01 9.22337204e+17 1.14456127e+29 8.50705917e+36]

and results ends up being a list of 2-tuples:

[(array([1, 2, 3, 4]), array([0.1, 0.2, 0.3, 0.4])),
 (array([ 1.,  4.,  9., 16.]), array([0.1, 0.8, 2.7, 6.4])),
 (array([  1.,  16.,  81., 256.]),
  array([1.0000e-01, 1.2800e+01, 2.1870e+02, 1.6384e+03])),
 (array([1.0000e+00, 2.5600e+02, 6.5610e+03, 6.5536e+04]),
  array([1.00000000e-01, 3.27680000e+03, 1.43489070e+06, 1.07374182e+08])),
 (array([1.0000000e+00, 6.5536000e+04, 4.3046721e+07, 4.2949673e+09]),
  array([1.00000000e-01, 2.14748365e+08, 6.17673396e+13, 4.61168602e+17])),
 (array([1.00000000e+00, 4.29496730e+09, 1.85302019e+15, 1.84467441e+19]),
  array([1.00000000e-01, 9.22337204e+17, 1.14456127e+29, 8.50705917e+36]))]
Sign up to request clarification or add additional context in comments.

Comments

1

Just use a for loop to iterate:

numOfIteration = 3
for i in range(numOfIteration):
    x,p = fun1(x1, p1)
    p1 = p
    print("p", p)
    print("x", x)

output:

p [0.1 0.8 2.7 6.4]
x [ 1.  4.  9. 16.]
p [1.000e-01 3.200e+00 2.430e+01 1.024e+02]
x [ 1.  4.  9. 16.]
p [1.0000e-01 1.2800e+01 2.1870e+02 1.6384e+03]
x [ 1.  4.  9. 16.]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.