Programming Question

Flying Pig

Utrinque Paratus
Retired Moderator
Joined
Jan 24, 2009
Messages
15,647
Location
Perfidious Albion
I discovered a formula a few hours ago for producing fractal results on a graph: take x and y, and run the formula x = y(x(1-x)) until it stabilises out between a few numbers (it goes mad when y = 4) and then if you plot a graph of the final result of x and the value of y it makes a fractal.

I spent ages trying to write a Visual Basic 2008 function to get a final result for x, but failed. That's all I want to do - take a value x and y, run the formula till I get the same result twice, then stop and return a new number. Can anyone help me?
 
It's been a long, long while since I've done any programming, but what kind of errors do you get back?

Offhand, I think the problem is that the definition is recursive (X is on both sides of the equation), so you might need to study some recursive algorithms to get it running. Alternative might be to re-solve the equation in terms of y. Might also try looking up some fractal algorithms and find one that is similiar to yours.
 
Well it's iterative so

x(n+1) = y*x(n)(1-x(n))

So it depends upon constant y and the initial value for x, x(0).

I think the issue you may be encountering is floating point inaccuracy, it's rare for 2 floating point numbers to be exactly equal because of rounding issues.

You need to use a small error value, end the iteration when

ABS(x(n+1) - x(n)) < 0.0001 say.

EDIT: It's also possible for the result to oscillate between 2 or more points so it may never stabilise to a single value... you need a get-out clause for this as well, e.g. stop after 1000 iterations.
 
Ah, ok, as I thought you are trying to draw something like this, a bifurcation diagram

BifurcationLogistic_900.gif


http://mathworld.wolfram.com/Bifurcation.html

Note that the graph has 2 or more values for certain input, these are oscillating points ("orbits"). You won't be able to write a function that returns a single value I'm afraid.

EDIT: I started a "let's discuss mathematics" thread in science and technology, ask there if you want to know more about this kind of fractal.
 
I discovered a formula a few hours ago for producing fractal results on a graph: take x and y, and run the formula x = y(x(1-x)) until it stabilises out between a few numbers (it goes mad when y = 4) and then if you plot a graph of the final result of x and the value of y it makes a fractal.

I spent ages trying to write a Visual Basic 2008 function to get a final result for x, but failed. That's all I want to do - take a value x and y, run the formula till I get the same result twice, then stop and return a new number. Can anyone help me?

Here's a good book on fractals

The Computational Beauty of Nature: Computer Explorations of Fractals, Chaos, Complex Systems, and Adaptation
by Gary William Flake


It's the University standard for teaching computer science/math students about Fractals and Complex Systems.

Should be able to find it in a University Library or online.
edit: here's a link to it.
http://mitpress.mit.edu/books/FLAOH/cbnhtml/home.html

I'd dump Visual Basic and use Java or C++ instead.
 
I don't know how to make it :"do until you get the same value twice"

Same value twice in succession? Use

ABS(last_x - current_x) <0.0001

Otherwise you need to store all previous results and compare to those. That WILL happen with the function you are using, it has cycles of every possible length in the chaotic regions.
 
Programming 101: It's easier to diagnose a problem if you can actually frickin' see it!

You should dump the values of each step into a log file (or some other easily accessible location) so you can find out where and how it hangs.
 
Why the poop would you do a divide?

Because you're doing it until you get the same value twice. If num1 and num 2 are your consecutive values, when num1/num2 == 1, you have the same value? Im sure there are other ways you can do it, like num1%num2 ==0, etc.
 
Because you're doing it until you get the same value twice. If num1 and num 2 are your consecutive values, when num1/num2 == 1, you have the same value? Im sure there are other ways you can do it, like num1%num2 ==0, etc.
can't you like do num1 == num2?

I mean the first one is susceptible to divide by zero errors, plus computers generally suck at dividing stuff, the second presumes a mod function (I don't know basic, so % might mean something else) works with the datatype (which if it's floating point might not be the case) and if it's an even multiple creates a true when it shouldn't.

Plus it's easier for a person reading the code to understand an equality then some arithmetic operation.

Personally I'd do a basic equality, or I'd do an inequality like ParadigmShifter's.
 
can't you like do num1 == num2?

I mean the first one is susceptible to divide by zero errors, plus computers generally suck at dividing stuff, the second presumes a mod function (I don't know basic, so % might mean something else) works with the datatype (which if it's floating point might not be the case) and if it's an even multiple creates a true when it shouldn't.

Plus it's easier for a person reading the code to understand an equality then some arithmetic operation.

Personally I'd do a basic equality, or I'd do an inequality like ParadigmShifter's.

Oh you can for sure. I was just saying why its done. As I said, lots of other ways, but yes, simple equality is the best, unless you're dealing with variables that are prone to rounding errors. In which case, you'd need to have some method of rounding them to the point where such errors would dissappear. But I digress.
 
Oh you can for sure. I was just saying why its done.
You're saying why it works, not why it's done. There's a very big difference. For any problem there's pretty much an infinite number of ways to solve it, but there are very few that one should actually do.
 
for i = 0; array == array[i-1]; i++
__array = function
for each i in array
__readstuff

Dunno vb loops are all weird
 
Yes, that's what you need to do.

When you find one you already have the entire sequence will then repeat (obvious). The period of the cycle is the difference in the index of the first and second occurrence. You can then plot all the intermediate values on your bifurcation diagram.

Floating point accuracy may be an issue which is why I suggested using an epsilon value of .00001 say.

You will probably still have to have a bail out in the chaotic region say after a couple of thousand terms with no repeat.
 
Post your code and I will have a look at it.

I think you can use [ code ] tags like this

Code:
stuff
 
Code:
for i = 0; array[i] == array[i-1]; i++
__array[i] = function
for each i in array
__readstuff

I meant this. I'll find my own code and post it, and see what you make of it.

Code:
Public Class Form1

    Dim X As Decimal
    Dim Y As Decimal
    Dim result As Decimal


    Private Function Fractal(ByVal X As String, ByVal Y As String) As Double

        X = Val(X) * (1 - X)
        X = Y * X

        PROBLEM! WHAT GOES HERE? Needs to be something along the lines of 'do it again until you get a value that you've seen before'

        Return result

    End Function


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        X = X_BOX.Text
        Y = Y_BOX.Text

        Fractal(X, Y)

        MsgBox (THE VALUES OF X, MINUS THE REPEAT)

    End Sub
End Class
 
Back
Top Bottom