Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The heat index HI, calculated from the air temperature and relative humidity, is

ID: 2258184 • Letter: T

Question

The heat index HI, calculated from the air temperature and relative humidity, is the apparent temperature felt by the body. An equation used by the National Weather Service for calculating the HI is given by: HI = C1 + C2T + C3R + C4TR + C5T 2 + C6R 2 + C7T 2R + C8TR2 + C9T 2R 2 Where T is the temperature in °F, and R is the relative humidity in integer percentage. Write a MATLAB program that reads a file having the following format:

Temperature 80 82 84 86 88 90 92 94

Relative Humidity 50 55 60 65 70 75

Coefficients -42.379 2.04901523 10.14333127 -0.22475541 -6.83783e-3 - 5.481717e-2 1.22874e-3 8.5282e-4 -1.99e-06

In the file above, it is unknown ahead of time how many numbers will be on the Temperature and Relative Humidity lines. There will be exactly nine numbers on the Coefficients line. In the example above, the numbers wrapped to the next line, but in reality, all the Coefficient numbers will be on the same line. All the numbers will likely be different than shown in the file above. Next, compute HI for each combination of T and R. Your results should be written to a file as shown below:

Temperature (F)

1                                             80 82 84 86 88 90 92 94

Relative Humidity (%)

50        81        83        85       88        91        95        99        103

55        81        84        86        89       93       97       101     106

60        82        84       88       91        95       100     105     110

65        82        85        89        93       98        103      108      114

70        83        86        90        95        100      106     112     119

75       84        88        92        97        103      109      116      124

Each of the columns above is 10 characters wide. Here is a printout of what the user should see while running your program at the command window:

>> miniproject input filename : input.txt

output filename : output.txt

Hints One of the sticky problems that you must deal with is not knowing how many temperatures or relative humidities will be in the input file. An elegant solution to this problem is the built-in MATLAB function repmat. For the numbers shown in the example above, I recommend using repmat to build a Tmat matrix like this:

Tmat =

80 82 84 86 88 90 92 94

80 82 84 86 88 90 92 94

80 82 84 86 88 90 92 94

80 82 84 86 88 90 92 94

80 82 84 86 88 90 92 94

80 82 84 86 88 90 92 94

Notice that Tmat above has as many rows as there are relative humidity numbers, and this number can be found with the built-in function length. Also use repmat to build an Rmat matrix, having as many columns as you have temperature numbers:

Rmat =

50 50 50 50 50 50 50 50

55 55 55 55 55 55 55 55

60 60 60 60 60 60 60 60

65 65 65 65 65 65 65 65

70 70 70 70 70 70 70 70

75 75 75 75 75 75 75 75

Now it’s easy to do an element-by-element calculation and calculate all the HI numbers, using just one line of code. A final problem is constructing the call to fprintf, not knowing how many numbers you will be printing ahead of time. Repmat is, again, an elegant solution to this problem. For example, the following line of code will create a format string, suitable for using in fprintf, that will print n numbers per row:

format_string = [repmat('%10.0f',1,n) ' '];

Explanation / Answer

fid = fopen('inputFile.txt');
T = str2num(fgetl(fid));
R = str2num(fgetl(fid));
C = str2num(fgetl(fid));
fclose(fid);
T = repmat(T,length(R),1);
R = repmat(R',1,length(T));
HI = C(1) + C(2)*T + C(3)*R + C(4)*(T.*R) + C(5)*(T.^2) + ...
C(6)*(R.^2) + C(7)*(T.^2).*R + C(8)*T.*(R.^2) + C(9)*(T.^2).*(R.^2);
fid1 = fopen('outputFile.txt','wt');
format_string = [repmat('%10.4f',1,8) ' '];
fprintf(fid1,format_string,HI);
fclose(fid1);