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

In summary, the conversation is about creating a Matlab program that calculates temperatures for different scales and handling an arbitrary temperature increment. The current code increments by 1 degree Celsius and the speaker wants to change it. They mention using an optional increment expression in a for loop.
  • #1
kal854
13
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
  • #2
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.
 
  • #3
kal854 someone has been on yahoo answers...
 

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

1. How do I increment a for loop by a specific value in MATLAB?

To increment a for loop by a specific value, you can use the "step" argument in the for loop syntax. For example, if you want to increment by 2, you can use the syntax "for i = 1:2:n", where n is the upper limit of your loop.

2. Can I increment a for loop by a decimal value in MATLAB?

Yes, you can increment a for loop by a decimal value in MATLAB. However, the step value must be a whole number. To increment by a decimal value, you can use the "step" argument along with the "linspace" function. For example, "for i = linspace(1,10,0.5)" will increment the loop by 0.5 from 1 to 10.

3. Is it possible to increment a for loop by a negative value in MATLAB?

Yes, it is possible to increment a for loop by a negative value in MATLAB. You can use the "step" argument with a negative value to decrement the loop. For example, "for i = 10:-2:1" will decrement the loop by 2 from 10 to 1.

4. How can I increment a for loop by a user-defined value in MATLAB?

To increment a for loop by a user-defined value, you can use the "input" function to prompt the user to enter the desired value. Then, you can use that value in the "step" argument of the for loop. For example, "n = input('Enter increment value: '); for i = 1:n:10" will prompt the user to enter a value and then increment the loop by that value from 1 to 10.

5. Can I use a variable as the step value in a for loop in MATLAB?

Yes, you can use a variable as the step value in a for loop in MATLAB. This can be useful if you want to dynamically change the step value during the execution of the loop. Just make sure the variable is defined and has a numerical value before using it in the loop. For example, "n = 2; for i = 1:n:10" will use the value of n as the step value.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top