B4J Tutorial Bifurcation Map (Java-Logistic-Map)

A shortcut warp to kick-start this Github project from B4J. Copy attached Jar to your additional libs folder. Run the attached B4J project

Check this YouTube Video - it is related to what is explained in the video

Change the values for A, 1st Initial Value, 2nd Initial Value




From the Github posting:

Simulation of the logistic map function X(n+1) = A[X(n)][1 - X(n)], showing chaotic behavior for A values ~3.6 to 4

Description: The logistic map is the function X(n+1) = A[X(n)][1 - X(n)], a time series plotting X(n) vs n. This function is a simple example of chaotic behavior, as for values of A in the range of ~3.6 to 4, the results quickly start to look scattered and random. A slight change in the initial value of X(0) can result in dramatic changes in the plot points. This kind of chaotic function is utilized for things like psuedo random number generation (although PRNGs use a bit more complex functions than this one).

Files: Display.java Contains the main method, instantiates Graph objects to display data from logistic map. It creates 3 graphs: The first is a line graph, showing the behavior of the function towards the beginning, for small values of n. The second is a scatter plot showing the full range of points. High values of A close to 4 makes this scatter plot appear nearly random. The third is a phase diagram, which plots X(n+1) vs X(n), instead of X(n) vs n. The parabolic pattern seen here reveals that the apparently random output of the logistic map in fact is very deterministic. This class also creates a table to show the values of the logistic map, and includes text fields for the user to input values for A and 2 initial values.

Graph.java Contains code to create the GUI. Creates the 3 types of graphs mentioned above.

RandomNumbers.java Contains the code to generate the logistic map, in the psuedoRandomNumbers method, which creates 2 time series given the A value and 2 initial values, and returns them in a 2D array. Another method is phaseDiagram, which takes the previously created logistic map to create a phase diagram, as descibed above. Another minor method is checkDouble, which is used to ensure proper user input.
 

Attachments

  • b4jLogisticsMap.zip
    1.7 KB · Views: 192
  • LogisticsMap.jar
    5.6 KB · Views: 198

Johan Schoeman

Expert
Licensed User
Longtime User
Shortcut kickstart for Bifurcation Map (from here)

Copy attached JARS to your B4J additional Libs folder
Run attached B4J project - you should see the below - KILL THE PROCESS BEFORE EXITING THE B4J PROJECT.

 

Attachments

  • Bifurcation.jar
    5.6 KB · Views: 183
  • java-getopt-1.0.12.jar
    54.1 KB · Views: 156
  • b4jBifurcation.zip
    1.7 KB · Views: 172
  • Java Project.zip
    67.4 KB · Views: 157

Johan Schoeman

Expert
Licensed User
Longtime User
OK - this is done with B4J code using the ScatterChart in the jChart library by @Informatix. The B4J project is calculating and adding some 1500 different series to the ScatterChart.
Have added a CSS file so that each point in each series will be a black dot with radius of 1px.
Be prepared to sit it out for 5+ minutes (depending on how fast your computer is) while it is drawing the Bifurcation Map (live drawing) - but quite interesting to see it being drawn in real time.

The Bifurcation Map is based on this web posting
No additional libraries required other than jChart - all just B4J code



If you comment out this line in the code....
B4X:
        Sleep(0)
then it takes about 100 seconds (on my laptop) to draw the chart.

B4J sample code:
#Region Project Attributes
    #MainFormWidth: 1200
    #MainFormHeight: 800
#End Region


Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
 
    Dim N, cnt As Int
    Dim r, y As Double

    Private sc1 As ScatterChart
    Private YAxis As NumberAxis
    Private XAxis As NumberAxis
 
    Dim starttime, endtime As Long
 
 

End Sub

Sub AppStart (Form1 As Form, Args() As String)
 
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
 
    MainForm.Stylesheets.Add(File.GetUri(File.DirAssets, "chartcolors.css"))
 
    sc1.Animated = False
    sc1.LegendVisible = False
 
    YAxis = sc1.YAxis
    YAxis.AutoRanging = False
    XAxis = sc1.XAxis
    XAxis.AutoRanging = False
 
    XAxis.LowerBound = 0.69
    XAxis.UpperBound = 1.1
 
    YAxis.LowerBound = -0.1
    YAxis.UpperBound = 1.1

    cnt = 0
    Dim series(1600) As XYSeries
    For i = 0 To 1599
        series(i).Initialize("")
    
    Next

    cnt = 0
    N = 1500
    starttime = DateTime.Now
    For r = 0.7 To 1.0 Step r + 0.3/N
        y = (Rnd(0, 1000001))/1000000
        For i = 0 To 999
            y = logistic(y,r)
        Next
        For i = 0 To 99
            y = logistic(y,r)
            series(cnt).Add(r, y)
        Next
        sc1.AddSeries(series(cnt))
        cnt = cnt + 1
        'Log(cnt)
        Sleep(0)
    Next
 
    endtime = DateTime.now
    Log("cnt = " & cnt)
    Log("DONE")
    Dim duration As Long = endtime - starttime
    Dim seconds As Int = duration/1000
    Log(seconds)
 
 
