Blogger Widgets

Search This Blog

Saturday, 10 March 2012

Fuzzy heart fractal with matlab


% Fuzzy heart-shaped Mandelbrot fractal
% Matius Fanny
% October 2009

% iterations
n = 300;
% resolution
N = 200;
% Create grid
x = linspace(-1, 1, N);
y = linspace(-1.4, .6, N);
[X,Y] = meshgrid(x, y);

Z = X + i*Y;
Zn = Z;

figure
for j=1:n
  % Mandelbrot map with random noise
  Zn = -i*(Zn).^2 +  (rand(N,N).^(1/5)).*Z;
  M = abs(Zn);
  ind1 = find(M<2);
  ind2 = find(M>=2);
  M(ind1) = 0;
  M(ind2) = -1;
  m = 1;
  imagesc(x, y, (M/m))
  colormap([1 1 1; 1 0 0])
  set(gca,'YDir','normal')
  title(n-j)
  pause(0.01)
end
title('Love or whatever', 'fontsize', 16)



%code to plot a heart shape in MATLAB
%set up mesh
n=100;
x=linspace(-3,3,n);
y=linspace(-3,3,n);
z=linspace(-3,3,n);
[X,Y,Z]=ndgrid(x,y,z);
%Compute function at every point in mesh
F=320 * ((-X.^2 .* Z.^3 -9.*Y.^2.*Z.^3/80) + (X.^2 + 9.* Y.^2/4 + Z.^2-1).^3);
%generate plot
isosurface(F,0)
view([-67.5 2]);
Did you know that the equation for a heart (or a cardioid if you want to get technical) is very similar to the equation for a flower?  The polar equation you need is  http://demonstrations.wolfram.com/ThePolarEquationsOfHeartsAndFlowers/HTMLImages/index.en/1.gifand you get a rotated cardioid for n=1.  Change n to 6 and you get a flower.  Let’s use the free maths package, SAGE, this time.
First, define the function:
def eqn(x,n):
    return(1-sin(n*x))
then plot it for n=1
polar_plot(eqn(x,1), (x,-pi, pi),aspect_ratio=1,axes=False)


polar_plot(eqn(x,7), (x,-pi, pi),aspect_ratio=1,axes=False)


% gtenerates a fractal 
% pjfrench@imperial.ac.uk
clear all
close all
rand('state',sum(100*clock))
 
% three starting points:
P1 = [50,100];
P2 = [0, 0];
P3 = [100,0];
Steps = input('How many steps: ');  % no. of steps
count = 0;                          % internal count
S = floor(rand(1,2)*100);           % starting point
 
 
%initial plot before iteration
figure(1)
axis off
hold on
plot(P1(1),P1(2),'r.')
plot(P2(1),P2(2),'r.')
plot(P3(1),P3(2),'r.')
plot(S(1),S(2),'k.')
 
 
while(count <= Steps)
    Dice = 1 + floor(rand(1)*6);
    if ( (Dice == 1) || (Dice == 2) )
        x = S(1) + (P1(1)-S(1))*0.5;
        y = S(2) + (P1(2)-S(2))*0.5;
    elseif ( (Dice == 3) || (Dice == 4) )
        x = S(1) + (P2(1)-S(1))*0.5;
        y = S(2) + (P2(2)-S(2))*0.5;
    else
        x = S(1) + (P3(1)-S(1))*0.5;
        y = S(2) + (P3(2)-S(2))*0.5;
    end
    plot(x,y,'k-')
    S(1) = x;
    S(2) = y;
    count = count + 1;   
end

No comments:

Post a Comment