Why are the lines not showing up on my graph?

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 Comments

why does lines on my graph does not show? It gives me an empty plot. Here is my code
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);
for i = p:25:800
q = p*sind(x0)/sind(30);
fprintf('%4d %8.3f %8.3f %8.3f %8.3f\n', i, 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
plot(fd0, i), 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 θ')
Check the values you are plotting (fd0 and i). They are both scalars. Plotting a scalar vs a scalar is plotting a single point. A single point will not render without a data marker.
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);
for i = p:25:800
q = p*sind(x0)/sind(30);
fprintf('%4d %8.3f %8.3f %8.3f %8.3f\n', i, 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
Both scalars:
fd0
fd0 = 846.5149
i
i = 800
Here's a plot of i vs fd0 using a data marker ('s' for 'square'); the plotted point is on the top edge of the axes, above the legend.
plot(fd0, i, '-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 θ')
what should i change on that code to make it a non-scalar?
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 θ')

Sign in to comment.

 Accepted Answer

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.

More Answers (1)

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.

Categories

Products

Asked:

on 31 Jan 2021

Commented:

on 26 Sep 2024

Community Treasure Hunt

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

Start Hunting!