MATLAB - Image Processing - Douday's rabbit fractal

AI Thread Summary
The discussion focuses on generating Douady's rabbit fractal using MATLAB, specifically the quadratic Julia set defined by z(n+1) = z(n)^2 + c, with c = -0.123 + 0.745i. Participants address issues with the initial code, particularly the initialization of the 'map' variable and the logic for determining when to update it based on the condition of z exceeding a threshold. Suggestions include starting the 'map' with zeros to avoid monochrome outputs and ensuring that the code correctly captures the fractal's complexity. The conversation also touches on labeling axes for the generated images and understanding the iterative nature of the fractal generation process. Overall, the thread emphasizes troubleshooting and refining the code to achieve the desired fractal visualization.
GreenPrint
Messages
1,186
Reaction score
0

Homework Statement



13.2 A quadratic Julia set has the form

z(n+1)=z(n)^2+c

The special case where c=-.123+.745i is called Douday's rabbit fractal. Follow example 13.1, and create an image using this value of c. For the Madelbrot image, we started with all z-values equal to 0. You'll need to start with z=x+yi. Let both x and y vary from -1.5 to 1.5.

Homework Equations





The Attempt at a Solution



I'm really confused by this question. Here's my attempt at a solution. I really hope someone can point me in the right direction. My book did a very bad explaining the matter.

Code:
>> iterations=80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.5,grid_size),linspace(-1.5,1.5,grid_size));
c=-.123+.745*i;
z=x+y*i;
map=x+y*i;
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)>sqrt(5));
    map(a)=k;
end
figure(1)
image(map)
colormap(jet)
? Error using ==> image
Error using ==> image
Image CData can not be complex
 
Physics news on Phys.org
Should I have posted this in the calculus and beyond section?
 
Hi,
Does anybody has idea about lbp(local binary pattern)?
 
No sorry I don't.
 
I feel less bad for not knowing how to solve this problem lol
 
I'm serious.
 
So am I have no clue.
 
Hi Greenprint. Your immediate problem is in this line, "map=x+y*i;".

You have a secondary problem, that will cause your printout to be monochrome (but otherwise correct) in the code related to "map(a)=k;"

But work on the immediate problem first. Can you think of a better way to initialize the map?
 
I'm glad I ran into someone who's familiar with this topic, I guess there aren't many. This problem was my first exposure to Mandelbrot sets and of the such and my book didn't do that great of a job explaining this subject matter in the least.

Simply removing "map=x+y*i;" does seem to produce a result. I don't know why I included that in there. I used the example in my book as a guide as I'm sort of lost on this subject.

This seems to produce a image like you suggested.
Code:
iterations=80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.5,grid_size),linspace(-1.5,1.5,grid_size));
c=-.123+.745*i;
z=x+y*i;
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)>sqrt(5));
    map(a)=k;
end
figure(1)
image(map)
colormap(jet)

Code:
%Example 13.1 Mandelbrot Image
clear, clc
iterations = 80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.0,gird_size),linspace(-1.5,1.5,grid_size));
c=x+i*y;
z=zeros(size(x));           %set the intial matrix to 0
map=zeros(size(x));         %create a map of all grid points equal to 0
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)>sqrt(5)); %Determine which elements have exceeded sqrt(5)
    map(a)=k;
end
figure(1)
image(map)                  %Create an image
colormap(jet)

I did some more research on Mandelbrot images and must say that it's a rather fascinating topic. I have a better understanding of what exactly Mandelbrot images are but as far as creating them, not so much. I will see if I can dissect this some, as far as initializing the map, no clue.
 
Last edited:
  • #10
Code:
iterations=80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.5,grid_size),linspace(-1.5,1.5,grid_size));
c=-.123+.745*i;
z=x+y*i;
map=zeros(size(x));
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)>sqrt(5));
    map(a)=k;
end
figure(1)
image(map)
colormap(jet)
Don't know if it's right or not.
 

Attachments

  • Capture.JPG
    Capture.JPG
    35.7 KB · Views: 713
  • #11
http://mathworld.wolfram.com/DouadysRabbitFractal.html
It would appear as if it's correct. It's just my image is rotated a bit I guess lol. If I wanted to label the axises, whatever the plural form of axis is, what exactly would I put for labels. I can't seem to find a similar plot with labels.
 
  • #12
"Simply removing "map=x+y*i;" does seem to produce a result"

Yeah you don't really want to just remove it. You want to initialize it with all zeros.
 
  • #13
"Don't know if it's right or not."

Yes that looks right, but I don't know how you're getting more than two color levels. There is still a mistake near the line "map(a)=k" that will cause it to constantly overwrite parts of the map that have a lower "k" value with whatever the largest value of "k" is. Effectively giving you only a monochrome image.

Perhaps the map initialization problem (failure to initialize) is masking the other problem.
 
  • #14
I'm not exactly sure.

Code:
z=x+y*i;
map=zeros(size(x));

My best bet is that there's something wrong with these lines, the only reason why I did "map=zeros(size(x))" was because it is what the example had in their problem, but that was when z was set equaled to the same exact vector. In the example they are essentially setting the map = z but doing so with my code doesn't produce any results.

Something tells me that instead of setting the map equal to k in the loop I'm suppose to use k for some other aspect like
z(k)=z.^2+c;
z(k)=z(k).^2+c;
z=z(k).^2+c;
but an image is not produce with either of these options

Something seems weird about imputing a into the map function map(a)=k;

I'm really unsure...
 
  • #15
Code:
iterations=80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.5,grid_size),linspace(-1.5,1.5,grid_size));
c=-.123+.745*i;
z=x+y.*i;
map=zeros(grid_size,grid_size);
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)>sqrt(5));
    map(a)=k;
end
figure(1)
image(map)
title('Douday''s rabbit fractal')
colormap(jet)
Is this better?
 
  • #16
Yeah that's good. And are you getting a monochrome (two tone) fractal now?
 
  • #17
Yes. I'm oddly getting the same image that I did before I made the change.

