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
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB plotting issue where a user is attempting to plot a series of cost values against a range of radius values but is only seeing a single point on the graph. The conversation includes troubleshooting the code and exploring different coding approaches to achieve the desired plot.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares their code and expresses frustration that the plot only shows one point instead of a line.
  • Another participant suggests that the issue is due to the lack of an index and provides a modified version of the code, emphasizing the use of element-wise operations.
  • A subsequent participant encounters a matrix dimension error when trying the suggested code and seeks further assistance.
  • Another participant proposes a similar code structure but asks if it resolves the issue.
  • One participant recommends adding "figure; hold on" before the loop to allow multiple points to be plotted without overwriting previous points.
  • Another participant critiques this approach as inefficient and expresses concern about CPU usage, suggesting it is poor coding practice.
  • A later reply reiterates the critique of the original plotting method and compares the execution time of both approaches, highlighting minimal differences in performance.

Areas of Agreement / Disagreement

Participants express differing opinions on the efficiency of the coding methods proposed. While some suggest modifications to resolve the plotting issue, others argue about the implications of those methods on performance and coding standards. No consensus is reached regarding the best approach.

Contextual Notes

Participants mention the importance of using element-wise operations in MATLAB, but there is no agreement on the best coding practices or the most efficient way to achieve the desired plotting outcome.

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 ·
Replies
4
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K