How to increment a for loop by an arbitrary value?

I am creating a Matlab program that calculates corresponding temperatures for Celsius, Kelvin, Fahrenheit, and Rankine scales. I want to write my code so that it can handle an arbitrary temperature increment (a user-inputted value).
Here is my current code (it currently increments by 1 degree Celsius--I want to change this):
Cstart = -50;
Cend = 100;
for Celsius = Cstart:Cend
Celius = Celsius
Kelvin = Celsius + 273.15
Fahrenheit = 1.8.*Celsius + 32
Rankine = (Celsius+273.15)*1.8
end
plot(Celsius,Kelvin,'r')
hold on
plot(Celsius,Fahrenheit,'b')
hold on
plot(Celsius,Rankine,'g')
legend('Kelvin','Fahrenheit','Rankine','FontSize',20)
xlabel('Temperature in Celsius','FontSize',20)
ylabel('Converted Temperature','FontSize',20)
title('Equivalent Temperatures','FontSize',20)
set(gca,'FontSize',20)
Thank you in advance!

답변 (1개)

Amit
Amit 2014년 1월 22일
편집: Amit 2014년 1월 22일
Cstart = -50;
Cend = 100;
step = input('Enter the increment ');
Celsius = Cstart:step:Cend;
%for ii = 1:numel(Celcius)
% Celius = Celsius
Kelvin = Celsius + 273.15
Fahrenheit = 1.8.*Celsius + 32
Rankine = (Celsius+273.15)*1.8
%end
plot(Celsius,Kelvin,'r',Celsius,Fahrenheit,'b',Celsius,Rankine,'g')
% hold on
% plot(Celsius,Fahrenheit,'b')
% hold on
% plot(Celsius,Rankine,'g')
legend('Kelvin','Fahrenheit','Rankine','FontSize',20)
xlabel('Temperature in Celsius','FontSize',20)
ylabel('Converted Temperature','FontSize',20)
title('Equivalent Temperatures','FontSize',20)
set(gca,'FontSize',20)
Moreover, you cannot plot this way. I modified it so that Celcius doesn't stay a scalar. Also, you can skip the looping.

댓글 수: 8

Kelsey
Kelsey 2014년 1월 22일
Thank you so much! Very very helpful! :)
Amit
Amit 2014년 1월 22일
You're welcome. Fontsize 20 is a little bit large for me, but your choice, so I didnot changed that.
Kelsey
Kelsey 2014년 1월 22일
I agree, but once I paste it into a document and scale it, usually it needs to be pretty big in order for the labels to not be super tiny.
Amit
Amit 2014년 1월 22일
Also, can you accept the answer when they work for you. You have never accepted a single answer from all the questions you've asked.
Kelsey
Kelsey 2014년 1월 22일
How can I get the Celsius value to show up in the Command Window (but not on the graph)?
Type
Celsius
After you have run the code. This will show the Celsius vector on command line.
Kelsey
Kelsey 2014년 1월 22일
Okay great! Is there any way to have it show up automatically?
Remove the ';' from Celsius as
Celsius = Cstart:step:Cend

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 1월 22일

댓글:

2014년 1월 22일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by