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

3· The average monthly temperature in city A in 2016 is given by a table below.

ID: 2249874 • Letter: 3

Question

3· The average monthly temperature in city A in 2016 is given by a table below. Average monthly temperature in City A in 2016 10 12 21 40 60 82 96 46 34 a. Create a file datatemp.txt for the table above. b. Write a M-file to load the file datatemp.txt, and to find the best fitted curve using MATLAB's curve fitting feature and write the equation of the curve. Plot the original data points and the fitted curve on the same graph. The graph should be appropriately labeled and presented. c. Write a function to load the file datatemp.txt, and to interpolate the values of temperature between any two months using "spline". The function will ask the user to input a month (e.g. for the month of April, enter 4), and output the average temperature on that month. Find the temperatures at the months of February and June.

Explanation / Answer

a) Generate a text file and type the contents as displayed below in the next line and save the file as datatemp.txt.

1,21
3,40
5,60
7,82
8,96
10,46
12,34

-------------------------------------------------------------------- (Do not enter this dotted line. This line is used for demarkation.)

b) Write a matlab m-file code as written below and save it as program.m. The program loads data from the datatemp.txt file, performs cubic interpolation on the data, plots the data in figure(1).

-------------------------- CODE BEGINS ------------------------------------

% load the data from file

fid = fopen('datatemp.txt','r');

data = textscan(fid,'%d%d','delimiter',',');

fclose(fid);

month = data{1};

temp = data{2};

% perform cubic interpolation

pp = spline(month, temp);

% Generate the data from all the months

% x variable refers to month

x = 1:12

for k=1:12

y(k) = ppval(pp, x(k));

end

% y variable has the interpolated values

% plot the samples and curve

figure(1);

plot(month,temp,'o');

hold on;

plot(x,y,'r-');

xlabel('Month');

ylabel('Temperature');

title('Plot of average temperature in a month');

legend('Data','Interpolation');

---------------------- CODE ENDS -----------------------------------

----------------------------------------------------------------------------------------------------------------

c) A matlab function is written in the code format save it as avgTemp.m and use the command as out = avgTemp(4) as an example to obtain the interpolated average temperature in a month.

--------------------- CODE BEGINS -------------------------------

function out = avgTemp(monthNumber)
% load the data from file
fid = fopen('datatemp.txt','r');
data = textscan(fid,'%d%d','delimiter',',');
fclose(fid);
month = data{1};
temp = data{2};

% perform cubic interpolation
pp = spline(month, temp);

out = ppval(pp, monthNumber);
end

----------------------------- CODE ENDS ----------------------------------------

The temperatures at months of February and June can be obtained by running the above function in matlab command window as,

FebruaryTemperature = avgTemp(2);

FebruaryTemperature = 30. (This is observed in the workspace)

JuneTemperature = avgTemp(6);

JuneTemperature = 68. (This is observed in the workspace)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote