16
Math 4330 Sec. 1, Matlab Assignment # 5 , April 28, 2006 Name 1 Discrete Systems and Iteration of Mappings In this last assignment we return to the topic of discrete dynamical systems x(0) = x 0 , and x(k + 1) = f (x(k)). The goal is to consider the topics covered in the notes on discrete dynamical systems, this time with an emphasis on using Matlab to do the calculations. In our introduction to Matlab we have learned how to locate zeros of functions using fzero and also fminbnd. This is useful in locating fixed points of the dynamical system. Since there is so little time left in the semester perhaps it is better to cut to the chase and go directly to bifurcations, Julia and Mandelbrodt sets. 1.1 Fixed Points and Stability Let us consider the same example we looked at earlier in the discrete dynamical systems notes. Namely we consider the function f (x)= 1 4 (4 + x - x 2 ). There is a fixed point of f (i.e., a solution to the equation f (x)= x) at x = 1. In Matlab we can consider the question of stability or instability of the fixed point by plotting the “cobweb” diagram and by plotting trajectories using Matlab. 0.8 0.9 1 1.1 1.2 0.85 0.9 0.95 1 1.05 1.1 The graph of the function f (x)= 1 4 (4 + x - x 2 ). The line y = x and the iterates from two initial points fixed points x 0 = .85 and x 0 =1.1. It would appear that the fixed point at x = 1 is stable. 1

Article de Ayuda Matlab

Embed Size (px)

DESCRIPTION

sofware matlab

