• Support PF! Buy your school textbooks, materials and every day products Here!

MATLAB Programming Loops Question

  • #1

Homework Statement



Write a program that does the following:
  • Prints the four gases on the screen and asks the user to select which gas to find the heat capacity for.
  • Asks the user for a temperature.
  • Asks the user if another temperature is needed (enter yes or no). If the answer is yes, the user is asked to enter another temperature. This process continues until the user enters no.
  • Display a table containing the temperatures entered and the corresponding heat capacities.

The table containing the gases and the values for a, b, c and d is attached.



Homework Equations



Heat Capacity = Cp

Cp = a + bT + cT^2 + dT^3

The Attempt at a Solution



display('-----------------------------------------------------------------')
clear

display('Problem 32')

SO2.a = 38.91; SO2.b = 3.904*10^-2; SO2.c = -3.105*10^-5; SO2.d = 8.606*10^-9;
SO3.a = 48.50; SO3.b = 9.188*10^-2; SO3.c = -8.540*10^-5; SO3.d = 32.40*10^-9;
O2.a = 29.10; O2.b = 1.158*10^-2; O2.c = -0.6076*10^-5; O2.d = 1.311*10^-9;
N2.a = 29.00; N2.b = 0.2199*10^-2; N2.c = -0.5723*10^-5; N2.d = -2.871*10^-9;

inputval = input('Select a gas, SO2, SO3, O2, or N2: ');
T = input('Input a temperature, in celsius,: ');
Cp1 = inputval.a + inputval.b*T + inputval.c*(T)^2 + inputval.d*(T)^3;
Table = [T Cp1]

reply = input('Do you want to input another temperature? Y/N: ','s');
Cp2 = 0;
Table1 = [];
while reply == 'Y'
T1 = input('Input another temperature: ');
Cp2 = inputval.a + inputval.b*T1 + inputval.c*(T1)^2 + inputval.d*(T1)^3;
Table1 = [T1 Cp2];
Table1 = [Table1 ; Table1];

reply = input('Do you want to input another temperature? Y/N: ','s');
end
answer = [Table; Table1]
disp(answer)


I'm not sure where my error is, could someone help?
 

Attachments

Answers and Replies

  • #2
Student100
Education Advisor
Gold Member
1,649
416
Hmm, your code crashed my Matlab!

Okay, so the crashing was my computer.. look really close at how your handling your temps in the while loop.

....
Do you want to input another temperature? Y/N: Y
Input another temperature: 10
Do you want to input another temperature? Y/N: Y
Input another temperature: 20
Do you want to input another temperature? Y/N: Y
Input another temperature: 30
Do you want to input another temperature? Y/N: Y
Input another temperature: 40
Do you want to input another temperature? Y/N: N
32.0000 40.1278
40.0000 40.4225
30.0000 40.0535
20.0000 39.6784
10.0000 39.2973

...

Is what you want to see right? Each temp that was entered? So look at how your handling the temps being put into the tables... As a further hint, check out the cat() function.

Good luck!
 
Last edited:
  • #3
Hi, thank you for replying! I've fixed my problem that it kept closing after running the script, but at the moment I've got this:

while reply == 'Y'
T1 = input('Input another temperature: ');
Cp2 = inputval.a + inputval.b*T1 + inputval.c*(T1)^2 + inputval.d*(T1)^3;
Table1 = [T1 Cp2];


reply = input('Do you want to input another temperature? Y/N: ','s');
end
answer = cat(1,Table, Table1)

And all this yields me is the final value that I put in for T, and the corresponding Cp. Is there any way to simply add on the next values to the existing matrix? I've tried simply adding them like [A B] and using the cat() function, but I don't believe I am doing it right?
 
  • #4
Student100
Education Advisor
Gold Member
1,649
416
To get it to work, try adding another table in the while loop that will cat table1 with that table(inside the loop!), use vertical cat. When you display the anwser you should use the new table and not the table you're placing the values in during the loop iteration. This should print out Cp and all of the vaules for Cp2.

When you're going through the program use the variable editor to check that the logic is doing what you want it to, you'll probably kick yourself when you see how simple it is.
 
Last edited:
  • #5
I apologize, I really do, but I'm not entirely sure how to do what you are talking about. The problem I seem to be having is that every time the user puts in the reply of 'Y', and inputs a new T1 (hence creating a new Cp2), it seems to overwrite the previous one. Thus, when I display the end, all I get is a 2 by 2 matrix, instead of however many times the user pressed 'Y' plus one. It probably is very simple, but at this moment I don't seem to be getting the right answer.

