Search code examples
matlabgraphplotfractals

Plotting iteratively defined function in MATLAB


I'm not a MATLAB professional and so, I need some help at producing a 3D plot of the iteratively defined function f : R^2-0 -> R defined below (in pseudocode) for x,y values in [-1,1] and

For each I = 1,2,3 and A = (0.5,0.5)

function  f(v in R^2-0)
{
    a=b=0;     (a,b in R)        
    for (i=0; i<I; i=i+1)
    {
        v = |v| / ||v||^2 - A;
        a = a + | ||v||-b |;
        b = ||v||;
    }
    return a;
}

(|v| denotes component-wise vector absolute value)

(If you want you can look at the fractal that is generated by the function at my question on math-exchange here:

https://math.stackexchange.com/questions/1457733/a-question-about-a-fractal-like-iteratively-defined-function )

MATLAB code to do that will be appreciated.

Much Thanks.


Solution

  • Save this your main program:

    clear
    clc
    close all
    
    % I = 1;
    % A = [ 0.5 0.5 ];
    
    I = 10;
    A = [ 0.5 0.5 0.5 ];
    
    xmin = -1;
    xmax = 1;
    
    ymin = -1;
    ymax = 1;
    
    nx = 101;
    ny = 101;
    
    dx = (xmax - xmin) / (nx - 1);
    dy = (ymax - ymin) / (ny - 1);
    
    x = xmin: dx: xmax;
    y = ymin: dy: ymax;
    
    for ix = 1: nx
        for iy = 1: ny
            if (length(A) == 2)
                z(iy, ix) = f([x(ix) y(iy)], A, I);
            elseif (length(A) == 3)
                z(iy, ix) = f([x(ix) y(iy) 0], A, I);
            end
        end
    end
    
    pcolor(x, y, z)
    shading interp
    

    Then save this function in the same directory as the main program as f.m:

    function result = f(v, A, I)
    
    a = 0;
    b = 0;
    
    for i = 1: I
        v = abs(v) / dot(v, v) - A;
        a = a + abs(norm(v) - b);
        b = norm(v);
    end
    result = a;
    
    end