Citation preview

  • Math 4330 Sec. 1, Matlab Assignment # 5 , April 28, 2006 Name

    1 Discrete Systems and Iteration of Mappings

    In this last assignment we return to the topic of discrete dynamical systems

    x(0) = x0, and

    x(k + 1) = f(x(k)).

    The goal is to consider the topics covered in the notes on discrete dynamical systems, this time

    with an emphasis on using Matlab to do the calculations.

    In our introduction to Matlab we have learned how to locate zeros of functions using fzero and

    also fminbnd. This is useful in locating fixed points of the dynamical system.

    Since there is so little time left in the semester perhaps it is better to cut to the chase and go

    directly to bifurcations, Julia and Mandelbrodt sets.

    1.1 Fixed Points and Stability

    Let us consider the same example we looked at earlier in the discrete dynamical systems notes.

    Namely we consider the function f(x) = 14(4 + x x2). There is a fixed point of f (i.e., a solution

    to the equation f(x) = x) at x = 1.

    In Matlab we can consider the question of stability or instability of the fixed point by plotting

    the cobweb diagram and by plotting trajectories using Matlab.

    0.8 0.9 1 1.1 1.20.85

    0.9

    0.95

    1

    1.05

    1.1

    The graph of the function f(x) = 14(4 + x x2). The

    line y = x and the iterates from two initial points fixedpoints x0 = .85 and x0 = 1.1. It would appear that thefixed point at x = 1 is stable.

    1

  • fn=inline(1/4*(4+x-x.^2),x);

    fxpt=inline(1/4*(4+x-x.^2)-x,x);

    fzero(fxpt,0,1)

    xmin=.8;

    xmax=1.2;

    N=6;

    dx=(xmax-xmin)/1000;

    x=xmin:dx:xmax;

    y=fn(x);

    plot(x,y,b)

    set(gca,FontSize,18)

    hold on

    plot([xmin xmax],[.8 .8],k,Linewidth,1.5)

    plot([.8 .8], [min(y)-.1 max(y)+.1],k,Linewidth,1.5)

    plot([xmin xmax],[xmin xmax],g);

    x0=.85;

    Y=[x0];

    xx=x0;

    for i=1:N

    yy=fn(xx);

    Y=[Y yy];

    xx=yy;

    end;

    YY(1)=Y(1);

    for i=1:N

    XX(2*i-1)=Y(i);

    XX(2*i)=Y(i);

    YY(2*i)=Y(i+1);

    YY(2*i+1)=Y(i+1);

    end;

    XX(2*N+1)=Y(N+1);

    plot(XX,YY,r,LineWidth,1.5);

    axis([xmin xmax min(y)-.1 max(y)+.1])

    grid on

    hold off

    1.2 Bifurcation, Period Doubling, etc

    Recall that a bifurcation is a sudden change in the number or nature of the fixed and periodic points

    of the system. Fixed points may appear or disappear, change their stability, or even break apart

    into periodic points! Consider the functions fa(x) = x2 a.

    2

  • clear all

    fn=inline(x.^2-aa,x,aa);

    x0=.1; a0=.5; af=2.25; N=100;

    clf; hold on %clear graphs and draw points on the same graph

    deltaa = (af - a0)/N; aa = a0; x=x0;

    count=0;

    for i = 1:150

    x= fn(x,aa);

    if i>100

    count=count+1;

    b(count)=x;

    end

    end;

    aa_one=aa*ones(size(b));

    plot(aa_one,b,.)

    drawnow

    %increment the parameter value and repeat until done

    for j = 1:N

    count=0;

    aa=aa + deltaa;

    x=x0;

    for i = 1:200

    x=fn(x,aa);

    if i>100

    count=count+1;

    b(count)=x;

    end

    end;

    aa_one=aa*ones(100);

    plot(aa_one,b,.)

    drawnow

    clear b;

    end;

    grid on

    set(gca,FontSize,18)

    axis([.5 2 -2 2])

    xlabel(a)

    ylabel(x,rotation,0)

    hold off

    Note that this is slightly different than the example from the Dynamical Systems Notes 4 where

    we studied fa(x) = x2 + a. The only difference is that a is replaced by a.

    We know that this system undergoes periodic doubling to chaos as a varies from .5 to 2.

    3

  • 0.5 1 1.5 22

    1.5

    1

    0.5

    0

    0.5

    1

    1.5

    2

    a

    x

    Bifurcation for the quadratic map

    1.3 Julia Sets

    This material in this section is taken from the outstanding text An Invitation to Dynamical Systems

    by Edward R. Scheinerman (Professor of Applied Mathematics and Statistics at the Johns Hopkins

    University). The text can be obtained at http://www.ams.jhu.edu/ers/invite.html.We now invite the return of complex numbers to our study of dynamical systems. We will

    explore discrete time dynamical system in one complex variable. In other words,we ask, What

    happens when we iterate a function f(z) where z may be a complex number? In several previous

    sections we considered the family of functions fa(x) = x2 + a.

    For this family we define

    Ba ={z : |fka (z)| 6 as k

    }and

    Ua ={z : |fka (z)| as k

    }Note that Ba and Ua are complementary sets. The boundary between these sets is denoted Ja. The

    set Ja is called the Julia set of the function fa, and the set Ba is called the filled-in Julia set of fa.

    We can make a picture of the set Ba as follows. Every complex number z corresponds to a point

    in the plane. We can plot a point for every element of Ba and thereby produce a two-dimensional

    depiction of Ba.

    How do we compute pictures of Julia sets

    4

  • The method is fairly straightforward. We fix a complex number a and ask, Is a given complex

    number z in Ba? To answer this, we pick a big number k and check if |fk(z)| is large. The issue is,what do big and large mean?

    We say |fk(z)| is large if |fk(z)| is larger than both 2 and |a|. Typically (but not always) wehave |a| < 2, so we simply require |fk(z)| > 2. Now you might say 2 is not very large so lets seewhy this works.

    We claim that if |z| > 2 and |z| a, then |fk(z)| explodes as k , i.e. we claim that if, aswe iterate f starting at z, we ever reach a point whose absolute value is bigger than 2 (or at least

    |a|), then we are sure that |fk(z)| .To see why this is true suppose |z| > 2 (and |z| > |a|). Since |z| > 2, we know that |z| 2 +

    for some fixed positive number . Now we compute

    |f(z)| = |z2 + a| by definition of f |z2| |a| since |z2 + a|+ | a| |z2| |z2| |z| since |z| |a|= |z|(|z| 1) factoring |z|(1 + ) since |z| 2 + .

    It now follows by repeated use of this reasoning that

    |fk(z)| |z|(1 + )k ,

    and therefore, z Ua.Thus to test if z Ba, we compute fk(z) for k = 1, 2, . . ., and if we ever find

    |fk(z)| max{a, |a|},

    then we know z Ua.There is a problem with this test strategy. Suppose z Ba : How many iterations k of f should

    we compute before we decide that z 6 Ba? This is a harder question to answer. The answer dependson which you, the person making the computation, value more: speed or accuracy.

    If speed is very important, then a modest value of k (such as 20) gives acceptable Speed versus

    accuracy. results. There will be some points which you will misidentify (you will think they are

    in Ba , but are they are really in Ua ), but you will get a large percentage correct. If accuracy

    is very important, then a large value of k (such as 1000) gives good results. You will make far

    fewer mistakes, but your computer will crank away much longer, and the pictures will take more

    time to produce. Related to this issue is the magnification/resolution you are trying to achieve.

    Magnification versus resolution. If you simply want a coarse overview picture, then a small number

    of iterations suffices. On the other hand, if you are trying to produce a zoomed-in, high-resolution

    image, then a large value of k is important.

    We assemble these ideas into an algorithm for computing Julia set images

    5

  • Escape-Time Algorithm for Julia Sets

    Inputs:a) The complex number a for f(z) = z2 + a.

    b) The four numbers xmin, xmax, ymin, ymax representing the region of the complexplane in which you are doing your computations.

    Lower-left corner at xmin + ymin i, and upper-right corner at xmax + ymax i.

    c) A positive integer MAXITS. This is the maximum number of iterations of f youcompute before you assume the point in question is in Ba.

    Procedure:1. For all values of z = x + yi with xmin x xmax and ymin y ymax do

    the following steps:

    (a) Let z = z (a temporary copy).

    (b) Let k = 0 (iteration counter).

    (c) While |z| 2 and |z?| |a| and k < MAXITS do the following steps:(i) Let z = f(z) = (z)2 + a (compute the next iterate).

    (ii) Let k = k + 1 (increment the step counter).

    (d) IF |z| 2 and |z| |a| (i.e., if you have fully iterated without breaking outof bounds) THEN plot the point z (i.e., (x, y)) on the screen.

    If the width of the screen is 200 pixels, then we want to take 200 values of x evenly spaced

    between xmin and xmax . To prevent distortion, the shape of the plotting rectangle should be in

    proportion to the dimensions of the region (xmax xmin ymax ymin). We say plot thepoint; exactly how this is done depends on the computer system you are using.

    There is a natural and aesthetically pleasing way to add color to your plots of Julia sets. When

    you compute the iterations fk(z), record the first number k for which fk(z) is out of bounds, i.e.,

    has large absolute value. That number k is called the escape-time of the point z . Now, if z Ba wenever have |fk(z)| large, so its escape-time is infinite (or, effectively, MAXITS). We now want to plota point on the screen for all points z in the rectangular region (xmin x xmax and ymin y ymax). For points in Ba (those whose escape-time we compute to be MAXITS) we plot a blackdot. For all other points z (those z Ua) we plot a color point depending on z s escape-time. Forexample, we could follow the usual spectrum from red to violet. When k = 0, we plot a red point,

    and as k increases to MAXITS 1, we plot various colors of the rainbow through to violet.

    6

  • % This is a mfile to generate a Julia set.

    % c1, c2 c3 give nice pictures

    c1=-.75; c2= .375+.333*i; c3= -.117-.856*i;

    c=c1; %choose one

    if abs(c)>=2

    disp(Warning: Using a parameter with absolute value)

    disp(greater than 2 is not recommended.);

    end;

    z0=-1.5-1.5*i; z1=1.5+1.5*i;

    x0 = real(z0); y0 = imag(z0);

    x1 = real(z1); y1 = imag(z1);

    maxsteps=30; hres=300;

    vres = ceil( abs( hres * (y1-y0)/(x1-x0)) );

    dx = (x1-x0)/hres;

    dy = (y1-y0)/vres;

    [X,Y] = meshgrid(x0:dx:x1, y0:dy:y1);

    z = X + Y*i;

    c = c*ones(size(z));

    pict = 0*z;

    clear X Y

    for k=1:maxsteps

    bigs = find(abs(z)>=2);

    pict(bigs) = k*ones(size(bigs));

    z(bigs) = zeros(size(bigs));

    c(bigs) = zeros(size(bigs));

    z = z.^2 + c;

    end;

    image(flipud(pict))

    axis(image)

    axis(xy)

    axis(off)

    colormap(jet(maxsteps))

    %colormap(hsv(maxsteps)) %also try this

    7

  • c = .75

    c = .375 + .333i

    c = .117 .856i

    8

  • There are many alternatives to this method of computing and plotting the Julia set. We give

    one simple example.

    maxstep=30; m=400;

    cx=0; cy=0; %center of a rectangle

    l=1.5; % width of sides of rectangle

    x=linspace(cx-l,cx+l,m);

    y=linspace(cy-l,cy+l,m);

    [X,Y]=meshgrid(x,y);

    c1=-.75; c2= .375+.333*i; c3= -.117-.856*i;

    c=c1; %choose one

    %c= -.745429;

    Z=X+i*Y;

    for k=1:maxstep;

    Z=Z.^2+c;

    W=exp(-abs(Z));

    end

    colormap prism(256)

    pcolor(W);

    shading flat;

    axis(square,equal,off);

    In this method we use the graphics plotting commands pcolor, colormap style prism, and shading

    set to flat. There are many different options and if you look at help on each of these you will find

    many other interesting possibilities.

    9

  • c = .75

    c = .375 + .333i

    c = .117 .856i

    10

  • 1.4 Mandelbrodt Sets

    If you create Julia sets, you may note that for some values of a the set Ba is very sparse (called

    fractal dust) and for some values of a the set Ba is a large connected region. There is a simple way

    to decide which situation you are in: Iterate f starting at z = 0. If fk(0) remains bounded, then

    the set Ba will be connected, but if |fk(0)| , then Ba will be fractal dust. The justification ofthis fact is beyond the scope of this course.

    This leads to a natural question, For which values a does fka (0) remain bounded and for which

    values of a does it explode? The Mandelbrot set, denoted by M, is the set of values a for whichfka (0) remains bounded, i.e.,

    M = {a C : |fka (0)| 6 }.For complex values of a it might be reasonable to expect that M has a simple appearance.

    Instead, we see that M looks like the image in Figure

    The Mandelbrot set M using pcolor.

    The Mandelbrot set M using image.

    11

  • The antenna sticking out to the left runs along the real (x)axis to 2. There appears to be alittle blip toward the left end of the antenna. If we magnify this portion only to find another little

    copy of M itself.

    There is a lot to explore here. We will use Matlab to do this exploration. The idea is the same

    as the escape-time algorithm for computing Julia sets. First, we note that if |a| > 2, then a 6 M.To see why, note that f(0) = a, and now we are iterating f at a point (a) whose absolute value is

    at least |a| and greater than 2. Thus by the reasoning as above (for Julia sets) we conclude that|fk(0)| . If for some k we have |fk(0)| > 2, then we know that a 6 M. On the other hand,if |fk(0)| 2 for a large value of k, it is reasonable to suppose that a M. We again have thespeed/accuracy trade off. For coarse, quick pictures, you can iterate f a modest number of times,

    but for high-resolution/magnification images, a large number of iterations should be performed.

    The escape-time algorithm for computing images of the Mandelbrot set is virtually identical

    with the algorithm for computing Julia sets.

    Escape-Time Algorithm for Mandelbrodt Set

    Inputs:a) The four numbers xmin, xmax, ymin, ymax representing the region of the complex

    plane in which you are doing your computations.

    Lower-left corner at xmin + ymin i, and upper-right corner at xmax + ymax i.

    b) A positive integer MAXITS. This is the maximum number of iterations of f (start-ing at 0) you compute before you assume the point in question is in M.

    Procedure:1. For all values of a = x + yi with xmin x xmax and ymin y ymax do

    the following steps:

    (a) Let z = 0 (a temporary copy).

    (b) Let k = 0 (iteration counter).

    (c) While |z| 2 and |z?| |a| and k < MAXITS do the following steps:(i) Let z = f(z) = (z)2 + a (compute the next iterate).

    (ii) Let k = k + 1 (increment the step counter).

    (d) IF |z| 2 and |z| |a| (i.e., if you have fully iterated without breaking outof bounds) THEN plot the point z (i.e., (x, y)) on the screen.

    12

  • z0=-2-2*i; z1=2+2*i;

    x0 = real(z0); y0 = imag(z0);

    x1 = real(z1); y1 = imag(z1);

    maxsteps = 30;

    hres = 400;

    vres = ceil( abs( hres * (y1-y0)/(x1-x0)) );

    dx = (x1-x0)/hres;

    dy = (y1-y0)/vres;

    [X,Y] = meshgrid(x0:dx:x1, y0:dy:y1);

    c = X + Y*i;

    z = c;

    pict = 0*z;

    clear X Y

    for k=1:maxsteps

    bigs = find(abs(z)>=2);

    pict(bigs) = k*ones(size(bigs));

    z(bigs) = zeros(size(bigs));

    c(bigs) = zeros(size(bigs));

    z = z.^2 + c;

    end;

    image(flipud(pict))

    axis(image)

    axis(xy)

    axis(off)

    colormap(jet(maxsteps))

    Mandelbrodt Code Using image

    Once again there are many alternatives to this method of computing and plotting the Mandel-

    brodt set. We give one simple example. In this method we use the graphics plotting commands

    pcolor, colormap style copper, and shading set to flat. There are many different options and if you

    look at help on each of these you will find many other interesting possibilities.

    13

  • maxstep=20;

    m=400;

    cx=-.6;

    cy=0;

    l=1.5;

    x=linspace(cx-l,cx+l,m);

    y=linspace(cy-l,cy+l,m);

    [X,Y]=meshgrid(x,y);

    Z=zeros(m);

    C=X+i*Y;

    for k=1:maxstep;

    Z=Z.^2+C;

    W=exp(-abs(Z));

    end

    colormap copper(256);

    pcolor(W);

    shading flat;

    axis(square,equal,off);

    Mandelbrodt Code Using pcolor

    14

  • The Mandelbrot set is a sort of book each page of which is a picture of a Julia set, corresponding

    to a value of the parameter c identifying a point of the Mandelbrot set. (taken from http://www.

    ciram.unibo.it/strumia/Menu.html).In a more mathematical language we say that each Julia set may be represented by a point in a

    complex parameter space: each point of this space lies inside the Mandelbrot set if the Julia set is

    connected, while it lies outside the Mandelbrot set when the corresponding Julia set is not connected

    (Fatou dust). When the point labelled by the parameter c runs along the fractal boundary of the

    Mandelbrot set, the Julia set modifies its shape in a very elegant way.

    The shapes of the Julia sets corresponding to values of a moving in a neighbourhood of the

    boundary of the Mandelbrot set vary dramatically. The following is a picture taken of a graphic

    found at the http://www.ciram.unibo.it/strumia/Menu.html.

    15

  • ASSIGNMENT 5 Math 4330

    1. The function f(x) = 1/x has a fixed point at x = 1. Use a cobweb diagram to determine whether

    the fixed point is stable or unstable. Print out your matlab program and state whether the fixed

    point is stable or unstable.

    2. Write a matlab program to draw the bifurcation diagram for the f(x) = a cos(x) as a ranges

    from a0 = 1 to af = 2.25. Use a starting value of x0 = 1. Print out your code and a plot of the

    bifurcation diagram.

    3. Use a matlab code to generate the Julia set for the function f(z) = z2 + a for each of the values

    of a. Print out your matlab code and a plot of the Julia set.

    (a) a = .85 + .18i(b) a = 1.24 + .15i(c) a = .16 + .74i

    4. Exercises for Madelbrodt set. Modify either of the above codes to generate the Mandelbrodt set

    for the following functions f . For each function print out the matlab program and a plot of the

    Madelbrodt set.

    (a) f(z) = z3 + a

    (b) f(z) = z4 + a

    (c) f(z) = z5 + a

    References

    [1] The Matlab Primer, Kermit Sigmon

    [2] Introduction to scientific computing: a matrix vector approach using Matlab, Printice Hall,

    1997, Charles Van Loan

    [3] Mastering Matlab, Printice Hall, 1996, Duane Hanselman and Bruce Littlefield

    [4] Engineering Problem Solving with Matlab, Printice Hall, 1993, D.M Etter

    [5] Invitation to Dynamical Systems, (Prentice-Hall out-of-print) Edward R. Scheinerman, Full

    text available at http://www.ams.jhu.edu/ers/invite.html.

    16