help with nested loop and plot (2024)

78 views (last 30 days)

Show older comments

LUCA D'AMBROSIO on 2 Jul 2024 at 9:41

  • Link

    Direct link to this question

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot

  • Link

    Direct link to this question

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot

Commented: LUCA D'AMBROSIO on 6 Jul 2024 at 14:14

Accepted Answer: Alan Stevens

hello everyone, i need help with the following code.

i am trying to plot the boundary of the region that satisfies the following conditions:

solve and plot for x and y

x+y+e+t>=0

And

x*y-e*t>=0

where x and y are the two variables while e and t are two constants whose values has to vary in a range.

so far i have got (which is working fine for fixed e and t):

n= 101;

x = linspace(-100, 100, n);

y = linspace(-100, 100, n);

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

Z = zeros(n, n);

e= 10;

t=-25;

B = X + Y + e + t;

D = X.*Y - e.*t;

for i= 1:n

for j= 1:n

if B(i,j) >= 0

Z(i,j) = D(i,j);

else

Z(i,j) = -1;

end

end

end

v = [0,0];

contour(X,Y,Z,v, 'LineWidth', 1.5)

grid on

axis equal

xline(0, 'Color', 'k', 'LineWidth', 0.5);

yline(0, 'Color', 'k', 'LineWidth', 0.5);

now i would like to see the effects of the two constants e and t on the above mentioned boundary. i would like to plot different curves with varying e and t on the same graph but i am having troubles to understand an efficient way to do it.

e and t are two arrays such as linspace(-25, 25, 3), so i want to check how the plot evolves over 3x3 combiations of e and t.

i tried nesting for loops but it didn't work as i got a blank plot. could anybody please give me any suggestions as to do it with for loops or with any other way?

i know i could do it "manually", changing e and t every time and using hold on to plot the curves on the same figure but it is rather inefficient.

thanks to anyone who will help

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Alan Stevens on 2 Jul 2024 at 10:46

  • Link

    Direct link to this answer

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#answer_1479821

  • Link

    Direct link to this answer

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#answer_1479821

Open in MATLAB Online

Here's another way, still using nested loops:

n= 101;

x = linspace(-100, 100, n);

y = linspace(-100, 100, n);

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

figure

xline(0, 'Color', 'k', 'LineWidth', 0.5);

yline(0, 'Color', 'k', 'LineWidth', 0.5);

hold on

for e = -25:10:25

for t = -25:10:25

Z = fn(X,Y,e,t,n);

v = [0,0];

contour(X,Y,Z,v, 'LineWidth', 1.5)

end

end

grid on

axis equal

help with nested loop and plot (3)

function Z = fn(X,Y,e,t,n)

Z = zeros(n, n);

B = X + Y + e + t;

D = X.*Y - e.*t;

for i= 1:n

for j= 1:n

if B(i,j) >= 0

Z(i,j) = D(i,j);

else

Z(i,j) = -1;

end

end

end

end

9 Comments

Show 7 older commentsHide 7 older comments

LUCA D'AMBROSIO on 2 Jul 2024 at 17:08

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3200681

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3200681

thank you very much, it is what i was looking for.

is there a way to make the different curves in different colors?

Alan Stevens on 3 Jul 2024 at 9:22

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3201276

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3201276

Open in MATLAB Online

Here's a quick and dirty way:

n= 101;

x = linspace(-100, 100, n);

y = linspace(-100, 100, n);

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

clrs = ['r','g','b','c','m','y'];

figure

xline(0, 'Color', 'k', 'LineWidth', 0.5);

yline(0, 'Color', 'k', 'LineWidth', 0.5);

hold on

c = 0;

for e = -25:10:25

for t = -25:10:25

c = c+1;

Z = fn(X,Y,e,t,n);

v = [0,0];

c = mod(c,5)+1;

contour(X,Y,Z,v, 'LineWidth', 1.5,'LineColor', clrs(c))

end

end

grid on

axis equal

help with nested loop and plot (6)

function Z = fn(X,Y,e,t,n)

Z = zeros(n, n);

B = X + Y + e + t;

D = X.*Y - e.*t;

for i= 1:n

for j= 1:n

if B(i,j) >= 0

Z(i,j) = D(i,j);

else

Z(i,j) = -1;

end

end

end

end

LUCA D'AMBROSIO on 3 Jul 2024 at 10:07

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3201311

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3201311

thank you very much

Torsten on 3 Jul 2024 at 10:29

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3201336

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3201336

Edited: Torsten on 3 Jul 2024 at 12:48

You must have at least 6x7/2 = 21 different colors for your nested loop over e and t ...

LUCA D'AMBROSIO on 5 Jul 2024 at 18:11

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3203871

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3203871

hi, sorry to bother you again.

i slightly changed the code to try and create a legend and to make it more readable with subplots. problem is the colors and the legend don't match the lines in the graphs.

would you be so kind to take a look and help me find the mistake? thank you

n = 1001;

x = linspace(-100, 100, n);

y = linspace(-100, 100, n);

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

clr = ['r','g','b','c','k'];

a = 4;

e = [-50 -10 0 50];

t = linspace(-50, 50, 5);

LegendsStrings = cell(length(t),1);

for q = 1:a

for r = 1:5

Z = fn(X, Y, e(q), t(r), n);

subplot(2, 2, q)

xline(0, 'Color', 'k', 'LineWidth', 0.5);

yline(0, 'Color', 'k', 'LineWidth', 0.5);

hold on

v = [0, 0];

contour(X, Y, Z, v, 'LineWidth', 1.5, 'LineColor', clr(r))

xlabel('x')

ylabel('y')

title("e = " + e(q))

