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

Using Matlab: % Q1. The vector tempv given below has the highest temperature for

ID: 3688798 • Letter: U

Question

Using Matlab:

% Q1. The vector tempv given below has the highest temperature for each day in May.

% Write a program to compute:
% 1) the number of days with the highest temperature above 50 Fahrenheit
% 2) the average of the highest temperature in the month.
%
% You want to output the calculation result. The output example:
% "There were 19 days with the highest temperature above 50 Fahrenheit in the month."
% "The average of the highest temperature in the month was 53.870968 Fahrenheit."
%
% You are required to use two different approaches as specified below.

clear, clc
tempv = [50, 60, 65, 30, 40, 66, 30, 55, 72, 72, 75, 50, 60, 65, 30, 40, ...
66, 30, 55, 63, 72, 75, 50, 60, 65, 30, 40, 66, 30, 55, 53];

% Q1.1 Approach 1: use a for loop to implement the requirement

% Q1.2 Approach 2: use a while loop to implement the same requirement

Explanation / Answer

% Matlab program to compute:
% 1) the number of days with the highest temperature above 50 Fahrenheit
% 2) the average of the highest temperature in the month.

v= [50, 60, 65, 30, 40, 66, 30, 55, 72, 72, 75, 50, 60, 65, 30, 40, 66, 30, 55, 63, 72, 75, 50, 60, 65, 30, 40, 66, 30, 55, 53];

sz = size(v, 2);
%using for loop
a=0;
sum = 0;
for i=1:sz
sum = sum+v(i);
if v(i)>50
a=a+1;
end
end
avg = sum/sz;
disp('using for loop, temperature average:');
disp(avg);
disp('using for loop, temperature greater than 50:');
disp(a);

%using while loop
a=0;
sum=0;
i=1;
while i<=sz
sum = sum+v(i);
if v(i)>50
a=a+1;
end
i=i+1;
end
avg=sum/sz;
disp('using while loop, temperature average:');
disp(avg);
disp('using while loop, temperature greater than 50:');
disp(a);

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