Why are the lines not showing up on my graph?
Show older comments
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
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
Both scalars:
fd0
i
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 θ')
Carine
on 26 Sep 2024
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
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 θ')
Accepted Answer
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
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


