1

I ran into the problem, I am new with this kind of usage of Python, I hope some one helps me in this issue. I have a code using RungeKutta algorithm. As I do print(vH), it prints:

[70, 98.72259439054349, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

How could I juts have 98.72259439054349 in out put when I write print(vH)? This is the RungeKutta part of the code and nrk=300. Using nrk we have accurate out put, but with this code as you see we have 300 zeros in output.

def rk4(dH_func, H0, A, B, alpha, b, rho_de0, rho_dm0, z0, z1, nrk):

        if dH_func != False and drho_dm_func != False and drho_de_func != False :
           vH = [0] * (nrk + 1)

           h = (z1 - z0) / float(nrk)
           vH[0] = H = H0

           for i in range(1, nrk + 1):
               k1_H = h * dH_func(z, H, rho_de, rho_dm)

               k2_H = h * dH_func(z + 0.5 * h, H + 0.5 * k1_H, rho_de + 0.5 * k1_rho_de, rho_dm + 0.5 * k1_rho_dm)

               k3_H = h * dH_func(z + 0.5 * h, H + 0.5 * k2_H, rho_de + 0.5 * k2_rho_de, rho_dm + 0.5 * k2_rho_dm)

               k4_H = h * dH_func(z + h, H + k3_H, rho_de + k2_rho_de, rho_dm + k2_rho_dm)

               vH[i] = H = H + (k1_H + k2_H + k2_H + k3_H + k3_H + k4_H) / 6

               return vH
Ehsan
  • 73
  • 11

1 Answers1

1

In your function you write:

for i in range(1, nrk + 1):
    # ...
    return vH

So that means that at the end of the first iteration, you immediately return the result.

If you want the function to return the result after all the iterations are done, you should write the return vH outside the body of the for loop, like:

for i in range(1, nrk + 1):
    # ...
return vH

If you only want to print the second item, you should print it with:

print(vH[1])
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • `[70, 98.72259439054349, 144.72020358361797, 215.66663177218655, 269.9988586813829, 269.9988586813829, 269.9988586813829, .....` the result – Ehsan Sep 30 '18 at 21:11
  • 1
    @Ehsan: if you only want the second element, you should print it like `print(vH[1])`. – Willem Van Onsem Sep 30 '18 at 21:13
  • it could be used as a regular number in other calculation automatically? I mean ` ans=vH[1]` and I use `ans` in all next calculations. – Ehsan Sep 30 '18 at 21:14
  • 1
    @Ehsan: yes, you obtain the second element, and can treat it as an indpendent one (well it is an independent one :) – Willem Van Onsem Sep 30 '18 at 21:15
  • and final question, as I did what you said to bring out the `return` out of loop, why the outputs has changed?`70, 98.72259439054349, 144.72020358361797, 215.66663177218655, 269.9988586813829` – Ehsan Sep 30 '18 at 21:17
  • @Ehsan: the point is that it is strange to define a list of 300 elements, and only fill the first two. In software design the idea is that functions are independent elements, that each solve a specific task, with not much "influence" of the specific use case, or other functions. – Willem Van Onsem Sep 30 '18 at 21:18
  • Because I am wondering the, which one of those numbers is my final answer then?! when I get the `return` out of loop. – Ehsan Sep 30 '18 at 21:21
  • @Ehsan: well you get the *list* out of the loop, so you can call the function, and then retrieve the proper element, for example `rk4(dH_func, H0, A, B, alpha, b, rho_de0, rho_dm0, z0, z1, nrk)[1]` (with the paramters filled in)`. – Willem Van Onsem Sep 30 '18 at 21:24
  • Thank you for your help. – Ehsan Sep 30 '18 at 21:28