This is part six of the Beginners Guide to Writing Fractal formulas.
Please do one, two, three, four and five before doing this tutorial.

For this Tutorial we're going to optimize and trouble shoot the 
formula. While optimizing we will make the initial x value of the formula 
selectable. We will also add a fuzz factor for texturing.

In preparation:

Start UF 

From the menu bar click Options/Options/ .  This dialogue is the place 
where you change all your default settings. Click the Environment Tab.  On 
the upper left you will see a checkbox that says "strict type-checking". 
Click that box if is not clicked.  Click the generate warnings if not
already clicked. Unclick Hide messages upon success.  Click OK.

Load Fttutorial5
And you should get these alarms. 

Warning:fttut.ufm(L#:C#):Imaginary part of complex numbers is ignored
Warning:fttut.ufm(L#:C#):Imaginary part of complex numbers is ignored
Successfully compiled FTTutorial5 in fttut.ufm.  

Close the warnings window.

Click the edit to open the ufm in the editor if its not already there.
Copy and paste a new instance of Fttutorial5. Change the title of the new
instance to Fttutorial6. Close the formula editor, save your changes
Load Fttutorial6, And you should get these alarms again. 

Warning:fttut.ufm(L#:C#):Imaginary part of complex numbers is ignored
Warning:fttut.ufm(L#:C#):Imaginary part of complex numbers is ignored
Successfully compiled FTTutorial6 in fttut.ufm.  

Lets look at the first warning in detail.

Warning:fttut.ufm(L#:C#):Imaginary part of complex numbers is ignored

L# is the line number where the problem occurred C# is the column number
on L# where the problem is. The compiler thinks that the variables being
used in this place are imaginary and it's a float application so it's
going to ignore the imaginary part.

Double click that warning and it will open the formula editor and place 
the cursor to the right of where the problem is.  What's there, it's the
circular trap test.

  If (X-H)^2 + (Y-K)^2 < 4

That means at some point the compiler is making X or Y or H or K 
imaginary. But I tried to defined them above.  Lets force the compiler to
define all these as float type.  The loop section looks like this

        LOOP:
          Z = A * Z^3 + B * Z + @FTFUNC(C)
          X = real (Z)
          Y = image(Z)
          M = @FTM
          N = @FTN
          H = real (@Center)
          K = imag (@Center)
        
We are going to append a Float in front of the lines where we define X, 
Y, H, K. Lets do that.

        LOOP:
          Z = A * Z^3 + B * Z + @FTFUNC(C)
          Float X = real (Z)
          Float Y = imag(Z)
          M = @FTM
          N = @FTN
          Float H = real (@Center)
          Float K = imag (@Center)
        
Lets save our work, close the warning window and reload the formula.

Another alarm comes up.  It's the same ignore imaginary part type error 
so lets double click the warning.

It goes to the elliptical trap now, not the circular trap.

  If (X-H)^2/M^2 + (Y-K)^2/N^2 <= 1

We defined the H and K the only other variables are M and N.  So lets 
force the compiler to define M and N as float type.  The loop section
should now look kike this.

        LOOP:
          Z = A * Z^3 + B * Z + @FTFUNC(C)
          Float X = real (Z)
          Float Y = imag(Z)
          Float M = @FTM
          Float N = @FTN
          Float H = real (@Center)
          Float K = imag (@Center)
         
Lets save our work, close the warning window and reload the formula.
Now the message window says:

Successfully compiled FTTutorial6 in fttut.ufm.  

You can go back into options/options/environment and click the "hide
messages upon success" but its a good idea to have it on when writing
formulas.

You can ignore the following but don't. Keep the pressure on the little 
gray cells.  It's a little tutorial on how to figure out what the critical 
value is for Z (thestarting point of every iteration).  The math may be a 
little involved.  The steps are simple.

1.Take the function you are iterating.
        AZ^3+BZ+C

2. Take the first derivative of the function.
        3AZ^2 + B

3. Set the derivative equal to 0
        3AZ^2 + B = 0

4.    Use whatever method you can to solve for Z
        In this formula I will use manipulation (HS algebra, solving
quadratic equations).
        3AZ^2 + B = 0
              3AZ^2 =-B
                  Z^2 =-B/3A
                     Z = sqrt (-B/3A)
The two roots of the equation are:
        Z = sqrt (-b/(3*a) and
        Z = -sqrt (-b/(3*a)

We're only going to use the positive root.  Later if you want you can 
make the formula randomly intialize from one to the other. You can also
have it calculate a new initial value depending on you're A and B values.
This method only works with escape type fractal formulas.

To our formula, Batman.

We only want to make this point selectable for now.  Lets see what we 
need to do:
        We need to create a variable called zstart
        We need to make its default value equal to sqrt (-B/(3*a)
        We need to change where the z initialization.
         
Lets create a new param called zstart
        Param zstart
          Caption = "Z Initial"
          Hint = "This is the starting point of every iteration"
          Default = (0.0,0.0577350)
        Endparam

Lets put this into our formula in the default section.

Lets change the init section from
        Z=#pixel to 
        Z=@zstart

That's it were done.  Lets save our work, reload the formula.

 

The fuzz factor. (What artists call hiding the rough edges).

What do we need to do for this fuzz factor?

First we need to create a parameter called fuzz.  I think we should 
make it float type type. And lets make the default value for this param 0.
For educational purposes let me tell you about two other items you can put 
in the param block.  They are max and min.  They do exactly what they say.  
If we wanted to limit the value of @fuzz we would include a 

max = 256 so now you cant input anything over 256 and

min = 0 so now you cant input negative numbers.

This is what the param block would look like but we won't use max and 
min for now.

        Param fuzz
          Caption = "Fuzzy Navel"
          Hint = "This is a blurring brush"
          Default = 0.0
          Min = 0
          Max = 256
        Endparam

Lets do that.  Put the above definition into our formula but leave out 
the max and min.

We need to think up a fuzz function.  
First of all if the fuzz value is zero I don't want any fuzz and as I
increase the fuzz value I want it to get more and more misty. Lets plan 
this
        If @fuzz == 0
          Keep current Z No fuzzy navel at all
        Else
          Current Z value plus a random constant
        Endif

Lets try a little more specific
        If @fuzz == 0
          Z = @zstart
        Else
          Z = @zstart + some function of fuzz
        Endif

We're almost there, i can almost taste it.

At this time I'd like to introduce the system function #rand.  This
generates a random complex number, of which x and y are greater than 
zero but less than 1.

Lets come up with a fuzzy function using #rand.  What do we have?
        We have our user input             @fuzz
        We have numbers between 0 and 1 #rand
        And just for scaling purposes lets scale the random number down by
100000 (once you are comfortable with this numbe you can        change it
to anything you like).

Lets multiply the two together and divide by the scaling factor.

Fuzzy function is 

      @fuzz*#rand/100000.

Lets put this into the If Fuzzy else loop
        If @fuzz == 0
           Z=@zstart
        Else
           Z=@zstart + @fuzz*#rand/10000
        Endif

So now our init section lokks like this:
        Z = @zstart
        If @fuzz == 0
           Z=@zstart
        Else
           Z=@zstart + @fuzz*#rand/10000
        Endif
        A = @FTA
        B = @FTB
        C = #PIXEL
        STOP = 0
We must now remove the Z=#pixel initialization line.

Lets save our work, and reload the formula.

The formula should look like this now.

FTtutorial6{

; If this tutorial goes any slower I'll have to go to the dentist.

; All right I made the appointment for the dentist

INIT:
  If @fuzz == 0
    Z=@zstart
  Else
    Z=@zstart + @fuzz*#rand/10000
  Endif

  A = @FTA
  B = @FTB
  C = #PIXEL
  STOP = 0
LOOP:
  Z = A * Z^3 + B * Z + @FTFUNC(C)
  Float X = real(Z)
  float Y = imag(Z)
  Float M = @FTM
  Float N = @FTN
  Float H = real (@Center)
  Float K = imag (@Center)
  IF @FTTEST == 0
    If (X-H)^2 + (Y-K)^2 < 4
      STOP =1
    Else
      STOP =0
    Endif
  Elseif @FTTEST == 1
    If (X-H)^2/M^2 + (Y-K)^2/N^2 <= 1
      STOP =1
    Else
      STOP =0
    Endif
  Endif
BAILOUT:
  STOP != 0
DEFAULT:    ;It's a great habit to just comment
PARAM FTA
      Default = (1.0,0.0)   
      Caption ="Coeff A"
      Hint = "A in AZ^3+BZ+C"
    Endparam
 
PARAM FTB
      Default = (1.0,0.0)
      Caption ="Coeff B"
      Hint = "B in AZ^3+BZ+C"
    Endparam
 
Param FTM
  Default = 2.0
  Caption = "Ellipse Xaxis Width"
  Hint = "the xaxis length for the elliptical trap"
Endparam
 
Param FTN
  Default = 1.0
  Caption = "Ellipse Yaxis Width"
  Hint = "the yaxis length for the elliptical trap"
Endparam
 
Param FTTEST
  Caption = "Bailout Test"
  Enum = "Circle" "Ellipse"
  Default = 0
Endparam
 
Param Center
  Caption = "Trap Center"
  Hint = "Center of the Elliptical Trap\
          an the Circle Trap"
  Default = (0.0,0.0)
Endparam
 
Func FTFUNC
  Caption = "C Function"
  Hint = "This changes the value of \
          C the pixel, and actually \
          Grabs the point from another \
          plane projected by this function"
  Default = Ident()
Endfunc
 
Param zstart
  Caption = "Z Initial"
  Hint = "This is the starting point of every iteration"
  Default = (0.0,.0577350)
Endparam
 
Param fuzz
  Caption = "Fuzzy Navel"
  Hint = "This is blurring brush"
  Default = 0.0
  Endparam
}

 

Well this brings us to the end of chapter 6.  This tutorial had a lot 
of "leaps of faith" and I hope everyone made it over the crevasse with me.  
So lets recap what we learned.  We learned that you can further define 
Param blocks with min and max.  We learned about the #rand function.  We 
learned how to trouble shoot the formula compiler and how to go to the 
problems.  We looked at how to optimize the start value for an escape time
fractal.  For fun we added a fuzz factor, the first artistic artistic
addition to the formula.