Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
the bug of the plot command

Subject: the bug of the plot command

From: Shoukui Si

Date: 17 Apr, 2011 14:48:05

Message: 1 of 3

When drawing the Julia set, we can use the real number iteration as following:
clc, clear
a=-0.11; b=0.65; r=2; N=60; hold on
for x0=-r:0.02:r
    for y0=-r:0.02:r
        x(1)=x0; y(1)=y0;
        if x0^2+y0^2<=r^2
            for n=1:N
                x(n+1)=x(n)^2-y(n)^2+a;
                y(n+1)=2*x(n)*y(n)+b;
            end
            if x(end)^2+y(end)^2<r^2
                plot(x0,y0,'.')
            end
        end
    end
end

We can use the complex number iteration as follows:
clc, clear
c=-0.11+0.65*i; r=2; N=60; hold on
for x0=-r:0.02:r
    for y0=-r:0.02:r
        z0=x0+y0*i; z=z0;
        if abs(z0)<r
            for n=1:N
                z=z^2+c;
            end
            if abs(z)<r
                plot(z0,'.')
            end
        end
    end
end

But the graph of the above program is wrong. We must correct the program to get the right results as follows:
clc, clear
c=-0.11+0.65*i; r=2; N=60; hold on
for x0=-r:0.02:r
    for y0=-r:0.02:r
        z0=x0+y0*i; z=z0;
        if abs(z0)<r
            for n=1:N
                z=z^2+c;
            end
            if abs(z)<r
                if isreal(z0)
                    plot(z0,0,'.') %if z0 is real number, plot has wrong.
                else
                    plot(z0,'.') %if z0 is complex number, plot is right.
                end
            end
        end
    end
end

Subject: the bug of the plot command

From: Roger Stafford

Date: 18 Apr, 2011 01:36:04

Message: 2 of 3

"Shoukui Si" <sishoukui@163.com> wrote in message <ioeuj5$rf4$1@fred.mathworks.com>...
> .......
> if isreal(z0)
> plot(z0,0,'.') %if z0 is real number, plot has wrong.
> else
> plot(z0,'.') %if z0 is complex number, plot is right.
> end
> .......
- - - - - - - - -
  Matlab documentation states that this is the way 'plot' is designed to work, and therefore it shouldn't be called a "bug". You might consider it an inconvenience for your particular purpose, but there is an easier way out than you have used. Just write:

 plot(real(z0),imag(z0),'.')

to avoid the if..else..end part. It should work in every case.

Roger Stafford

Subject: the bug of the plot command

From: Steven_Lord

Date: 18 Apr, 2011 15:06:44

Message: 3 of 3



"Shoukui Si" <sishoukui@163.com> wrote in message
news:ioeuj5$rf4$1@fred.mathworks.com...

*snip*

> But the graph of the above program is wrong. We must correct the program
> to get the right results as follows:
> clc, clear
> c=-0.11+0.65*i; r=2; N=60; hold on
> for x0=-r:0.02:r
> for y0=-r:0.02:r
> z0=x0+y0*i; z=z0;
> if abs(z0)<r
> for n=1:N
> z=z^2+c;
> end
> if abs(z)<r
> if isreal(z0)
> plot(z0,0,'.') %if z0 is real number, plot has wrong.
> else
> plot(z0,'.') %if z0 is complex number, plot is
> right.

This is NOT a bug but is documented and expected behavior.

http://www.mathworks.com/help/techdoc/ref/plot.html?BB=1

"plot(Y) plots the columns of Y versus the index of each value when Y is a
real number. For complex Y, plot(Y) is equivalent to plot(real(Y),imag(Y))."

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us