# Matlab help. Can't figure out why this wont work!

1. Feb 1, 2006

### LakeMountD

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 wont it create a string of points?

2. Feb 1, 2006

### Cyrus

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-+')

3. Feb 1, 2006

### LakeMountD

??? Error using ==> /
Matrix dimensions must agree.

that is the error I am getting when i tried it the way you just posted.. any ideas?

4. Feb 1, 2006

### Cyrus

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?

5. Feb 1, 2006

Code (Text):
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.

6. Feb 1, 2006

### Cyrus

Arg, why. Thats such a waste of CPU speed and its really bad coding.

7. Feb 2, 2006

Code (Text):
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
Oh no, 0.0026 seconds! All my CPU speed are belong to Matlab.