Why are the lines not showing up on my graph?
17 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
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
i_all
fd0_all
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
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 件のコメント
その他の回答 (1 件)
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!