title: Regarding iterating a function in python
body: <p>I wrote follwing code using Python</p>
  
  <pre class="lang-py prettyprint-override"><code>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
  </code></pre>
  
  <p><strong>First Iteration</strong>   </p>
  
  <pre class="lang-py prettyprint-override"><code>x2=np.array(len(x1))
  p2=np.array(len(p1))
  
  x2 ,p2 = fun1(x1,p1)
  </code></pre>
  
  <p><strong>Second Iteration</strong>     </p>
  
  <pre class="lang-py prettyprint-override"><code>x3=np.array(len(x1))
  p3=np.array(len(p1))
  
  x3 ,p3 = fun1(x1,p2)
  </code></pre>
  
  <p>So in the second iteration, I used <strong>p2</strong>  which was obtained from the previous iteration. </p>
  
  <p><strong>Third Iteration</strong>  </p>
  
  <pre class="lang-py prettyprint-override"><code>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)
  </code></pre>
  
  <p>Based on this, My desired output is, (for 3 iterations)</p>
  
  <pre><code>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.]
  </code></pre>
  
  <p>Since the above code manually updating the values, I need to do the same thing using a for loop or some iterator in Python. </p>
  
  <p>Since I am new to Python, I dont have a clue how to do that. Can anyone suggest an approach to do that? </p>