Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Following this link (with a working code in a Wolfram language):
http://demonstrations.wolfram.com/LyapunovFractals/[^]

generating the image should be pretty straight forward:
Choose a string of As and Bs of any nontrivial length (e.g., AABAB).
Construct the sequence S formed by successive terms in the string, repeated as many times as necessary.
Choose a point (a,b) \in [0,4] \times [0,4].
Define the function
r_n = a if S_n = A,
and
r_n = b if S_n = B.

Let x_0 = 0.5, and compute the iterates
x_{n+1} = r_n x_n (1 - x_n).

Compute the Lyapunov exponent:
\lambda = \lim_{N \rightarrow \infty} {1 \over N} \sum_{n = 1}^N \log \left|{dx_{n+1} \over dx_n}\right| = \lim_{N \rightarrow \infty} {1 \over N} \sum_{n = 1}^N \log |r_n (1 - 2x_n)|

In practice, \lambda is approximated by choosing a suitably large N.
Color the point (a,b) according to the value of \lambda obtained.
Repeat steps (3–7) for each point in the image plane.

VB
' https://en.wikipedia.org/wiki/Lyapunov_fractal
  ' http://hypertextbook.com/chaos/44.shtml

  LyapunovCanvas.Background = Nothing

  ' Save the generating string (AB) into a char array
  Dim Sn As Char() = txtLyapunovGeneratingString.Text.ToString.ToCharArray

  ' Set the resolution of the image
  Dim LyapunovImageDimensions As Integer = 400

  'Calculate the scaling factor to get the appropriate a and b values
  Dim LyapunovScaleFactor = 4 / LyapunovImageDimensions

  Dim LyapunovLambdaArray(LyapunovImageDimensions + 1, LyapunovImageDimensions + 1) As Double

  Dim MAxValue, MinValue As Double
  MAxValue = Double.MinValue
  MinValue = Double.MaxValue

  ' The number of iterations to calculate the Lyapunov Exponent Lambda
  Dim N As Integer = 180

  ' The values a and b that should be between 0 and 4 unless you have zoomed in
  Dim a, b As Double

  For x As Integer = 0 To LyapunovImageDimensions
      For y As Integer = 0 To LyapunovImageDimensions

          ' Caluclate the a and b values from the placement
          a = LyapunovScaleFactor * x
          b = LyapunovScaleFactor * y

          ' Initialize the calcualtion parameters
          Dim rn As Double = 0
          Dim X0 As Double = 0.5
          Dim Xn As Double = X0
          Dim LyapunovExponent As Double = 0

          'Loop through the items in the generation string
          For CharItem As Integer = 0 To Sn.Length - 1

              ' Choose the value for rn based on the current item in the generating string
              rn = If(Sn(CharItem).ToString.ToUpper = "A", a, b)

              ' Reset the Xn value
              Xn = 0.5

              ' The iteration loop
              For NCount As Integer = 1 To N

                  ' Calcualte the exponent value and add a small value inside the logarithm
                  ' in case the clculated value is 0
                  LyapunovExponent += Math.Log(0.00001 + Math.Abs(rn * (1 - 2 * Xn))) / N

                  ' Create the next value of Xn
                  Xn = rn * Xn * (1 - Xn)
              Next
          Next

          ' Save the exponent value to the Image array
          LyapunovLambdaArray(x, y) = LyapunovExponent

          'Calculate the max and min values calculated (can be used for drawing the image)
          If MAxValue < LyapunovExponent Then
              MAxValue = LyapunovExponent
          End If

          If MinValue > LyapunovExponent Then
              MinValue = LyapunovExponent
          End If

      Next
  Next

  ' Create a Byte array of the image
  Dim buffer As New List(Of Byte)

  For x As Integer = 0 To LyapunovImageDimensions
      For y As Integer = 0 To LyapunovImageDimensions
          Dim k As Integer = 0

          Dim TempBrush As New SolidColorBrush

          ' Based on the value of the exponent
          If LyapunovLambdaArray(x, y) > 0.1 Then
              TempBrush = Brushes.Blue
          ElseIf LyapunovLambdaArray(x, y) < 0.1 Then
              TempBrush = Brushes.Yellow
          Else
              TempBrush = Brushes.Black
          End If

          buffer.Add(TempBrush.Color.B)
          buffer.Add(TempBrush.Color.G)
          buffer.Add(TempBrush.Color.R)
          buffer.Add(TempBrush.Color.A)

      Next
  Next

  Dim dpiX As Double = 96D
  Dim dpiY As Double = 96D
  Dim pixelFormat = PixelFormats.Pbgra32
  Dim bytesPerPixel = Math.Truncate(((pixelFormat.BitsPerPixel + 7) / 8))
  Dim stride = bytesPerPixel * (LyapunovImageDimensions + 1)


  Dim img As New ImageBrush
  img.ImageSource = BitmapSource.Create(LyapunovImageDimensions, LyapunovImageDimensions, dpiX, dpiY,
                                   pixelFormat, Nothing, buffer.ToArray, stride) '
  LyapunovCanvas.Background = img


But the images that are created looks nothing like they should. Can anyone spot my mistake here?
Posted
Updated 19-Nov-15 0:00am
v2

I'm not sure, but these lines:
VB
Dim MAxValue, MinValue As Double
MAxValue = Double.MinValue
MinValue = Double.MaxValue

seems suspicious to me.
Maybe:
VB
Dim MAxValue, MinValue As Double
MAxValue = Double.MaxValue
MinValue = Double.MinValue

?
 
Share this answer
 
Comments
Kenneth Haugland 19-Nov-15 5:56am    
No, I think it's correct the way they are. The Max value is set so the new value found will be lower than the current value is, and the opposite for the min value.
phil.o 19-Nov-15 6:03am    
Ok, makes sense. Did you try to invert the order in which you are filling your buffer variable (I mean ARGB instead of BGRA) ?
Kenneth Haugland 19-Nov-15 6:21am    
Ill try, waiting for VS to update lol
Kenneth Haugland 19-Nov-15 6:34am    
The array is decided by the arrangements in this line:
Dim pixelFormat = PixelFormats.Pbgra32
I could change the format, but then again, it work as it is.
Kenneth Haugland 19-Nov-15 18:54pm    
Found the problem, it now works :-)
I figured out what I missed. The text string of a's and b's have to be repeted for all iterations. It now works as expected.

VB
' Caluclate the a and b values from the placement
               a = LyapunovScaleFactor * x
               b = LyapunovScaleFactor * y

               ' Initialize the calcualtion parameters
               Dim rn As Double = 0
               Dim X0 As Double = 0.5
               Dim Xn As Double = X0
               Dim LyapunovExponent As Double = 0
               Xn = 0.5
               'Loop throught the items in the generation string
               '    For CharItem As Integer = 0 To Sn.Length - 1
               For NCount As Integer = 1 To N

                   ' Choose the value for rn based on the current item in the generating string
                   ' You need to make the string repeatable for all the iterations hence the mod function 
                   rn = If(Sn(NCount Mod Sn.Length).ToString.ToUpper = "A", a, b)       

                   ' Create the next value of Xn
                   Xn = rn * Xn * (1 - Xn)

                   ' Calcualte the exponent value and add a small value inside the logarithm
                   ' in case the clculated value is 0
                   LyapunovExponent += Math.Log(Math.Abs(rn * (1 - 2 * Xn))) / N / Math.Log(2)
               Next

               ' Sace the exponent value to the Image array
               LyapunovLambdaArray(x, y) = LyapunovExponent
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900