I need some help with plotting a scatter plot in a for loop

I am given z = (sin(xy))^2, x and y. z is used for to represent color coding. I need to create a 2-D x-y scatter plot of z = (sin(xy))^2. I am required to plot each point using the for loop. I can get it to plot x(i), y(i) no problem. That creates a linear relationship.
The graph I am trying to make does x(1),y(1) x(1),y(2) up to x(1),y(end) and the same idea for y: x(1),y(1) x(2),y(1) up to x(end),y(1). That way it populates the entire plane and creates a color pattern. I'm not sure how to do that.
if true
x = -3:0.1:3;
y = -3:0.1:3;
z = (sin(x.*y)).^2;
hold on
for i = 1:length(x)
if z(i) < 0.1
plot(x(z),y(z),'sb')
elseif z(i) > 0.1 && z(i) < 0.5
plot(x(i),y(i),'sc')
elseif z(i) > 0.5 && z(i) < 0.75
plot(x(i),y(i),'sg')
elseif z(i) > 0.75 && z(i) < 0.95
plot(x(i),y(i),'sr')
else
plot(x(i),y(i),'sk')
end
xlim([-3 3])
ylim([-3 3])
end
hold off
end

1 Commento

Replace
plot(x(z),y(z),'sb')
by
plot(x(i),y(i),'sb')
to make the script run. However, the result is not what you want.

Accedi per commentare.

 Risposta accettata

per isakson
per isakson il 17 Feb 2016
Modificato: per isakson il 17 Feb 2016
The function kevin, which I think implements "The graph I am trying to make does x(1),y(1) x(1),y(2) up to x(1),y(end) and the same idea for y: x(1),y(1) x(2),y(1) up to x(end),y(1).", creates
&nbsp
where
function kevin
x = -3:0.1:3;
y = -3:0.1:3;
z = (sin(x.*y)).^2;
hold on
for jj = 1:length(x)
if z(jj) < 0.1
plot(x(jj),y,'sb')
elseif z(jj) > 0.1 && z(jj) < 0.5
plot(x(jj),y,'sc')
elseif z(jj) > 0.5 && z(jj) < 0.75
plot(x(jj),y,'sg')
elseif z(jj) > 0.75 && z(jj) < 0.95
plot(x(jj),y,'sr')
else
plot(x(jj),y,'sk')
end
xlim([-3 3])
ylim([-3 3])
end
hold off
end
Functions are easier than scripts to debug and use. And they don't clutter the base workspace.

5 Commenti

This is what my plot should look like. I apologize for not providing this earlier.
per isakson
per isakson il 17 Feb 2016
Modificato: per isakson il 17 Feb 2016
I guess this is your homework. Did you produce this plot?
There is a function named scatter, Scatter plot
Yes, this is my homework; no, I did not produce the plot above. That is what was given to us as what our plot needs to look like. However, we can only use the plot function. The plot that gets produced in my code below is:
if true
x = -3:0.1:3;
y = -3:0.1:3;
z = (sin(x.*y)).^2;
hold on
for i = 1:length(x)
if z(i) < 0.1
plot(x(i),y(i),'sb')
elseif z(i) > 0.1 && z(i) < 0.5
plot(x(i),y(i),'sc')
elseif z(i) > 0.5 && z(i) < 0.75
plot(x(i),y(i),'sg')
elseif z(i) > 0.75 && z(i) < 0.95
plot(x(i),y(i),'sr')
else
plot(x(i),y(i),'sk')
end
xlim([-3 3])
ylim([-3 3])
end
hold off
end
I'm not really looking for a code to get the desired results. I just want to know how I can utilize for loop(s) and the plot function to plot x(1),y(1) x(1),y(2) all the way to the end of y, and also x(1),y(1) x(2),y(1) all the way to the end of x, so it creates:
per isakson
per isakson il 18 Feb 2016
Modificato: per isakson il 18 Feb 2016
  • I think that the points you plot are correct; your diagonal is identical to the diagonal of the full plot.
  • the problem is that you only plot the points ((x(i),y(i)) for all values of i, i.e all points with the same value of x and y. (The vectors, x and y, are identical.)
  • you want to plot all combinations of values of x and y, i.e you need to loop over all values of y and for each value of y you need to loop over all values of x (or the other way round)
  • to save time while experimenting, replace -3:0.1:3; by -3:0.5:3;
Experimenting
  • Download and install ALWAYSONTOP, by Elmar Tarajan
  • To see what is going on, put the function drawnow inside the loop.
  • Convert your script to a function. (See my answer.)
  • Put a break-point inside the loop and call the function
>> alwaysontop auto
>> figure % and click the alwaysontop button
>> kevin % call the function
7 z = (sin(x(ii)*y(jj)))^2;
and step through the code and try to understand what is going on.

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by