Shop OBEX P1 Docs P2 Docs Learn Events
newb needs help with logistic equation — Parallax Forums

newb needs help with logistic equation

binkybinky Posts: 3
edited 2012-10-27 10:42 in Propeller 1
Hello!

Just starting with the Propellor. Got most things down but I'm having a hard time with the logistic equation. i"m sure it's something very simple that I'm over looking. It's kind of bloat code but that's not a concern at this point, I'm just trying to get the logistic equation to print out to the terminal. I'm only having problems with the algorithm, not with the terminal. Thanks!
{logistic equation printed to terminal}


CON
        _clkmode = xtal1 + pll16x     'Standard clock mode * crystal frequency = 80 MHz
        _xinfreq = 5_000_000
                           
VAR
     
  long stack[100]
  long k
  long x[2]
  long temp[2]
OBJ
     BS2 : "BS2_Functions"    ' Create BS2 Object
     f : "float32full"
     pst : "Parallax Serial Terminal"
     fString : "FloatString"
PUB main

 cognew(start, @stack[0] )  'init start cog
      
  BS2.start (31,30)         'Initialize BS2 Object timing, Rx and Tx pins for DEBUG
  pst.Start(115_200)        'init pst
  f.start                   'init float
PUB start 
       'logistic equation       ' x(i+1)=k*x(i)*(1-x(i)); 
                               
                              

                                 
       k:=3.8       'constant
       x[0]:= 2.0      'start variable
       x[1] := 2.0
       temp[0] := 1.0
       temp[1] := 1.0
       
    repeat   
         x[1] := x[0]

         temp[0] := f.fMul(k, x[0])                ' r*x(i)
         temp[1] := f.fsub (1.0 , x[0])            ' (1-x(i))
         x[1] := f.fmul(temp[0], temp[1])
                 
                 
        pst.str(fstring.FloatToString(x[1]))   'print to terminal
        bs2.pause(200)                      'wait 200 msec
        
        pst.clear                           'clear terminal
                                            'go back to top of loop
        


Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2012-10-25 03:11
    Here is the fix with some little addons:
    CON
            _clkmode = xtal1 + pll16x     'Standard clock mode * crystal frequency = 80 MHz
            _xinfreq = 5_000_000
                               
    VAR
         
      long stack[100]
      long k
      long x[2]
      long temp[2]
    
    OBJ
         BS2 : "BS2_Functions"    ' Create BS2 Object
         f : "float32full"
         pst : "Parallax Serial Terminal"
         fString : "FloatString"
    
    PUB main | tmp
      pst.Start(115_200)        'init pst
    
      tmp:=0
      repeat while tmp==0
        pst.str( string( "Float",$0d,"== please press key to start ==",$0d ) )
        waitcnt( cnt+clkfreq )
        tmp:=pst.RxCount
    
      BS2.start (31,30)         'Initialize BS2 Object timing, Rx and Tx pins for DEBUG
      f.start                   'init float
    
      cognew(start, @stack[0] )  'init start cog
    
    PUB start 
           'logistic equation       ' x(i+1)=k*x(i)*(1-x(i)); 
                                     
           k:=3.8       'constant
           x[0]:= 2.0      'start variable
           x[1] := 2.0
           temp[0] := 1.0
           temp[1] := 1.0
           
        repeat   
             x[0] := x[1]
    
             temp[0] := f.fMul(k, x[0])                ' r*x(i)
             temp[1] := f.fsub (1.0 , x[0])            ' (1-x(i))
             x[1] := f.fmul(temp[0], temp[1])
                     
                     
            pst.str(fstring.FloatToString(x[1]))   'print to terminal
            pst.NewLine
            bs2.pause(200)                      'wait 200 msec
    

    First of all you should take care of the order of starting stuff. In your code you started your calculation before you started float and pst, but these are used by your calculations. Here the result is unpredictable. I did not analyse whether it caused a problem in your code - propably not because I got the same result after 'fixing' it.

    Additionally I added a waiting loop because switching to PST takes a while. This way the interesting thing waits for you to be started with a keypress in PST.

    And finally the BUG:
    x[0]:=x[1]

    Is the thing you want to do at the beginning of the loop. Guess you can figure out yourself why?!
  • binkybinky Posts: 3
    edited 2012-10-26 01:21
    I appreciate the valuable ordering tips, like how you put the "press any key" before starting, get the bug you mentioned on the bottom and very thankful for you taking the time to respond. I really do like 8 cores to play with.

    Unfortunately, the outputs should always be between 0-4 and chaotic at k=3.57 and there's still an error in the algorithm that I'm not figuring out. I'm an analog guy and usually build chaotic oscillators in analog. There is just one piece of the puzzle that I can't figure out to get the logistic equation to work in SPIN. This just quickly goes out of bounds.

    http://www.stsci.edu/~lbradley/seminar/logdiffeqn.html
  • kuronekokuroneko Posts: 3,623
    edited 2012-10-26 01:49
    Could it be that your xn is too big? I get reasonable values (oscillation) with e.g. k = 3.2, x0=0.1 .
  • max72max72 Posts: 1,155
    edited 2012-10-26 02:08
    I tested the values you started with in a spreadsheet and in fact it diverges rapidly.

    I would say it diverges for any starting value greater than 1.

    Massimo
  • binkybinky Posts: 3
    edited 2012-10-27 02:46
    Yes, the starting value was the problem. It's chaotic at k=3.9 but quickly diverges at k=4.1 as it should and get a long term period 2 oscillation at k=3.1. x[0]=0.1 is a good starting value.

    I really appreciate the help offered by forum members here!

    edit: yup, period 4 at k=3.52, chaotic at k=3.82 and solid long term period 3 at k=3.828427. Period 3 is always what I look for in a (analog) chaotic oscillator and is a small "island of stability" in a chaotic system. Period 6 at k=3.84 showing period doubling and back to chaos at k=3.85.

    BTW, the Propellor is going to be tied into some sine wave oscillators for robotic locomotion. A vid I made for a 555 timer competition (took grand prize, I won a Fluke 287 and 2 mini oscilloscopes) that gives some detail on the analog side. Forrest Mims and Hans Camenzind (creator of the 555 timer and 40 other chips) were the final judges.

    http://www.youtube.com/watch?v=uXeSsevsNNE

    Also, if one wants learn to make a quick and dirty balancing robot, it can be done with either a 555 timer chip or a 2 transistor multivibrator. It'll help one understand PID systems.

    http://www.youtube.com/watch?v=Y-afnY32RrY&list=UUpfn9vk8fDuEx5ZtcNLXLCQ&index=2&feature=plcp
  • Heater.Heater. Posts: 21,230
    edited 2012-10-27 03:31
    Gosh, blinky, I programmed that equation for my Atari ST and had it graph the results back in 1986. I had just then read about chaos somewhere and just had to try it out. I was quite amazed by it. Had thought about it for years now and forgotten that it was called the logistic equation.
Sign In or Register to comment.