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

In summary: It would take about 8 hours to run in Matlab if it took 0.0026 seconds for each iteration, but it only takes 0.0001 seconds per iteration on my computer.)I don't think you'll find a faster way to do it, because it's just plotting a large number of points one at a time.
  • #1
LakeMountD
59
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
  • #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-+')
 
  • #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?
 
  • #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?
 
  • #5
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.
 
  • #6
Arg, why. Thats such a waste of CPU speed and its really bad coding.
 
  • #7
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.
 

1. Why is my code not running in Matlab?

There could be several reasons why your code is not running in Matlab. Some common causes include syntax errors, missing or incorrect input data, or issues with the installation or setup of Matlab. Make sure to check your code for any errors and double check your inputs and setup to troubleshoot the issue.

2. How do I debug my Matlab code?

To debug your Matlab code, you can use the built-in debugger tool. Set a breakpoint at the line where you suspect the error is occurring, and then run your code in debug mode. This will allow you to step through each line of code and track the values of variables to identify the source of the issue.

3. What is the best way to learn Matlab?

The best way to learn Matlab is to practice and experiment with the software. You can also refer to online tutorials, forums, and documentation provided by MathWorks, the company behind Matlab. Additionally, taking a course or attending a workshop can also help you gain a better understanding of the software.

4. How do I save my data or plots in Matlab?

To save your data or plots in Matlab, you can use the save function. This allows you to save variables, data structures, and plots to a file that can be loaded and used later. You can also specify the file format, such as .mat for variables or .fig for plots.

5. Can I use Matlab for machine learning?

Yes, Matlab has a suite of tools and functions specifically designed for machine learning and data analysis. It offers capabilities for data preprocessing, feature engineering, and various machine learning algorithms such as neural networks, support vector machines, and decision trees. You can also use Matlab for deep learning applications with its Deep Learning Toolbox.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
986
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
928
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
Back
Top