Plot checker board pattern where color of square is based on value of function.

This is part of a larger process so I have rewritten the problem so that it can stand on its own. If something is unclear or looks off I would be happy to clarify or take a look at it again.
So what I am doing is taking a 100 points along the x and y axis and evaluating a function, f(x,y), on each point created by that grid. So if (1,1) gives a value that I associate with blue then I want the square with the corners (0,0),(0,1),(1,0) and (1,1) to be colored blue.
for x=1:X
for y=1:Y
if [Conditions on the value of f(x,y)]
color='blue'
elseif ...
...
else
...
end
% Here is what I tried for the plotting the figure. The figure opens and runs, but no colors are plotted so I something is off.
figure(1)
hold on
if x==1 && y~=1
fill(0,Y(y-1),X(x),Y(y-1),0,Y(y),X(x),Y(y),color)
elseif x~=1 && y==1
fill(X(x-1),0,X(x),0,X(x-1),Y(y),X(x),Y(y),color)
elseif i==1 && j==1
fill(0,0,X(x),0,0,Y(y),X(x),Y(y),color)
else
fill(X(x-1),Y(y-1),X(x),Y(y-1),X(x-1),Y(y),X(x),Y(y),color)
end

Answers (1)

I don't understand. You have
for x=1:X
and then in the fills you have X(x). Exactly what is capital X? If it's a 1D array of 100 values, then what are you doing in the "for" line? Can you give the values of X and Y so we can run your code? It's not quite standalone right now because it does run as given. I want to see the range of values that X takes on.
By the way, there is a checkerboard() function in the Image Processing Toolbox.

6 Comments

Sorry about that. X is an array, X=1:100. So the for statement should say x=1:length(X).
Also the code runs as I said but the figure is completely white even though the value of the variable color is changing each iteration. So I'm fairly certain the problem is with the fill command.
Sorry - still cant' run it. Can you make up some function so we can run it?
Sorry about that. Here is the exact code that I am using for this bit. It is evaluating a system and coloring points based on their long term behavior.
clear all;
% Parametric Values
a1=0.2;
k1=0.05;
k2=0.05;
% Initial Conditions for the Population
J(1)=0;
A(1)=4;
N(1)=4;
% Determines how many iterations the model will run for
T=1500;
% Vector specifying the birth rates to be evaluated over
% We are choosing 100 points for each parameter and thus creating a 100x100 grid to evaluate the long term behavior of our system over.
b=linspace(0,300,100);
a2 = linspace(0,1,100);
for j=1:length(b)
for i=1:length(a2)
% Run the code for T iterations.
for t=1:T
J(t+1)=a1*exp(-k1*J(t))*J(t)+b(j)*A(t);
A(t+1)=a1*exp(-k1*J(t))*J(t)+a2(i)*exp(-k2*A(t))*A(t);
N(t+1)=J(t+1)+A(t+1);
end
% Discard the first 1000 iterations since we are interested in long term behavior. Assign color based on whether the points solution goes to 0, is an equilibria, or a periodic point.
if (N(1001)==0)
color = 'white'
elseif (N(1001)==N(1002))
color = 'yellow'
elseif N(1001)==N(1003) && N(1002)==N(1004)
color = 'red'
elseif N(1001)==N(1004) && N(1002)==N(1005) && N(1003)==N(1006)
color = 'blue'
elseif N(1001)==N(1005) && N(1002)==N(1006) && N(1003)==N(1007) && N(1004)==N(1008)
color = 'magenta'
elseif N(1001)==N(1006) && N(1002)==N(1007) && N(1003)==N(1008) && N(1004)==N(1009) && N(1005)==N(1010)
color = 'green'
elseif N(1001)==N(1007) && N(1002)==N(1008) && N(1003)==N(1009) && N(1004)==N(1010) && N(1005)==N(1011) && N(1006)==N(1012)
color = 'cyan'
%elseif N(1001)==N(1008) && N(1002)==N(1009) && N(1003)==N(1010) && N(1004)==N(1011) && N(1005)==N(1012) && N(1006)==N(1013) && N(1007)==N(1014)
% color = 'cyan'
%elseif N(1001)==N(1009) && N(1002)==N(1010) && N(1003)==N(1011) && N(1004)==N(1012) && N(1005)==N(1013) && N(1006)==N(1014) && N(1007)==N(1015)
% color = 'yellow'
else
color = 'black'
end
figure(1)
hold on
if i==1 && j~=1
fill(0,a2(j-1),b(i),a2(j-1),0,a2(j),b(i),a2(j),color)
elseif i~=1 && j==1
fill(b(i-1),0,b(i),0,b(i-1),a2(j),b(i),a2(j),color)
elseif i==1 && j==1
fill(0,0,b(i),0,0,a2(j),b(i),a2(j),color)
else
fill(b(i-1),a2(j-1),b(i),a2(j-1),b(i-1),a2(j),b(i),a2(j),color)
end
end
end
Can you add some comments to explain what's going on. What are J, A, and N? Can't you use more descriptive names? These seem to be 1 x 1501 1D arrays. Why are you checking the elements between 1000 and 1015 to determine a color?
This code is for an ecological model. J is the juvenile population, A is the adult population and N is the total population. I am discarding the first 1000 iterations because I want the system to settle into its long term behavior. My goal is to assign different colors for points that go extinction or 0, fall into an equilibrium ie N(t)=N(t+1), and periodic solutions. That is what the first if-elseif statements are for. It all works fine because the value of the variable color changes for successive iterations but it doesn't plot the colors. So for some reason the fill statements are faulty. I added a couple comments in the code to help make it easier to follow.
I don't have time to dive into the code and understand now. Maybe you can just use an image instead of the fill() function.

Sign in to comment.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Asked:

on 23 Mar 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!