Also what exactly would be the proper x and y labels on these types of graphs?
 
Last edited:
  • #18
GreenPrint said:
Yes. I'm oddly getting the same image that I did before I made the change.

Also what exactly would be the proper x and y labels on these types of graphs?

Code:
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)>sqrt(5));
    map(a)=k;

Once abs(z) exceeds sqrt(5) on one iteration, it will exceed sqrt(5) on every subsequent iteration. Do you agree?

So the set of all points where |z|>sqrt(5) at a given iteration level "k" is a superset of that from all previous iterations. Do you agree?

So that section of your code keeps overwriting previously written elements of "map" with newer larger values of "k". This means that in the end "map" only has two levels, zero (or whatever map was initialized with) and the final value of "k". That's the reason that I think your map can only be monochrome. Are you sure you haven't changed the code subtly and not told us? Like changing the ">" for "<" for example (hint hint).
 
  • #19
No I did not accidentally change a thing

Code:
iterations=80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.5,grid_size),linspace(-1.5,1.5,grid_size));
c=-.123+.745*i;
z=x+y*i;
map=zeros(size(x));
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)>sqrt(5));
    map(a)=k;
end
figure(1)
image(map)
colormap(jet)
both codes produce the same exact image, at least from what I can see
Code:
iterations=80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.5,grid_size),linspace(-1.5,1.5,grid_size));
c=-.123+.745*i;
z=x+y.*i;
map=zeros(grid_size,grid_size);
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)>sqrt(5));
    map(a)=k;
end
figure(1)
image(map)
title('Douday''s rabbit fractal')
colormap(jet)

you can copy and paste and run both codes for yourself

and yes I agree to both statements

your suggested code produces the same results except the inside is colored red instead of black
Code:
iterations=80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.5,grid_size),linspace(-1.5,1.5,grid_size));
c=-.123+.745*i;
z=x+y.*i;
map=zeros(grid_size,grid_size);
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)<sqrt(5));
    map(a)=k;
end
figure(1)
image(map)
title('Douday''s rabbit fractal')
colormap(jet)
Is that what you meant by monochrome?
 
Last edited:
  • #20
GreenPrint said:
No I did not accidentally change a thing

- your suggested code produces the same results except the inside is colored red instead of black

- Is that what you meant by monochrome?

Yes that's monochrome, it basically means there are only two colors (as in a single color foreground and single color background).

The image you showed in your attachment at reply #10 in this thread was not just black and red though, it had multiple colors and shades. It was from this that I couldn't understand how your code was producing those results. Can you explain where the results in the mentioned attachment can from?
 
  • #21
Done Editing This Post =)

code #1: see attachment #1 of this post
Code:
iterations=80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.5,grid_size),linspace(-1.5,1.5,grid_size));
c=-.123+.745*i;
z=x+y*i;
map=zeros(size(x));
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)>sqrt(5));
    map(a)=k;
end
figure(1)
image(map)
colormap(jet)

code #2: see attachment #2 of this post
Code:
iterations=80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.5,grid_size),linspace(-1.5,1.5,grid_size));
c=-.123+.745*i;
z=x+y.*i;
map=zeros(grid_size,grid_size);
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)>sqrt(5));
    map(a)=k;
end
figure(1)
image(map)
title('Douday''s rabbit fractal')
colormap(jet)

code #3: see attachment #3 of this post
Code:
iterations=80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.5,grid_size),linspace(-1.5,1.5,grid_size));
c=-.123+.745*i;
z=x+y.*i;
map=zeros(grid_size,grid_size);
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)<sqrt(5));
    map(a)=k;
end
figure(1)
image(map)
title('Douday''s rabbit fractal')
colormap(jet)

If your asking me to explain the results of the first two codes. I can't really. I just followed the example in my textbook. While my actual understanding of this topic in general is sufficient to understand the topic. I was completely lost in coming up with the code. If it wasn't for the example in my textbook I wouldn't have come up with one. I have no idea why the first two codes come up with the results that they do.

code 4: This is the example from my text
the results for the example are after the code
Code:
%Example 13.1 Mandelbrot Image
clear, clc
iterations = 80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.0,grid_size),linspace(-1.5,1.5,grid_size));
c=x+i*y;
z=zeros(size(x));           %set the intial matrix to 0
map=zeros(size(x));         %create a map of all grid points equal to 0
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)>sqrt(5)); %Determine which elements have exceeded sqrt(5)
    map(a)=k;
end
figure(1)
image(map)                  %Create an image
colormap(jet)
THIS IS FOR CODE 4
[PLAIN]http://img20.imageshack.us/img20/7077/capture4pu.jpg
THIS IS FOR CODE 4

Just out of curiosity. If i were to label the x and y axes on these figures what would be the proper labels?

The above codes and corresponding attachments match correctly with their code. Note that code 4 results weren't stored as a attachment but the rest were.
 

Attachments

  • Capture.JPG
    Capture.JPG
    35.6 KB · Views: 516
  • Capture2.JPG
    Capture2.JPG
    37 KB · Views: 554
  • Capture3.JPG
    Capture3.JPG
    33 KB · Views: 531
Last edited by a moderator:
  • #22
There is no functional difference between the first two code examples. "map=zeros(size(x))" and "map=zeros(grid_size,grid_size)" do exactly the same thing in this context. There is absolutely no reason to expect any output differences there, you can flip a coin on which method you use to initialize the map to zero.

While my actual understanding of this topic in general is sufficient to understand the topic. I was completely lost in coming up with the code. If it wasn't for the example in my textbook I wouldn't have come up with one. I have no idea why the first two codes come up with the results that they do.
The thing you're trying to plot is the set of all starting points z_0, in the complex plane, for which the iterative process of z=z^2+c (starting with z=z0) remains bounded. The thing you're calculating in "map" is essentially an "indicator function" for the set. That is, a function that is zero if the point is not in the set and one if the point is in the set (or actually in this case two values that are not necessarily zero and one, but essentially the same thing).

So for the basic set you really only get a two color map with this type of thing (same for the mandelbrot set). People however often like to get something that looks a bit more interesting than just two colors. So instead of using just one value for "in the set" and one other value for "not in the set", they still use just one color for "in the set" but for "not in the set" they use a number proportional to (or in some other way related to) the number of iterations taken for the point to "escape" to some predefined radius. (this is some radius from which divergence is inevitable).

Yes I've ran your code in gnu-octave (a freeware MATLAB clone) and it produces exactly what I expected, a two level map containing only 0's and 80's. I have no idea how you are getting shades and colors there from a two level map.
Just out of curiosity. If i were to label the x and y axes on these figures what would be the proper labels?
Properly scaled they are the real and imaginary values of points in the complex plane, that is Re(z) and Im(z). As they are currently drawn they are approximately ( Re(z) + 1.5 )*500/3 and ( Im(z) + 1.5 )*500/3 respectively.
 
Last edited:
  • #23
http://imageshack.us/clip/my-videos/193/fpx.mp4/
Code:
iterations=80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.5,grid_size),linspace(-1.5,1.5,grid_size));
c=-.123+.745*i;
z=x+y.*i;
map=zeros(grid_size,grid_size);
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)>sqrt(5));
    map(a)=k;
end
figure(1)
image(map)
title('Douday''s rabbit fractal')
colormap(jet)
 
Last edited:
  • #24
First attachment : Here's what I get from your code.

Second attachment : And here's what I get if I replace "abs(z) > .." with "abs(z) < .." as I suggested.

So pretty much exactly as I predicted.
 

Attachments

  • wabbit2.jpg
    wabbit2.jpg
    35.1 KB · Views: 557
  • wabbit1.jpg
    wabbit1.jpg
    19.5 KB · Views: 551
Last edited:
  • #25
make sure you click on the full screen button
i can't post videos so i just posted a link to it on imageshack in the previous post
 
Last edited:
  • #26
Well this wouldn't be the first time my copy of MATLAB has been doing things other peoples didn't. Have you seen my topic about floating point numbers? >_> somebody runs the same exact code that I do on MATLAB that stresses the difference on how to find the exact answer versus a rounded answer when solving a function and he gets both. My copy of MATLAB only produces two estimated answers =_=.
 
  • #27
Yeah that's strange. Can you take a look at the numbers stored in "map". I would have expected it be only zero's and 80's

There may be some differences in the way gnu-Octave and Matlab handles the "image" command to actually render the map, but I can't see any way the it should produce a different "map" matrix.

So take a look at the data in the "map" matrix and see if it's all zero's and 80's or if are there other numbers in there as well.

BTW. Can someone else running Matlab chime in here and check GreenPrints code?
 
  • #28
I copied and pasted the 500x500 array that was stored in "map"

"beetxt.com/apz"[/URL]

I don't think that's all of it >_> but like the first couple hundred characters but that should give you an idea of what was stored in "map"

I can upload the actual file itself if you want or something
 
Last edited by a moderator:
  • #29
Yes that's definitely a multi-level map (and therefore consistent with the multi-color multi-tone image being displayed).

Could you try one more thing just for interest. Could you try replacing,
Code:
 a = find(abs(z)>sqrt(5));

with
Code:
 a = (abs(z)>sqrt(5));

It should give exactly the same results. Basically it changes the way that MATLAB stores the information in "a" from a linear array (which has advantages of sparsity) to a logical matrix of the same dimensions as the map.

The second method might use a little more memory but it is simpler and definitely more transparent, so it would be interesting if you could give that a try. Both of the above give identical results for me on gnu-Octave, (both of which are different to your results as previously stated).
 
Last edited:
  • #30
Code #1
Code:
iterations=80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.5,grid_size),linspace(-1.5,1.5,grid_size));
c=-.123+.745*i;
z=x+y.*i;
map=zeros(grid_size,grid_size);
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)>sqrt(5));
    map(a)=k;
end
figure(1)
image(map)
title('Douday''s rabbit fractal')
colormap(jet)
See attachment #2 in post #21

Code #2
Code:
iterations=80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.5,grid_size),linspace(-1.5,1.5,grid_size));
c=-.123+.745*i;
z=x+y.*i;
map=zeros(grid_size,grid_size);
for k=1:iterations
    z=z.^2+c;
    a = (abs(z)>sqrt(5));
    map(a)=k;
end
figure(1)
image(map)
title('Douday''s rabbit fractal')
colormap(jet)
See attachment #1 in this post

Interesting their identical to each other.
 

Attachments

  • Capture.JPG
    Capture.JPG
    36.9 KB · Views: 545
Last edited:
  • #31
GreenPrint said:
Interesting their identical to each other.

Yeah they should be identical, but just testing.


Anyway that means the difference must be in the previous lines of code. I've come up wit ha test that should indicate where the difference is occurring if you could take the time to test it for me GreenPrint.

Could you please just try entering the following into a MATLAB interactive session.

Code:
> y=[1:10]
y =

    1    2    3    4    5    6    7    8    9   10

> x=zeros(size(y))

x =   0   0   0   0   0   0   0   0   0   0


> b=(y>6)

b =  0   0   0   0   0   0   1   1   1   1


> x(b)=5

x =  0   0   0   0   0   0   5   5   5   5

> b=(y>4)

b =  0   0   0   0   1   1   1   1   1   1


> x(b)=8

x = 0   0   0   0   8   8   8   8   8   8

>

I'm really interested if that last statement causes all of the 5's to be overwritten by 8's as per my results above.

Thanks in advance. :)
 
  • #32
This is what I got when I ran each line in MATLAB

Code:
>> y=[1:10]

y =

     1     2     3     4     5     6     7     8     9    10

>> x=zeros(size(y))

x =

     0     0     0     0     0     0     0     0     0     0

>> b=(y>6)

b =

     0     0     0     0     0     0     1     1     1     1

>> x(b)=5

x =

     0     0     0     0     0     0     5     5     5     5

>> b=(y>4)

b =

     0     0     0     0     1     1     1     1     1     1

