MATLAB Why Does My MATLAB Plot Show Only One Point Instead of a Line?

AI Thread Summary
The discussion revolves around plotting the cost associated with varying radius values for a cylindrical structure with a hemispherical top, given a maximum volume constraint. The initial attempt to plot resulted in only one point being displayed due to the lack of proper indexing in the loop. The solution suggested involves using element-wise operations and ensuring that the plotting function accumulates points rather than replacing them. The corrected code utilizes a vectorized approach for calculations, which improves efficiency and clarity. The final implementation shows that using a loop with `hold on` allows for multiple points to be plotted without significant CPU overhead, although some participants express concern over the inefficiency of plotting within a loop. The elapsed time for the operations is noted, highlighting the minimal impact on performance.
LakeMountD
Messages
59
Reaction score
0
for r = 2:.1:10
h=(500/(pi*r^2))-(2*r); %Height for given r assuming max volume of 500m^3
a=2*pi*r*h; %Surface area for cylindrical part
b=2*pi*r^2; %Surface area for hemispherical top
t=a*300+b*400; %Total cost using values of surface area
plot(r,t,'r-+');
end

I am just trying to get it to plot each r value against its respective cost (t value) but it only gives me one point on the graph. Why won't it create a string of points?
 
Physics news on Phys.org
because you lack an index. Just do this:

r = 2:.1:10;
h=(500/(pi*r.^2))-(2.*r); %Height for given r assuming max volume of 500
m^3
a=2*pi*.r*h; %Surface area for cylindrical part
b=2*pi*r.^2; %Surface area for hemispherical top
t=(a.*300)+(b.*400); %Total cost using values of surface area
plot(r,t,'r-+')
 
? Error using ==> /
Matrix dimensions must agree.that is the error I am getting when i tried it the way you just posted.. any ideas?
 
r = 2:.1:10;
h=(500./(pi*r.^2))-(2.*r); %Height for given r assuming max volume of 500
m^3
a=2*pi*.r*h; %Surface area for cylindrical part
b=2*pi*r.^2; %Surface area for hemispherical top
t=(a.*300)+(b.*400); %Total cost using values of surface area
plot(r,t,'r-+')

Does that work?
 
Code:
figure;hold on
for r = 2:.1:10
h=(500/(pi*r^2))-(2*r); %Height for given r assuming max volume of 500m^3
a=2*pi*r*h; %Surface area for cylindrical part
b=2*pi*r^2; %Surface area for hemispherical top
t=a*300+b*400; %Total cost using values of surface area
plot(r,t,'r-+');
end

You can do it one point at a time like you originally had, but you have to add "figure; hold on" before the loop. Now every time you do plot(r,t) it will add points to the plot, rather than replace the previous point.
 
Arg, why. Thats such a waste of CPU speed and its really bad coding.
 
cyrusabdollahi said:
Arg, why. Thats such a waste of CPU speed and its really bad coding.

Code:
clear all
tic
r = 2:.1:10;
h=(500./(pi*r.^2))-(2.*r); %Height for given r assuming max volume of 500 m^3
a=2*pi.*r.*h; %Surface area for cylindrical part
b=2*pi*r.^2; %Surface area for hemispherical top
t=(a.*300)+(b.*400); %Total cost using values of surface area
figure;plot(r,t,'r-+')
toc

clear all
tic
figure;hold on
for r = 2:.1:10
h=(500/(pi*r^2))-(2*r); %Height for given r assuming max volume of 500m^3
a=2*pi*r*h; %Surface area for cylindrical part
b=2*pi*r^2; %Surface area for hemispherical top
t=a*300+b*400; %Total cost using values of surface area
plot(r,t,'r-+');
end
toc

Matlab said:
Elapsed time is 0.123388 seconds.
Elapsed time is 0.125952 seconds.
>>

Oh no, 0.0026 seconds! All my CPU speed are belong to Matlab.
 

Similar threads

Replies
4
Views
2K
Replies
1
Views
3K
Replies
4
Views
2K
Replies
7
Views
3K
Replies
6
Views
5K
Back
Top