Skip to content

steventhornton/Newton-Fractal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Newton Fractal

This repository contains Matlab code for producing fractals in the complex plane arising from Newton's and Halley's iterations for root-finding. These are referred to as Newton Fractals.

This code unfortunately does not take advantage of the GPU computing capabilities in Matlab as Matlab requires CUDA, which I do not have.

Matlab Requirements

  • The Parallel Computing Toolbox must be installed. If you do not have the parallel computing toolbox install you may modify the code slightly by changing any parfor loops to for loops.
  • The Symbolic Toolbox is needed for providing symbolic expressions to the fractal functions. You can still create fractals by using function handles instead.
  • All contents (files and subfolders) of the src/ directory must be in your Matlab working directory.

Using the Matlab Code

There are two main functions for producing fractals newtonFractal and halleyFractal for Newton's and Halley's methods respectively. These functions are called as:

newtonFractal(f, workingDir, options);
halleyFractal(f, workingDir, options);

Input Function

The first input argument f can be either a function handle or a symbolic expression.

  • Function handle The function must take a row vector as input and return a matrix. The first row in the output matrix represents the values of the function evaluated at the input values. Subsequent rows represent represent derivatives of the function evaluated at the input points. For Newton's method, the first derivative is required (matrix with 2 rows). For Halley's method, the first two derivatives are require (matrix with 3 rows).
  • Symbolic expression The Symbolic Toolbox must be installed within Matlab to use this type of function input. A univariate symbolic function should be provided. It's derivatives will be computed automatically.

The Working Directory

The second input argument workingDir is the location where images will be saved. When either method is called a README.txt file will be written to the working directory with information about the images produced. The images are automatically named as Image-k.png to ensure images are never overwritten.

Options

Option Name Default Details
margin [-1-1i, 1+1i] Must be a struct with keys:
  • bottom
  • top
  • left
  • right
that indicating the margins for the image.
height 250 (pixels) The height (in pixels) of the grid to be used. The width is determined from the margin such that each grid point is square.
maxIter 50 Maximum number of iterations for Newton's or Halley's method
tol 1e-6 Tolerance used to determine if an iteration has converged. The value of the input function at a point must be smaller in magnitude than this value to have converged.
smoothing false When true, exponential smoothing is used to provide a smooth transition in the colors between different number of iterations to convergence.
r 1 Relaxation parameter used for the iterations.
cmap Matlab's hsv colormap. A m x 3 vector of values in [0,1] that specify the colors to be used for the image. Each row is an rgb triple.

Examples

Example 1

% -------------------------------------------------------------------------
% EXAMPLE 1 -- Newton fractal of x^3 - 1.
% -------------------------------------------------------------------------

f = @(x) [x.^3 - 1; % f(x)  = x^3 - 1
          3*x.^2];  % f'(x) = 3x^2

workingDir = '~/Example1/';

newtonFractal(f, workingDir);

Output image:

Newton Fractal Example 1

Example 2

% -------------------------------------------------------------------------
% EXAMPLE 2 -- Newton fractal of x^3 - 1.
%
% Requires the Symbolic Toolbox
% -------------------------------------------------------------------------

syms x

workingDir = '~/Example2/';

newtonFractal(x^3 - 1, workingDir);

Output image:

Newton Fractal Example 2

Example 3

% -------------------------------------------------------------------------
% EXAMPLE 3 -- Halley fractal of x^3 - 1.
% -------------------------------------------------------------------------

f = @(x) [x.^3 - 1; % f(x)   = x^3 - 1
          3*x.^2;   % f'(x)  = 3x^2
          6*x];     % f''(x) = 6x

workingDir = '~/Example3/';

halleyFractal(f, workingDir);

Output image:

Halley Fractal Example 3

Example 4

% -------------------------------------------------------------------------
% EXAMPLE 4 -- Newton fractal of x^3 - 1 with options.
% -------------------------------------------------------------------------

% Margins for the image
margin = struct('bottom', -1.5, ...
                   'top',  1.5, ...
                  'left', -pi,  ...
                 'right',  pi);

% Custom colormap
cmap = [255,   0,   0;
          0, 255,   0;
          0,   0, 255;
          0,   0,   0]/255;

opts = struct('margin', margin, ...
             'maxIter', 50,     ...
                 'tol', 0.01,   ...
              'height', 1000,   ...
                'cmap', cmap);

f = @(x) [sin(x);   % f(x)  = sin(x)
          cos(x)];  % f'(x) = cos(x)

workingDir = '~/Example4/';

newtonFractal(f, workingDir, opts);

Output image:

Newton Fractal Example 4

Example 5

% -------------------------------------------------------------------------
% EXAMPLE 5 -- Halley fractal of x^3 - 1 with relaxation parameter of 2.
% -------------------------------------------------------------------------

margin = struct('bottom', -0.9, ...
                   'top',  0.9, ...
                  'left', -0.9, ...
                 'right',  0.9);

% Custom colormap
cmap = [200, 200, 255;
         80,  80, 220;
          0,   0,   0;
          0,   0,   0]/255;

opts = struct('margin', margin, ...
             'maxIter', 75,     ...
                 'tol', 0.001,  ...
              'height', 1000,   ...
           'smoothing', true,   ...
                   'r', 2,      ...
                'cmap', cmap);

workingDir = '~/Example5/';

syms x

halleyFractal(x^3 - 1, workingDir, opts);

Output image:

Halley Fractal Example 5

About

Matlab code for producing Newton fractals

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages