フィルターのクリア

help with nested loop and plot

84 ビュー (過去 30 日間)
LUCA D'AMBROSIO
LUCA D'AMBROSIO 2024 年 7 月 2 日 9:41
コメント済み: LUCA D'AMBROSIO 2024 年 7 月 6 日 14:14
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 日 10:46
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 日 11:47
編集済み: Alan Stevens 2024 年 7 月 6 日 12:01
"...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 日 14:14
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 日 10:37
編集済み: Aquatris 2024 年 7 月 2 日 10:38
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
  1 件のコメント
LUCA D'AMBROSIO
LUCA D'AMBROSIO 2024 年 7 月 2 日 17:08
thank you for your help

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by