How to increment a MATLAB for loop by an arbitrary value?

Click For Summary
SUMMARY

The discussion focuses on modifying a MATLAB "for" loop to allow for an arbitrary temperature increment specified by the user. The original code increments by 1 degree Celsius, but participants suggest using the syntax for i = start:increment:end to achieve the desired functionality. This adjustment enables users to input any increment value, enhancing the flexibility of the temperature conversion program that calculates Celsius, Kelvin, Fahrenheit, and Rankine scales.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with temperature conversion formulas
  • Knowledge of MATLAB plotting functions
  • Understanding of loop structures in programming
NEXT STEPS
  • Learn how to implement user input in MATLAB using input()
  • Explore MATLAB's linspace() function for generating sequences
  • Investigate advanced plotting techniques in MATLAB for better data visualization
  • Study MATLAB's documentation on control flow and loops for deeper insights
USEFUL FOR

This discussion is beneficial for MATLAB programmers, data analysts, and anyone involved in scientific computing who seeks to enhance their skills in temperature data manipulation and visualization.

kal854
Messages
13
Reaction score
0
How to increment a MATLAB "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!
 
Physics news on Phys.org
kal854 said:
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!

The syntax for a for loop includes an optional increment expression, like this:
Code:
for i = 1.0: 0.1: 10.0
   ...
end
The increment expression is the one in the middle between the starting value and the ending value.
 
kal854 someone has been on yahoo answers...
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K