I did try creating a new table like you had said, however I'm not sure how to input values into this table, so I got stuck there.
 
  • #6
Student100
Education Advisor
Gold Member
1,649
416
Yes, because you're overwriting the table each iteration of the loop. That's why the simplest and most logical way to take care of it(at least it makes sense to me.. I'm sure there are easier ways to do this in MatLab) is to create another Table variable and cat the other variables with it.

When you run through the code at the moment, your Table 1 keeps getting over written by each User input.

Here's some code showing an outline of what needs to happen to create a matrix as big as the number of Y's.
...

Table1 = [];
Table2 = [];
while reply == 'Y'
T1 = input('Input another temperature: ');
Cp2 = inputval.a + inputval.b*T1 + inputval.c*(T1)^2 + inputval.d*(T1)^3;
Table1 = [T1 Cp2];
Table2 = cat(1, x, y);

reply = input('Do you want to input another temperature? Y/N: ','s');
end

...

Think about what input arguments you'd need in the cat Table to save the information for the next round of the while loop. :)

Don't forgot about what you're displaying out with your call to display at the end of your code and how you might have to change it as well.

Here is the output I'm getting just looking at the while loop..

...

-----------------------------------------------------------------
Problem 32
Select a gas, SO2, SO3, O2, or N2: O2
Input a temperature, in celsius,: 32

Table =

32.0000 29.4644

Do you want to input another temperature? Y/N: Y
Input another temperature: 10
Do you want to input another temperature? Y/N: Y
Input another temperature: 20
Do you want to input another temperature? Y/N: Y
Input another temperature: 30
Do you want to input another temperature? Y/N: Y
Input another temperature: 40
Do you want to input another temperature? Y/N: Y
Input another temperature: 50
Do you want to input another temperature? Y/N: N
32.0000 29.4644
50.0000 29.6640
40.0000 29.5536
30.0000 29.4420
20.0000 29.3292
10.0000 29.2152

...

I think this is the output you're looking for.
 
  • #7
Thanks for your help! I don't think I did it the way you were telling me, but the output was the same. Here was my final code:

Code:
SO2.a = 38.91; SO2.b = 3.904*10^-2; SO2.c = -3.105*10^-5; SO2.d = 8.606*10^-9;
SO3.a = 48.50; SO3.b = 9.188*10^-2; SO3.c = -8.540*10^-5; SO3.d = 32.40*10^-9;
O2.a = 29.10; O2.b = 1.158*10^-2; O2.c = -0.6076*10^-5; O2.d = 1.311*10^-9;
N2.a = 29.00; N2.b = 0.2199*10^-2; N2.c = -0.5723*10^-5; N2.d = -2.871*10^-9;

inputval = input('Select a gas, SO2, SO3, O2, or N2: ');
T = input('Input a temperature, in celsius,: ');
Cp1 = inputval.a + inputval.b*T + inputval.c*(T)^2 + inputval.d*(T)^3;
Table = [T Cp1]

reply = input('Do you want to input another temperature? Y/N: ','s');
Cp2 = 0;
k = 1;
Table1 = [];

while reply == 'Y'
T1 = input('Input another temperature: ');
Cp2 = inputval.a + inputval.b*T1 + inputval.c*(T1)^2 + inputval.d*(T1)^3;
Table1(k,:) = [T1, Cp2];
k = k+1;

reply = input('Do you want to input another temperature? Y/N: ','s');
end
answer = vertcat(Table, Table1)
fprintf('\n')
 
  • #8
Student100
Education Advisor
Gold Member
1,649
416
Cool! Glad you were able to solve the problem. :)

This was the way I did it incase you wanted to know.

...
Table1 = [];
Table2 = [];
while reply == 'Y'
T1 = input('Input another temperature: ');
Cp2 = inputval.a + inputval.b*T1 + inputval.c*(T1)^2 + inputval.d*(T1)^3;
Table1 = [T1 Cp2];
Table2 =cat(1, Table2, Table1);

reply = input('Do you want to input another temperature? Y/N: ','s');
end
answer = [Table; Table2];
...

Your program looks good.
 

Related Threads on MATLAB Programming Loops Question

Replies
1
Views
3K
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
18
Views
9K
Replies
4
Views
4K
  • Last Post
Replies
3
Views
21K
  • Last Post
Replies
8
Views
1K
  • Last Post
Replies
22
Views
5K
  • Last Post
Replies
9
Views
4K
  • Last Post
Replies
15
Views
5K
  • Last Post
Replies
0
Views
5K
Top