Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Feb 1, 2006 #1
    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. jcsd
  3. Feb 1, 2006 #2
    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-+')
     
  4. Feb 1, 2006 #3
    ??? Error using ==> /
    Matrix dimensions must agree.


    that is the error I am getting when i tried it the way you just posted.. any ideas?
     
  5. Feb 1, 2006 #4
    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?
     
  6. Feb 1, 2006 #5
    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.
     
  7. Feb 1, 2006 #6
    Arg, why. Thats such a waste of CPU speed and its really bad coding.
     
  8. Feb 2, 2006 #7
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Matlab help. Can't figure out why this wont work!
  1. MATLAB figure in LATEX (Replies: 5)

Loading...