End Sub

Sub logistic(y1 As Double, r1 As Double) As Double
 
    
    Return 4.0 * r1 * y1 * (1.0 - y1)
 
End Sub


'It is based on this web posting:
'https://introcs.cs.princeton.edu/java/94diffeq/LogisticMap.java.html

Chart with the radius and padding in the CSS file set to 0.5px (better definition)

 

Attachments

  • bifurcationB4J.zip
    3.1 KB · Views: 161
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
56 seconds with the below code (less definition) - live with using 600 different series added to the ScatterChart



N = 600:
#Region Project Attributes
    #MainFormWidth: 1200
    #MainFormHeight: 800
#End Region


Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
   
    Dim N, cnt As Int
    Dim r, y As Double

    Private sc1 As ScatterChart
    Private YAxis As NumberAxis
    Private XAxis As NumberAxis
   
    Dim starttime, endtime As Long
   
   

End Sub

Sub AppStart (Form1 As Form, Args() As String)
   
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
   
    MainForm.Stylesheets.Add(File.GetUri(File.DirAssets, "chartcolors.css"))
   
    sc1.Animated = False
    sc1.LegendVisible = False
   
    YAxis = sc1.YAxis
    YAxis.AutoRanging = False
    XAxis = sc1.XAxis
    XAxis.AutoRanging = False
   
    XAxis.LowerBound = 0.69
    XAxis.UpperBound = 1.1
   
    YAxis.LowerBound = -0.1
    YAxis.UpperBound = 1.1

    cnt = 0
    Dim series(700) As XYSeries
    For i = 0 To 699
        series(i).Initialize("")
       
    Next

    cnt = 0
    N = 600
    starttime = DateTime.Now
    For r = 0.7 To 1.0 Step r + 0.3/N
        y = (Rnd(0, 100000001))/100000000
        For i = 0 To 999
            y = logistic(y,r)
        Next
        For i = 0 To 99
            y = logistic(y,r)
            series(cnt).Add(r, y)
        Next
        sc1.AddSeries(series(cnt))
        cnt = cnt + 1
        'Log(cnt)
        Sleep(0)
    Next
   
    endtime = DateTime.now
    Log("cnt = " & cnt)
    Log("DONE")
    Dim duration As Long = endtime - starttime
    Dim seconds As Int = duration/1000
    Log(seconds)
   
   
End Sub

Sub logistic(y1 As Double, r1 As Double) As Double
   
       
    Return 4.0 * r1 * y1 * (1.0 - y1)
   
End Sub


'It is based on this web posting:
'https://introcs.cs.princeton.edu/java/94diffeq/LogisticMap.java.html
 
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User

Attachments

  • b4jLogistics.zip
    1.6 KB · Views: 155
  • logistic.jar
    5.9 KB · Views: 170
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
A B4J kickstart for this web posting.

1. Copy attached Jar to your B4J additional libs folder.
2. Extract and run the B4J project
3. Fiddle around with the values in the boxes on the left of the UI



B4J code is short and sweet to kickstart it from within B4J

Short and Sweet B4J code:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

#AdditionalJar: LogisticMap


Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI

End Sub

Sub AppStart (Form1 As Form, Args() As String)
'    MainForm = Form1
'    MainForm.RootPane.LoadLayout("Layout1")
'    MainForm.Show

    Dim pendulum As JavaObject
    pendulum.InitializeNewInstance("logisticmap.Main", Null)
    
    Dim dummy() As String = Array As String("")
    pendulum.RunMethod("main", Array(dummy))
    
    'http://john-gardner.net/logisticmap/index.html

End Sub
 

Attachments

  • LogisticMap.jar
    28.6 KB · Views: 163
  • b4jLogisticMap.zip
    1.6 KB · Views: 148
Cookies are required to use this site. You must accept them to continue using the site. Learn more…