Why are the lines not showing up on my graph?

17 ビュー (過去 30 日間)
Bailey Eaton
Bailey Eaton 2021 年 1 月 31 日
コメント済み: Voss 2024 年 9 月 26 日
I need to show two separate lines on the same figure, one for dcs and one for d2p, using the values provided by each iteration of the for loop. I'm not sure what I'm doing wrong here. Thanks in advance! Here is my code:
N = [10 90 1000]; % number of peers
F = 20 * 1024; % converting Gbits to Mbits
us = 30; % server upload
di = 2; % client download
u = 300/1024; % client upload
dcs = zeros(size(N));
dp2 = zeros(size(N));
figure();
grid
hold on;
for i = 1:1:length(N)
Nval = N(i);
cs1 = Nval*F/us;
cs2 = F/di;
dcs = max(cs1,cs2);
p21 = F/us;
ui = u*Nval;
p23 = Nval*F/(us+ui);
dp2 = max([p21(:); cs2(:); p23(:)]);
plot(dcs, Nval);
plot(dp2, Nval);
title('Distribution Time')
end
ylabel('Minimum Distribution Time')
xlabel('N')
  4 件のコメント
Carine
Carine 2024 年 9 月 26 日
what should i change on that code to make it a non-scalar?
Voss
Voss 2024 年 9 月 26 日
If you want to store the fd0 value from each iteration, something like this:
clear;
x0 = 1; eps = 0.000001; p = 425; f = @(x) p*((3^.5)*sind(x)+p*cosd(x))-800; fd = @(x) (3^.5)*p*cosd(x)-p*sind(x); f0 = f(x0); fd0 = fd(x0);
i_all = p:25:800;
N = numel(i_all);
fd0_all = NaN(1,N);
for ii = 1:N
fd0_all(ii) = fd0;
q = p*sind(x0)/sind(30);
fprintf('%4d %8.3f %8.3f %8.3f %8.3f\n', i_all(ii), x0, f0, fd0, q)
x = x0 - f0/fd0; f0 = f(x); fd0 = fd(x);
if abs(x-x0) < eps; break;
end
x0 = x;% q = p*sind(x)/sind(30);
end
425 1.000 179810.337 728.592 14.835 450 -245.791 -74195.506 -689.478 775.250 475 -353.403 178713.453 682.417 97.659 500 -615.285 -45967.358 -598.039 822.123 525 -692.149 159246.266 452.304 397.098 550 -1044.227 146178.039 348.797 496.892 575 -1463.319 164778.858 844.228 -336.477 600 -1658.502 -141696.181 -840.659 529.162 625 -1827.056 159723.226 848.878 -386.630 650 -2015.214 -147947.110 -846.483 490.136 675 -2189.992 155269.748 850.000 -424.903 700 -2372.663 -152464.011 -849.082 458.739 725 -2552.226 151607.313 849.359 -453.272 750 -2730.722 -155698.847 -849.932 434.246 775 -2913.912 148689.225 848.020 -474.231 800 -3089.249 -158036.465 -849.927 415.315
i_all
i_all = 1×16
425 450 475 500 525 550 575 600 625 650 675 700 725 750 775 800
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fd0_all
fd0_all = 1×16
728.5922 -689.4783 682.4174 -598.0387 452.3042 348.7965 844.2285 -840.6588 848.8781 -846.4831 850.0000 -849.0822 849.3586 -849.9325 848.0195 -849.9270
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
plot(fd0_all, i_all, '-s'), grid on, xlabel('Theta (degrees)'), ylabel('P & Q (N)'), hold on; plot(x, q), legend('P', 'Q'), title('Variation of P and Q with respect to θ')

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

採用された回答

Star Strider
Star Strider 2021 年 1 月 31 日
I am not certain what you want to do.
Try this:
N = [10 90 1000]; % number of peers
F = 20 * 1024; % converting Gbits to Mbits
us = 30; % server upload
di = 2; % client download
u = 300/1024; % client upload
dcs = zeros(size(N));
dp2 = zeros(size(N));
figure();
grid
hold on;
for i = 1:1:length(N)
Nval = N(i);
cs1 = Nval*F/us;
cs2 = F/di;
dcs(i) = max(cs1,cs2);
p21 = F/us;
ui = u*Nval;
p23 = Nval*F/(us+ui);
dp2(i) = max([p21(:); cs2(:); p23(:)]);
end
figure();
grid
hold on;
plot(dcs, N, '-p');
plot(dp2, N, '-p');
title('Distribution Time')
ylabel('Minimum Distribution Time')
xlabel('N')
or perhaps instead:
plot(N, dcs, '-p');
plot(N, dp2, '-p');
Otherwise, note that you are plotting points, not lines, so you need to plot markers in the loop.
  2 件のコメント
Bailey Eaton
Bailey Eaton 2021 年 1 月 31 日
This worked! Thank you!
Star Strider
Star Strider 2021 年 1 月 31 日
As always, my pleasure!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 1 月 31 日
N = [10 90 1000]; % number of peers
F = 20 * 1024; % converting Gbits to Mbits
us = 30; % server upload
di = 2; % client download
u = 300/1024; % client upload
dcs = zeros(size(N));
dp2 = zeros(size(N));
figure();
grid
hold on;
for i = 1:1:length(N)
Nval = N(i);
cs1 = Nval*F/us;
cs2 = F/di;
dcs = max(cs1,cs2);
p21 = F/us;
ui = u*Nval;
p23 = Nval*F/(us+ui);
dp2 = max([p21(:); cs2(:); p23(:)]);
plot(dcs, Nval, 'k*-');
plot(dp2, Nval, 'b+-');
title('Distribution Time')
end
ylabel('Minimum Distribution Time')
xlabel('N')
MATLAB only connects points with a line when you plot() or line() in a situation where you have a minimum of two consecutive finite values in a single call. Your dcs and dps2 are scalar and your Nval are as well, so you are never plotting at least two consecutive points in a single call. You should consider storing all of the values in vectors and plotting them after the loop is done.
Also, your code implies that N is the number of peers, which would appear to be the independent variable, and dcs and dps2 would appear to be dependent variables. As such, convention would be that independent variable N should appear on the X axis (like you labeled) and the dependent variable should appear on the Y axis (like you labeled.) But that is not what you plotted: your plot() statements have your dependent variable along the x axis and your independent variable along the y axes.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by