How to increment a for loop by an arbitrary value?

1 visualización (últimos 30 días)
Kelsey
Kelsey el 22 de En. de 2014
Comentada: Amit el 22 de En. de 2014
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!

Respuestas (1)

Amit
Amit el 22 de En. de 2014
Editada: Amit el 22 de En. de 2014
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 comentarios
Kelsey
Kelsey el 22 de En. de 2014
Okay great! Is there any way to have it show up automatically?
Amit
Amit el 22 de En. de 2014
Remove the ';' from Celsius as
Celsius = Cstart:step:Cend

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by