>> x(b)=8

x =

     0     0     0     0     8     8     8     8     8     8
hmm...
 
Last edited:
  • #33
What are we able to conclude?
 
  • #34
GreenPrint said:
What are we able to conclude?

- Mathematically it's easy to prove to for all |z|&gt;0.5+0.5 \sqrt{(1+4 |c|)}, (about 1.503), that |z^2 + c| &gt; |z|.

- sqrt(5) is obviously greater than 1.503, so it follows that if |z|>sqrt(5) at any iteration, "k", then it will be greater than sqrt(5) for every subsequent iteration.

- This means that the set of all points where |z|>sqrt(5) grows with every iteration, and hence your map overwrites all previously written "k" values with the current value of "k" at each iteration.

- This means that at the end of the 80 iterations the map can only contain unwritten entries (which are zeros by initialization) and 80's. So we have a map with only two levels, which is exactly what I get with your code.

- So the only conclusion is that it is mathematically impossible for you to get multilevel maps you're getting. But yet you get them. :cry:

I really need someone else running your version of MATLAB to test this.
 
Last edited:
  • #35
Mathematically impossible is always good ^_^
 
  • #36
interestingly enough, just a while ago, i was running through my file in which i saved my home work problems on and ran into this cell and ran it and got a monochrome image. I was like O.O weird. I ran it another time and the same thing occurred. After being in shock I did a clear, clc and ran the cell again an produced the image that I had been producing before, a multicolored image, the same exact one.

I just think it's all very weird.

Also, how would you recommend handing in a assignment in which you are learning how to use MATLAB? I would imagine the best thing to do would to be handing in a .m file were each problem is a different cell. I'm noticing that there is no really great publishing option sense they all result in errors if you have user input functions in your file that you are trying to publish.

The immediate difference that I'm running into between the different versions of MATLAB is how MATLAB's symbolic tool box used to be based off of Mapple and now it runs through MuPad. It would see that the older versions of MATLAB that used the Mapple interface were stronger at solving large polynomials and could return exact answers for really complicated expressions, some that wolfram alpha can't even solve while the newer versions of MATLAB seem not to be able to do such. My book was written back when MATLAB could solve such equations. The examples however that include making the Mandelbrot sets seem to work exactly as they should on the newest version of MATLAB and apparently were suppose to produce the same results in older versions.

I just didn't realize how drastically different the versions were. I actually understand it now lol ^_^ and agree with you that it shouldn't be producing a multicolored image, but for some unknown reason it does @_@
 
  • #37
function [x, fval, exitflag, output] = pso3(objfunc, nvars)
% e=xlsread('WellData1.1.xls','n score');
x=[1 2 3;4 5 6;1 2 6];
obj =x.^3-2*x.^2+5;
% msg = (NARGCHK(1, 3, nargin));
% if ~isempty(msg)
% error('mrr:myoptim:pso:pso:narginerr', 'Inadequate number of input arguments.');
% end
%
% msg = (NARGCHK(0, 4, nargout));
% if ~isempty(msg)
% error('mrr:myoptim:pso:pso:nargouterr', 'Inadequate number of output arguments.');
% end
if nargin==1 && ischar(objfunc) && strcmp(objfunc, 'options')
if nargout<=1
x = getDefaultOptions();
else
error('mrr:myoptim:pso:pso:nargouterr', ...
'Cannot expext more than one output when only OPTIONS are required.');
end
else
if nargin<3, options=getDefaultOptions(); end

if nargout == 4
if strcmp(options.output_level, 'none')
if options.plot == 0
output_level = 0;
else
output_level = 1;
end
elseif strcmp(options.output_level, 'low')
output_level = 1;
elseif strcmp(options.output_level, 'medium')
output_level = 2;
elseif strcmp(options.output_level, 'high')
output_level = 3;
else
error('mrr:myoptim:pso:pso:optionserr:output_level', ...
'Invalid value of the OUTPUT_LEVEL options specified.');
end
else
if options.plot == 1
output_level = 1;
else
output_level = 0;
end
end
if ~all(isnan(options.vmax))
if any(isnan(options.vmax))
error('mrr:myoptim:pso:pso:optionserr:vmax', ...
'VMAX option cannot have some Inf and some numerical (or Inf) values.');
end
if ~isnan(options.vmaxscale)
warning('mrr:myoptim:pso:pso:optionserr:vmaxconflict', ...
'Both relative and absolute velocity limit are specified. The relative limit is ignored.');
end
if length(options.vmax) == 1
vmax = options.vmax*ones(nvars, 1);
elseif length(options.vmax) == nvars

if size(options.vmax, 1) ~= length(options.vmax)
error('mrr:myopim:pso:pso:optionserr:vmax', ...
'VMAX option should be specified as column-vector, or as a scalar value.');
end
vmax = options.vmax;
else
error('mrr:myoptim:pso:pso:optionserr:vmax', ...
'Inadequate dimension of VMAX option. Should be a scalar, or a column vector with NVARS elements.');
end
else

if isnan(options.vmaxscale)
error('mrr:myoptim:pso:pso:optionserr:vmaxscale', ...
'Either VMAX or VMAXSCALE options should be different than NaN.');
end

if length(options.vmaxscale) == 1
if length(options.initspan) == 1
vmax = options.vmaxscale*options.initspan*ones(nvars, 1);
else

