{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

22 - MATLAB Mandelbrot

# 22 - MATLAB Mandelbrot - Engineering 101 Quote of the Day...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Quote of the Day Education is what survives when what has been learned has been forgotten. - B. F. Skinner program! MATLAB Applications: Mandelbrot Set MATLAB Programming Example: The Mandelbrot Set Programming Example: The Mandelbrot Set Fractals are of interest because, in addition to being mathematically beautiful objects, they have the property of self-similarity. selfIn this respects they are like a number of natural and man-made systems like coastlines and rough mansurfaces. The Mandelbrot set Generated from the equation: The Mandelbrot set Generated from the equation: 2 zm +1 = zm + c c=1 2 zm +1 = zm + c z1=0 z2=1 z3=2 z4=5 z5=26 z6=677 z7=458330 c=0.1 z1=0 z2=0.1 z3=0.11 z4=0.1121 z5=0.11256... 1 The Mandelbrot set We determine the value of a point by determining how many iterations it takes to grow greater than 2 and dividing by the total number of iterations. For example, consider 10 total iterations M(1) = 0.4 M(0.1) = 1 (it never becomes greater than 2) The Mandelbrot set The trick is we do this with complex numbers. So every point in the plane has a real value given by the x axis and an imaginary value given by the y axis. Squares of imaginary numbers are taken in the standard way (1+2i)2 = (1+2i)(1+2i) = 1+2i+2i+4i2 = 1+4i -4 = -3+4i The condition to stop the iteration will be that the norm, absolute value, of the number (the square root of the number times its complex conjugate) is greater than 2. c=1 z1=0 z2=1 z3=2 z4=5 z5=26 z6=677 z7=458330 c=0.1 z1=0 z2=0.1 z3=0.11 z4=0.1121 z5=0.11256... The Mandelbrot set So to make the set we must first write a function that will take as input: a matrix c where each element of the matrix is a complex number a number of iterations, niters The Mandelbrot set function res = mandelbrotIterate (c, niters) z = zeros(size(c)); for i = 1:niters z = z.^2 + c; end It will have to set the initial values of z for each c to 0. Then we must make a loop that will repeatedly apply the equation to each value of z 2 zm +1 = zm + c The Mandelbrot set Since we only want to continue to iterate the z's that are less than 2 we will use a logical array called active The value of active will be 1 if abs(z) is still less than or equal to 2 The Mandelbrot set function res = mandelbrotIterate (c, niters) z = zeros(size(c)); active = ones(size(c)); for i = 1:niters z(active) = z(active).^2 + c(active); active = abs(z) <= 2; end 2 The Mandelbrot set As a final step, we need to make sure than when a site first becomes inactive, the program determines the iteration in which it exceeded 2 and calculates the value of iteration/total iterations for the result, res. res. When a site first becomes inactive, the program must determine the iteration in which it exceeded 2 and calculate the value of iteration/total iterations for the result, res. Which function does this? res. 1 2 The Mandelbrot Set Now we have a function mandelbrotIterate(c, niters) that mandelbrotIterate(c, will calculate the result for a matrix of complex values c over a number of iterations niter Next we must design a function that will create the matrix c, call mandelbrotIterate, mandelbrotIterate, and plot the result in a pseudocolor plot. We will call this function drawMandelbrot and pass it the range [xmin xmax ymin ymax], the resolution (number of boxes in each direction) and the number of iterations (niter). xmin=6, xmax=12, resolution=2 xmin=6, xmax=12, resolution=3 xmin=6, xmax=12, resolution=5 xvals = [6, 12] xvals = [6, 9, 12] xvals = [6, 7.5, 9, 10.5, 12] The Mandelbrot Set function drawMandelbrot(range,resolution,niter) drawMandelbrot(range,resolution,niter) % we will have to create c here res = mandelbrotIterate(c,niter); mandelbrotIterate(c,niter); pcolor(res); shading flat; colormap jet; Which function fragment creates c properly? Recall: range is a vector that holds [xmin xmax ymin ymax] resolution is the number of boxes in each direction Each box in c holds the complex number x+iy Recall: xmin=6, xmax=12, resolution=2 xmin=6, xmax=12, resolution=3 xmin=6, xmax=12, resolution=5 xvals = [6, 12] xvals = [6, 9, 12] xvals = [6, 7.5, 9, 10.5, 12] Mandelbrot set Now if we execute the command drawMandelbrot([drawMandelbrot([-2.5 2.5 -2 2],400,40) we obtain the image: 1 2 3 3 Graphical User Interfaces MATLAB also contains a variety of built in functions to help build graphical user interfaces (GUI). We will use a handful of these to make a GUI front end to our Mandelbrot set program. GUI Methods The function menu( titlestring, choices ) creates a titlestring, menu with a title given by the titlestring and buttons containing the list of strings given by choices and returns the number of the one chosen. mandelbrotgui function mandelbrotgui stack = [-1.8 1.8 -1.3 1.3]; [f = figure; res = 400; niter = 30; while 1 coords = stack(end,:); drawMandelbrot(coords, res, niter); choice = menu('Which one?', 'Zoom', 'Pan Out', 'Quit'); switch choice case 1 % Zoom case 2 % Pan Out case 3 % Quit break; end end GUI Methods The function waitforbuttonpress pauses and waits for the user to press the button. The function get(h, propertyname) returns the property propertyname) named associated with the handle h. The handle gca refers to the currently active figure. The propertyname `CurrentPoint' refers to the location CurrentPoint' in the figure. The function rbbox tracks a rubberband box. Case1: Zoom case 1 waitforbuttonpress; waitforbuttonpress; point1 = get(gca,'CurrentPoint'); get(gca,'CurrentPoint'); % button down detected rbbox; rbbox; point2 = get(gca,'CurrentPoint'); get(gca,'CurrentPoint'); % button up detected stack(end+1,:)= [point1(1,1) point2(1,1) point1(1,2) point2(1,2)]; case 2 Case2: Pan Out if size(stack,1) > 1 stack = stack(1:(end-1),:); stack(1:(endelse midx = mean(stack(1,1:2)); midy = mean(stack(1,3:4)); lenx = abs(diff(stack(1,1:2))); leny = abs(diff(stack(1,3:4))); stack = [midx+lenx*[-1,1], midy+leny*[-1,1]]; [midx+lenx*[midy+leny*[end 4 mandelbrotgui.m Code Available on CTools Code for mandelbrotIterate, drawMandelbrot mandelbrotIterate, and mandelbrotgui are available on the course website resources in the MATLAB folder. Next Lecture Introduction to Parallel Programming 5 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online