grid on

axis equal

hold off

LegendsStrings{r} = ['t = ', num2str(t(r))];

end

legend(LegendsStrings, 'Interpreter', 'none')

end

Alan Stevens on 6 Jul 2024 at 8:32

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3204131

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3204131

Open in MATLAB Online

Here's a possibility (though there is probably a much better one!). Note that I've just used n = 101 here for speed. You can change this to whatever you want.

n = 101;

x = linspace(-100, 100, n);

y = linspace(-100, 100, n);

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

clr = ['r','g','b','c','k'];

v = [0, 0];

a = 4;

e = [-50 -10 0 50];

t = linspace(-50, 50, 5);

% LegendsStrings = cell(length(t),1);

for q = 1:a

for r = 1:5

Z = fn(X, Y, e(q), t(r), n);

subplot(2, 2, q)

xline(0, 'Color', 'k', 'LineWidth', 0.5);

yline(0, 'Color', 'k', 'LineWidth', 0.5);

hold on

contour(X, Y, Z, v, 'LineWidth', 1.5, 'LineColor', clr(r))

xlabel('x')

ylabel('y')

title("e = " + e(q))

xp = [-95, -80]; yp = (-30-12*r)*ones(1,2);

plot(xp,yp,['-',clr(r)])

text(xp(2)+5, yp(2),['t = ', num2str(t(r))],'FontSize',8)

grid on

axis equal

hold off

end

end

help with nested loop and plot (11)

function Z = fn(X,Y,e,t,n)

Z = zeros(n, n);

B = X + Y + e + t;

D = X.*Y - e.*t;

for i= 1:n

for j= 1:n

if B(i,j) >= 0

Z(i,j) = D(i,j);

else

Z(i,j) = -1;

end

end

end

end

LUCA D'AMBROSIO on 6 Jul 2024 at 9:19

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3204151

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3204151

thank you, it works well.

i don't understand why you create the two arrays xp and yp, though.

Alan Stevens on 6 Jul 2024 at 11:47

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3204216

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3204216

Edited: Alan Stevens on 6 Jul 2024 at 12:01

Open in MATLAB Online

"...i don't understand why you create the two arrays xp and yp, though."

Comment them out and see what happens!

Incidentally, you could eliminate the need for your function fn, by using Matlab's indexing capabilities:

n = 101;

x = linspace(-100, 100, n);

y = linspace(-100, 100, n);

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

clr = ['r','g','b','c','k'];

v = [0, 0];

a = 4;

e = [-50 -10 0 50];

t = linspace(-50, 50, 5);

for q = 1:a

for r = 1:5

% the following three lines replace the need for the function

B = X + Y + e(q) + t(r);

Z = X.*Y - e(q).*t(r);

Z(B<0) = -1;

subplot(2, 2, q)

xline(0, 'Color', 'k', 'LineWidth', 0.5);

yline(0, 'Color', 'k', 'LineWidth', 0.5);

hold on

contour(X, Y, Z, v, 'LineWidth', 1.5, 'LineColor', clr(r))

xlabel('x')

ylabel('y')

title("e = " + e(q))

xp = [-95, -80]; yp = (-30-12*r)*ones(1,2); % coordinates for "legend" lines

plot(xp,yp,['-',clr(r)]) % "legend" lines

text(xp(2)+5, yp(2),['t = ', num2str(t(r))],'FontSize',8) % "legend" text

grid on

axis equal

hold off

end

end

help with nested loop and plot (14)

LUCA D'AMBROSIO on 6 Jul 2024 at 14:14

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3204321

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3204321

i understand it more now. thank you for thaking time to answer my questions. i have been using matlab for the last month so it's nice when someone explains "tricks" to make code more efficient.

thank you very much

Sign in to comment.

More Answers (1)

Aquatris on 2 Jul 2024 at 10:37

  • Link

    Direct link to this answer

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#answer_1479806

  • Link

    Direct link to this answer

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#answer_1479806

Edited: Aquatris on 2 Jul 2024 at 10:38

Open in MATLAB Online

Here is one simple dirty way:

n= 101;

x = linspace(-100, 100, n);

y = linspace(-100, 100, n);

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

Z = zeros(n, n);

cnt = 0;

% this nested loop can probably be vectorized

for e = 8:12

for t = -27:-24

cnt = cnt+1;

B = X + Y + e + t;

D = X.*Y - e.*t;

% find indeces where both conditions hold

idx = find(B >= 0 & D>=0);

% store points that satisfy both conditions

Xsol{cnt} = X(idx);

Ysol{cnt} = Y(idx);

lgdText{cnt} = (sprintf('e = %d, t = %d',e,t));

end

end

for i = 1:length(Xsol)

plot(Xsol{i},Ysol{i},'.')

hold on

end

xlabel('X')

ylabel('Y')

xlim([-100 100])

ylim([-100 100])

legend(lgdText)

title('Points that satisfy both conditions')

hold off

help with nested loop and plot (17)

1 Comment

Show -1 older commentsHide -1 older comments

LUCA D'AMBROSIO on 2 Jul 2024 at 17:08

Direct link to this comment

https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3200686

  • Link

    Direct link to this comment

    https://connections.mathworks.com/matlabcentral/answers/2133726-help-with-nested-loop-and-plot#comment_3200686

thank you for your help

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphicsFormatting and AnnotationLabels and AnnotationsLegend

Find more on Legend in Help Center and File Exchange

Tags

  • nested loops
  • for loop
  • plot
  • hold
  • loops
  • cycles
  • grid
  • meshgrid

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


help with nested loop and plot (19)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

help with nested loop and plot (2024)
Top Articles
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 6636

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.