vmax = options.vmaxscale*options.initspan;
end
else
error('mrr:myoptim:pso:pso:optionserr:vmax', ...
'Inadequate dimension of VMAXSCALE option. Must be a scalar.');
end
end
vmax = repmat(vmax', Ns, 1);


if ~isnan(options.initpopulation)

[pno, pdim] = size(options.initpopulation);
if (pno ~= Ns) || (pdim ~= nvars)
error('mrr:myoptim:pso:pso:optionserr:initpopulation', ...
['The format of initial population is inconsistent with desired population', ...
'size or dimension of search space - INITPOPULATION options is invalid']);
end
X = options.initpopulation;
elseif (length(options.initoffset) == 1) && (length(options.initspan) == 1)

X = (rand(Ns, nvars)-0.5)*2*options.initspan + options.initoffset;
elseif (length(options.initoffset) ~= size(options.initoffset, 1)) || ...
(length(options.initspan) ~= size(options.initspan, 1))
error('mrr:myoptim:pso:pso:optionserr:initoffset_initspan', ...
'Both INITOFFSET and INITSPAN options must be either scalars or column-vectors.');
elseif (length(options.initoffset) ~= nvars) || (length(options.initspan) ~= nvars)
error('mrr:myoptim:pso:pso:optionserr:init', ...
'Both INITOFFSET and INITSPAN options must be scalars or column-vectors of length NVARS.');
else
initoffset = repmat(options.initoffset', Ns, 1);
initspan = repmat(options.initspan', Ns, 1);
X = (rand(Ns, nvars)-0.5)*2.*initspan + initoffset;

if (options.trustoffset)
X(1, :) = options.initoffset';
end
end


if any(isnan(options.vspaninit))
error('mrr:myoptim:pso:pso:optionserr:vspaninit', ...
'VSPANINIT option must not contain NaN entries.');
elseif isscalar(options.vspaninit)
V = (rand(Ns, nvars)-0.5)*2*options.vspaninit;
else
if (length(options.vspaninit) ~= size(options.vspaninit, 1)) || ...
(length(options.vspaninit) ~= nvars)
error('mrr:myoptim:pso:pso:optionserr:vspaninit', ...
'VSPANINIT option must be either scalar or column-vector of length NVARS');
end
V = (rand(Ns, nvars)-0.5)*2.*repmat(options.vspaninit', Ns, 1);
end

Y = calcobjfunc(objfunc, X);
Ybest = Y;
Xbest = X;

[GYbest, gbest] = min(Ybest);

gbest = gbest(1);




tolbreak = ~isnan(options.globalmin);
foundglobal = 0;
if tolbreak && ~isscalar(options.globalmin)
error('mrr:myoptim:pso:pso:optionserr:globalmin', ...
'globalmin option, if specified, option must be a scalar value equal to the global minimum of the objective function');
end


if output_level >= 0

output.itersno = K;
if output_level >= 1

output.gbest_array = NaN*ones(K+1, 1);
output.gmean_array = NaN*ones(K+1, 1);
output.gworst_array = NaN*ones(K+1, 1);
output.gbest_array(1) = GYbest;
output.gmean_array(1) = mean(Ybest);
output.gworst_array(1) = max(Ybest);
if output_level >= 2

output.gbestndx_array = NaN*ones(K+1, 1);
output.Xbest = NaN*ones(K+1, nvars);
output.gbestndx_array(1) = gbest;
output.Xbest(1, :) = X(gbest, :);
if output_level == 3
output.X = NaN*zeros(Ns, nvars, K+1);
output.X(:,:,1) = X;
end
end
end
end

if options.verbose_period ~= 0
disp 'PSO algorithm: Initiating the optimization process.'
end


exitflag = 0;


for iter = 1:K


if options.verbose_period ~= 0
if rem(iter, options.verbose_period) == 0
disp(['iteration ', int2str(iter), '. best criteria = ', num2str(GYbest)]);
end
end


w = linrate(options.wf, options.wi, K, 0, iter);
cp = linrate(options.cbf, options.cbi, K, 0, iter);
cg = linrate(options.cgf, options.cgi, K, 0, iter);


GXbest = repmat(Xbest(gbest, :), Ns, 1);


V = w*V + cp*rand(size(V)).*(Xbest-X) + cg*rand(size(V)).*(GXbest-X);
V = min(vmax, abs(V)).*sign(V);


X = X + V;
Y = calcobjfunc(objfunc, X);

mask = Y<Ybest;
mask = repmat(mask, 1, nvars);
Xbest = mask.*X +(~mask).*Xbest;
Ybest = min(Y,Ybest);


[GYbest, gbest] = min(Ybest);
gbest = gbest(1);


if output_level >= 0

if output_level >= 1

output.gbest_array(iter+1) = GYbest;
output.gmean_array(iter+1) = mean(Ybest);
output.gworst_array(iter+1) = max(Ybest);
if output_level >= 2

output.gbestndx_array(iter+1) = gbest;
output.Xbest(iter+1, :) = X(gbest, :);
if output_level == 3

output.X(:,:,iter+1) = X;
end
end
end
end


if tolbreak && abs(GYbest - options.globalmin)<options.tol
output.itersno = iter;
foundglobal = 1;
break
end

end

if options.verbose_period ~= 0
disp 'Optimization process finished.'
end


x = Xbest(gbest, :); x = x(:);
fval = GYbest;


if foundglobal, exitflag = 1; end;


if options.plot
r = 0:K;
figure
plot(r, output.gbest_array, 'k.', r, output.gmean_array, 'r.', r, output.gworst_array, 'b.');
str = sprintf('Best objective value : %g', fval);
title(str);
legend({'best objective', 'mean objective', 'worst objective'})
end

end
function [func]= calcobjfunc(func, X)
np = size(X,1);
Y = zeros(np,1);
for i = 1:np
Y(i) = func(X(i,:));
end
function opts = getDefaultOptions
%This function, in fact, defines default values of the options within the options structure.
opts.npart = 40; % The number of particles.
opts.niter = 20; % The number of iterations.
opts.cbi = 2.5; % Initial value of the individual-best acceleration factor.
opts.cbf = 0.5; % Final value of the individual-best acceleration factor.
opts.cgi = 0.5; % Initial value of the global-best acceleration factor.
opts.cgf = 2.5; % Final value of the global-best acceleration factor.
opts.wi = 0.9; % Initial value of the inertia factor.
opts.wf = 0.4; % Final value of the inertia factor.
opts.vmax = Inf; % Absolute speed limit. It is the primary speed limit.
opts.vmaxscale = NaN; % Relative speed limit. Used only if absolute limit is unspecified.
opts.vspaninit = 1; % The initial velocity span. Initial velocities are initialized
% uniformly in [-VSPANINIT, VSPANINIT].
opts.initoffset = 0; % Offset of the initial population.
opts.initspan = 1; % Span of the initial population.
opts.trustoffset = 0; % If set to 1 (true) and offset is vector, than the offset is
% believed to be a good solution candidate, so it is included in
% the initial swarm.
opts.initpopulation = NaN; % The user-suplied initial population. If this is set to something
% meaningfull, then INITSPAN, INITOFFSET and TRUSTOFFSET are
% ignored.
opts.verbose_period = 10; % The verbose period, i.e. the number of iterations after which the
% results are prompted to the user. If set to 0, then verbosing is
% skipped.
opts.plot = 1; % If set to 1, evolution of the gbest is ploted to the user after
% the optimization process. The objective value of the best, mean
% and worse particle troughout the optimization process are plotted
% in the single graph.
opts.output_level = 'low'; % The output log level. Possible values are: 'none', 'low',
% 'medium', 'high'.
opts.globalmin = NaN; % Global minimum, used for testing only
opts.tol = 1e-6; % Precision tolerance, used for testing only
 
  • #38
this is PSO code ,
it has error in line 298
is there anyone to help me
 
  • #39
i mean here ,
function [func]= calcobjfunc(func, X)
np = size(X,1);
Y = zeros(np,1);
for i = 1:np
Y(i) = func(X(i,:));
end
 
  • #40
function Y= calcobjfunc(func, X)
np = size(X,1);
Y = zeros(np,1);
for i = 1:np
Y(i) = func(X(i,:));
end
 
  • #41
no clue dude
 
  • #42
we can't let the mathematically impossible results go
 
  • #43
can someone with MATLAB please run my code
 
  • #44
GreenPrint said:
we can't let the mathematically impossible results go

Hi Greenprint. I'm glad you have the tenacity to stick with trying to get to the bottom of this one. :)

What about single stepping through it, that's usually a really good way to expose a problem.

Firstly let's make an m-file containing a macro with the following code (I'll call mine wabbit.m)
Code:
k=k+1
z=z.^2+c;
a=(abs(z)>sqrt(5));
map(a)=k;

Now start a new interactive session (or clear your existing one) and enter the following commands.
Code:
clear
gs = 24
[x,y] = meshgrid(linspace(-1.5,1.5,gs),linspace(-1.5,1.5,gs));
z = x+y*i;
c = -.123+.745*i;
map=zeros(gs,gs);
k = 0

Here we've initialized everything that we need. Notice that I've only made the grid 24x24, so we'll have very coarse resolution, but we'll be able to easily display the matrix contents on the screen at each iteration. So if anything plays up, we'll catch it.

Now run the maco, type "wabbit". It should just display k=1, showing that you've done one iteration. We can now view the contents of "a" and "map" to see if anything unexpected is happening.

Type "a" (without quotes) at the prompt and you'll see the contents of the "a" matrix. Do the same for "map" and it should look exactly the same at this point.

Now run the macro a second time, "wabbit". Now look at "a" and "map" once more the same way as before. This timr "a" should be similar to before, but have slightly more ones and less zeros. However "map" now should contain only zeros and twos, with the twos in exactly the same place as where the ones are in "a". Check that this is what you get.

Now you can repeat this as many times as you wish. Each time you type "wabbit" the macro will do one more iteration, k will increase by one, "a" will have a few more ones (but always contain only zeros and ones) and "map" will have a few more non zero terms, (but always only contain zeros and whatever numerical value "k" currently is).

For example after five iterations map should look something like this :
Code:
 5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5
 5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5
 5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5
 5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5
 5   5   5   5   5   5   5   5   5   5   5   5   0   5   5   5   5   5   5   5   5   5   5   5
 5   5   5   5   5   5   5   5   5   5   5   0   0   0   0   5   0   0   0   0   5   5   5   5
 5   5   5   5   5   5   5   5   5   5   5   0   0   0   0   0   0   0   0   0   0   5   5   5
 5   5   5   5   5   5   5   5   5   5   5   0   0   0   0   0   0   0   0   0   0   0   5   5
 5   5   5   5   5   5   5   5   5   5   0   0   0   0   0   0   0   0   0   0   0   5   5   5
 5   5   5   5   5   5   5   5   0   0   0   0   0   0   0   0   0   0   0   5   5   5   5   5
 5   5   5   5   5   5   5   5   0   0   0   0   0   0   0   0   0   0   5   5   5   5   5   5
 5   5   5   5   5   5   5   5   0   0   0   0   0   0   0   0   5   5   5   5   5   5   5   5
 5   5   5   5   5   5   5   5   0   0   0   0   0   0   0   0   5   5   5   5   5   5   5   5
 5   5   5   5   5   5   0   0   0   0   0   0   0   0   0   0   5   5   5   5   5   5   5   5
 5   5   5   5   5   0   0   0   0   0   0   0   0   0   0   0   5   5   5   5   5   5   5   5
 5   5   5   0   0   0   0   0   0   0   0   0   0   0   5   5   5   5   5   5   5   5   5   5
 5   5   0   0   0   0   0   0   0   0   0   0   0   5   5   5   5   5   5   5   5   5   5   5
 5   5   5   0   0   0   0   0   0   0   0   0   0   5   5   5   5   5   5   5   5   5   5   5
 5   5   5   5   0   0   0   0   5   0   0   0   0   5   5   5   5   5   5   5   5   5   5   5
 5   5   5   5   5   5   5   5   5   5   5   0   5   5   5   5   5   5   5   5   5   5   5   5
 5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5
 5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5
 5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5
 5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5

Give it a try and let me know what you get.
 
Last edited:
  • #45
Code:
>> clear
gs = 24
[x,y] = meshgrid(linspace(-1.5,1.5,gs),linspace(-1.5,1.5,gs));
z = x+y*i;
c = -.123+.745*i;
map=zeros(gs,gs);
k = 0

gs =

    24


k =

     0

>> wabbit

k =

     1

>> a

a =

     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     1     1     1     1
     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1
     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1
     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1
     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1
     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1
     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1
     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1
     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1
     1     1     1     1     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

>> map

map =

     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     1     1     1     1
     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1
     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1
     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1
     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1
     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1
     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1
     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1
     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1
     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1
     1     1     1     1     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

>> wabbit

k =

     2

>> a

a =

     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1
     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1
     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1
     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1
     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1
     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1
     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1
     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1
     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1
     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1
     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1
     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1
     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

>> map

map =

     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2     2     2     2     0     0     0     0     0     0     0     2     2     2     2     2     2
     2     2     2     2     2     2     2     2     2     0     0     0     0     0     0     0     0     0     0     0     0     2     2     2
     2     2     2     2     2     2     2     2     2     0     0     0     0     0     0     0     0     0     0     0     0     0     2     2
     2     2     2     2     2     2     2     2     0     0     0     0     0     0     0     0     0     0     0     0     0     0     2     2
     2     2     2     2     2     2     2     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     2
     2     2     2     2     2     2     2     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     2
     2     2     2     2     2     2     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     2     2
     2     2     2     2     2     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     2     2     2
     2     2     2     2     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     2     2     2
     2     2     2     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     2     2     2     2
     2     2     2     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     2     2     2     2     2
     2     2     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     2     2     2     2     2     2
     2     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     2     2     2     2     2     2     2
     2     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     2     2     2     2     2     2     2
     2     2     0     0     0     0     0     0     0     0     0     0     0     0     0     0     2     2     2     2     2     2     2     2
     2     2     0     0     0     0     0     0     0     0     0     0     0     0     0     2     2     2     2     2     2     2     2     2
     2     2     2     0     0     0     0     0     0     0     0     0     0     0     0     2     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     0     0     0     0     0     0     0     2     2     2     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2
     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2     2
 
  • #46
Code:
>> wabbit

k =

     3

>> a

a =

     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     0     0     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     1     1     0     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     1     1     1
     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1
     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1
     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1
     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1
     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1
     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1
     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1
     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1
     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1
     1     1     1     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     0     1     1     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     0     0     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

>> map

map =

     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3
     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3
     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3
     3     3     3     3     3     3     3     3     3     3     3     3     0     0     3     3     3     3     3     3     3     3     3     3
     3     3     3     3     3     3     3     3     3     3     3     0     0     0     0     0     3     3     0     3     3     3     3     3
     3     3     3     3     3     3     3     3     3     3     0     0     0     0     0     0     0     0     0     0     0     3     3     3
     3     3     3     3     3     3     3     3     3     3     0     0     0     0     0     0     0     0     0     0     0     0     3     3
     3     3     3     3     3     3     3     3     3     0     0     0     0     0     0     0     0     0     0     0     0     0     3     3
     3     3     3     3     3     3     3     3     0     0     0     0     0     0     0     0     0     0     0     0     0     0     3     3
     3     3     3     3     3     3     3     0     0     0     0     0     0     0     0     0     0     0     0     0     0     3     3     3
     3     3     3     3     3     3     3     0     0     0     0     0     0     0     0     0     0     0     0     3     3     3     3     3
     3     3     3     3     3     3     0     0     0     0     0     0     0     0     0     0     0     0     3     3     3     3     3     3
     3     3     3     3     3     3     0     0     0     0     0     0     0     0     0     0     0     0     3     3     3     3     3     3
     3     3     3     3     3     0     0     0     0     0     0     0     0     0     0     0     0     3     3     3     3     3     3     3
     3     3     3     0     0     0     0     0     0     0     0     0     0     0     0     0     0     3     3     3     3     3     3     3
     3     3     0     0     0     0     0     0     0     0     0     0     0     0     0     0     3     3     3     3     3     3     3     3
     3     3     0     0     0     0     0     0     0     0     0     0     0     0     0     3     3     3     3     3     3     3     3     3
     3     3     0     0     0     0     0     0     0     0     0     0     0     0     3     3     3     3     3     3     3     3     3     3
     3     3     3     0     0     0     0     0     0     0     0     0     0     0     3     3     3     3     3     3     3     3     3     3
     3     3     3     3     3     0     3     3     0     0     0     0     0     3     3     3     3     3     3     3     3     3     3     3
     3     3     3     3     3     3     3     3     3     3     0     0     3     3     3     3     3     3     3     3     3     3     3     3
     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3
     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3
     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3     3

>> wabbit

k =

     4

>> a

a =

     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     0     0     0     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     1     1     1
     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1
     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1
     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1
     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1
     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1
     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1
     1     1     0     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1
     1     1     0     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1
     1     1     1     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     0     0     0     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

>> map

map =

     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4
     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4
     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4
     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4
     4     4     4     4     4     4     4     4     4     4     4     0     0     0     4     4     4     4     4     4     4     4     4     4
     4     4     4     4     4     4     4     4     4     4     4     0     0     0     0     0     0     0     0     0     4     4     4     4
     4     4     4     4     4     4     4     4     4     4     4     0     0     0     0     0     0     0     0     0     0     4     4     4
     4     4     4     4     4     4     4     4     4     4     0     0     0     0     0     0     0     0     0     0     0     0     4     4
     4     4     4     4     4     4     4     4     4     0     0     0     0     0     0     0     0     0     0     0     0     0     4     4
     4     4     4     4     4     4     4     4     0     0     0     0     0     0     0     0     0     0     0     4     4     4     4     4
     4     4     4     4     4     4     4     4     0     0     0     0     0     0     0     0     0     0     4     4     4     4     4     4
     4     4     4     4     4     4     4     0     0     0     0     0     0     0     0     0     0     4     4     4     4     4     4     4
     4     4     4     4     4     4     4     0     0     0     0     0     0     0     0     0     0     4     4     4     4     4     4     4
     4     4     4     4     4     4     0     0     0     0     0     0     0     0     0     0     4     4     4     4     4     4     4     4
     4     4     4     4     4     0     0     0     0     0     0     0     0     0     0     0     4     4     4     4     4     4     4     4
     4     4     0     0     0     0     0     0     0     0     0     0     0     0     0     4     4     4     4     4     4     4     4     4
     4     4     0     0     0     0     0     0     0     0     0     0     0     0     4     4     4     4     4     4     4     4     4     4
     4     4     4     0     0     0     0     0     0     0     0     0     0     4     4     4     4     4     4     4     4     4     4     4
     4     4     4     4     0     0     0     0     0     0     0     0     0     4     4     4     4     4     4     4     4     4     4     4
     4     4     4     4     4     4     4     4     4     4     0     0     0     4     4     4     4     4     4     4     4     4     4     4
     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4
     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4
     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4
     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4     4
 
  • #47
Code:
>> wabbit

k =

     5

>> a

a =

     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     0     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0     1     0     0     0     0     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     1     1
     1     1     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     1     1     1
     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1
     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1
     1     1     1     1     1     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1
     1     1     1     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1
     1     1     0     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     0     0     0     0     0     0     0     0     0     0     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     0     0     0     0     1     0     0     0     0     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     0     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1     1

>> map

map =

     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5
     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5
     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5
     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5
     5     5     5     5     5     5     5     5     5     5     5     5     0     5     5     5     5     5     5     5     5     5     5     5
     5     5     5     5     5     5     5     5     5     5     5     0     0     0     0     5     0     0     0     0     5     5     5     5
     5     5     5     5     5     5     5     5     5     5     5     0     0     0     0     0     0     0     0     0     0     5     5     5
     5     5     5     5     5     5     5     5     5     5     5     0     0     0     0     0     0     0     0     0     0     0     5     5
     5     5     5     5     5     5     5     5     5     5     0     0     0     0     0     0     0     0     0     0     0     5     5     5
     5     5     5     5     5     5     5     5     0     0     0     0     0     0     0     0     0     0     0     5     5     5     5     5
     5     5     5     5     5     5     5     5     0     0     0     0     0     0     0     0     0     0     5     5     5     5     5     5
     5     5     5     5     5     5     5     5     0     0     0     0     0     0     0     0     5     5     5     5     5     5     5     5
     5     5     5     5     5     5     5     5     0     0     0     0     0     0     0     0     5     5     5     5     5     5     5     5
     5     5     5     5     5     5     0     0     0     0     0     0     0     0     0     0     5     5     5     5     5     5     5     5
     5     5     5     5     5     0     0     0     0     0     0     0     0     0     0     0     5     5     5     5     5     5     5     5
     5     5     5     0     0     0     0     0     0     0     0     0     0     0     5     5     5     5     5     5     5     5     5     5
     5     5     0     0     0     0     0     0     0     0     0     0     0     5     5     5     5     5     5     5     5     5     5     5
     5     5     5     0     0     0     0     0     0     0     0     0     0     5     5     5     5     5     5     5     5     5     5     5
     5     5     5     5     0     0     0     0     5     0     0     0     0     5     5     5     5     5     5     5     5     5     5     5
     5     5     5     5     5     5     5     5     5     5     5     0     5     5     5     5     5     5     5     5     5     5     5     5
     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5
     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5
     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5
     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5     5

>>

It would appear as if we get the same exact results for the 5th iteration, yet we don't get the same plots when we run that one code. I posted the first 5.
 
Last edited:
  • #48
Yep everything is getting calculated exactly as I expected (and exactly the same as gnu-octave is producing here for me). You can plot those maps if you like with the command "image(map)" and of course they will be monochrome (two tone) as the maps have only two levels.

So everything works as expected when you run it step by step and look at the results before you run "image(map)". Sorry but I can't figure this out, it just has to be something that you're doing wrong/different but I don't know what.

The other really confounding thing is that the Mandlebrot example in your book apparently does the same thing and yet the image given for that one also shows a multilevel map. I would normally have put this down to a typo in the book (having a ">" where it should have been "<") except that when you copied their code you also got multilevel maps.
 
Last edited:
  • #49
BTW. One other test that you should try is to edit that little test macro (wabbit.m) so as to change the sign of the inequality as I previously suggested (from ">" to "<"). That is use the following code for the macro :
Code:
k=k+1
z=z.^2+c;
a=(abs(z) < sqrt(5));
map(a)=k;

Clear and re-initialize everything and go through the same steps as above, looking at map after each step. This time you'll see different numbers appearing in the map matrix and when you type "image(map)" you really will get a multi-tone image. Naturally the image will be very low resolution, given the small gridsize, but you'll definitely recognize a vague image of the rabbit-face developing there.

I recommend you doing this test so that you at least understand how it should work, and why I originally suggested making that change.
 
  • #50
With the following code for the marco:

Code:
k=k+1
z=z.^2+c;
a=(abs(z)>sqrt(5));
map(a)=k;
I do produce the following monochrome image after 5 iterations in attachment #1

Running the following code for the marco:

Code:
k=k+1
z=z.^2+c;
a=(abs(z) < sqrt(5));
map(a)=k;
does produce the following multi-tone image in attachment #2

running the following code still however produce the multi-tone image in attachment #3
Code:
>> iterations=80;
grid_size=500;
[x,y]=meshgrid(linspace(-1.5,1.5,grid_size),linspace(-1.5,1.5,grid_size));
c=-.123+.745*i;
z=x+y*i;
map=zeros(size(x));
for k=1:iterations
    z=z.^2+c;
    a=find(abs(z)>sqrt(5));
    map(a)=k;
end
figure(1)
image(map)
colormap(jet)

Wired my book also does an example of how to create a "movie" in which you can zoom in on a Mandelbrot set in which it uses

a= find(abs(z)>sqrt(5));

>_>
 

Attachments

  • capture2.JPG
    capture2.JPG
    20.3 KB · Views: 472
  • Capture.JPG
    Capture.JPG
    20.4 KB · Views: 491
  • Capture3.JPG
    Capture3.JPG
    36.1 KB · Views: 434
Back
Top