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

  • Context: MATLAB 
  • Thread starter Thread starter LakeMountD
  • Start date Start date
  • Tags Tags
    Figure Matlab Work
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 5K views
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.