1

I'm trying to plot a runge kutta equation, but it shows an empty value. Where did I go wrong in the logic?

for n in xrange(0,10):    
   k1 = R*T[n] + g*h[n]
   l1 = -1*r*h[n] - a*b*T[n]


   k2 = R*(T[n]+k1*not_dt/2) + g*(h[n]+l1*not_dt/2)
   l2 = -1*r*(h[n]+l1*not_dt/2) - a*b*(T[n]+k1*not_dt/2)


   k3 = R*(T[n]+k2*not_dt/2) + g*(h[n]+l2*not_dt/2)
   l3 = -1*r*(h[n]+l2*not_dt/2) - a*b*(T[n]+k2*not_dt/2)


   k4 = R*(T[n]+k3*not_dt) + g*(h[n]+l3*not_dt)        
   l4 = -1*r*(h[n]+l3*not_dt) - a*b*(T[n]+k3*not_dt)        


   T[n+1] = T[n] + not_dt/6 *(k1+2*k2+2*k3+k4)
   h[n+1] = h[n] + not_dt/6 *(l1+2*l2+2*l3+l4)


plt.plot(T[n+i],h[n+i]) 

Perhaps I try to simplify a bit

T[0] = 11
h[0] = 0

for test in xrange(0, 10):
    k1 = T[test] + h[test]
    k2 = 2*k1
    k3 = 2*k2

    T[test+1] = T[test]+k3
    h[test+1] = h[test]+k2
    print T[test+1], h[test+1]

plt.plot(T[test+1],h[test+1]) 

I've tried running simplified script above and it gives value for print, but it just wont showed up in plot. So the question is perhaps in the end not related to runge kutta after all, just some logic in plotting. Sorry about that.

F L
  • 183
  • 1
  • 10
  • It's impossible to read this. Can you indent your lines with 4 spaces to get monospaced code-like text? If it doesn't look like Python when you're done, keep editing it till it does. – Matt Hall Feb 09 '16 at 15:28
  • I've edited it. I hope it's more readable. – F L Feb 09 '16 at 15:32
  • Could you provide all the data needed to plot, please? So a full [MCVE](http://stackoverflow.com/help/mcve) would be nice since people not knowing _what_ you're calculating could know _what's_ going wrong with your code. – albert Feb 09 '16 at 15:34
  • The initialization of your simplified code `T[test] = 11` and `h[test] = 0` is not runnable since `test` is undefined at this stage. – albert Feb 09 '16 at 15:43
  • I've made a simplified script below that, with all the data needed. But it's not readable again, since I'm still ttrying to figure out how to make my script in good order – F L Feb 09 '16 at 15:44
  • @FL: You should make sure that your code is runnable. After that, we _might_ solve any problems/errors. There are some implementations given [here](http://www.codeproject.com/Tips/792927/Fourth-Order-Runge-Kutta-Method-in-Python) and [here](http://rosettacode.org/wiki/Runge-Kutta_method#Python). – albert Feb 09 '16 at 15:45
  • Already it looks like the first thing to do is to start using NumPy. Then you won't need to do all this indexing and your code will get much more readable. And I assume you know about division in Python 2? – Matt Hall Feb 09 '16 at 15:50

1 Answers1

1

You are using the running index of the loop outside of the loop where it is not defined.

You attempt to plot exactly one point, not the arrays. Use

plt.plot(T,h)

provided both lists have the same length.

Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51
  • Yes! It works! So that's the explanation. Thank you. And thanks for people responding my question. – F L Feb 09 '16 at 18:48