help with nested loop and plot

조회 수: 11 (최근 30일)
LUCA D'AMBROSIO
LUCA D'AMBROSIO 2024년 7월 2일
댓글: LUCA D'AMBROSIO 2024년 7월 6일
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

채택된 답변

Alan Stevens
Alan Stevens 2024년 7월 2일
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
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
Alan Stevens
Alan Stevens 2024년 7월 6일
편집: Alan Stevens 2024년 7월 6일
"...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
LUCA D'AMBROSIO
LUCA D'AMBROSIO 2024년 7월 6일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Aquatris
Aquatris 2024년 7월 2일
편집: Aquatris 2024년 7월 2일
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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by