Help me to do difficult tasks in MATLAB

5 views (last 30 days)
Suzur
Suzur on 7 Nov 2023
Answered: Image Analyst on 7 Nov 2023
Good day, friends! :)
I have no idea how to work in MATLAB, but I need to do some tasks. I have file named "airtempt.mat". When I open it in MATLAB I see in workspace 3 data elements (air 1 (value 1460x1 doubled) , air 2 (value 1460x1 doubled) and t (value 1460x1 doubled)), Then I shoud to do 2 tasks. But i cannot do it, becouse it's my first time in MATLAB and i want to know how can I do it and very need your assistance please (if it possible write MATLAB code to illustrate me how it is working) :)
Here it is:
"
  • Assignment: Statistic Tests
Introduction:
Below each question, please insert MATLAB codes (functions, commands) that you use to solve that question and copy the MATLAB outcomes. Please insert the required figures below the questions, and the figures should be clear and informative.
Load the data file “airtemp.mat” into MATLAB. You can either double click the file, or use command “load airtemp.mat”. Then you will see 3 variables in MATLAB:
air1: Air temperatures every 6 hours for entire year of 2013, at Station SEATS in the South China Sea (18°N, 118°E);
air2: Air temperatures every 6 hours for entire year of 2013, at a location in Sahara Desert (18°N, 20°E);
t: time of each data point in number of days in the year.
Question 1. (C5, CLO3, PLO6)
Consider how to code a MATLAB function to calculate moving average of a variable x:
y = mvavg(x, n),
in which n is the number of data points used for calculating each average. Then, evaluate the daily moving average of air1 and air2 using your coded function.
If you don’t know “moving average”, please check its definition on Internet. You must note that “daily moving average” is differentfrom “daily average”!
Plot on one figure including air1, air2 and their daily moving averages over time. (hint: you can also calculate daily moving average for t, so that the number of elements in the moving average of air1, air2 and t is same.)
Question 2. (C5, CLO3, PLO6)
Plot temperature of the two stations over time on one figure, using markers, no connecting lines. Requirements:
  • if the average temperature of one day (“today”) is higher than the day before by more than 1 degree (i.e. T_today – T_yesterday > 1), then ALL the 4 data points of “today” use red color;
  • if the average temperature of one day (“today”) is lower than the day before by more than 1 degree (i.e. T_yesterday – T_today > 1), then ALL the 4 data points of “today” use blue color;
  • if the difference between T_today and T_yesterday is no more than 1 degree, all the 4 data points of “today” use black color (you can also use black color for the markers of the first day).
  • Assess the differences in the temperature patterns between the two sites. Please explain possible reasons for the differences."
  1 Comment
Dyuman Joshi
Dyuman Joshi on 7 Nov 2023
"I have no idea how to work in MATLAB, but I need to do some tasks."
"But i cannot do it, becouse it's my first time in MATLAB and i want to know how can I do it"
I strongly recommend you to take the free introductory course - MATLAB Onramp to learn the essentials of MATLAB.
After you go through the course, you should be able to write a code. Then, if you are having a probelm, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty.
And the assignment also has hints included in it, so that would be helpful as well.

Sign in to comment.

Answers (2)

Suzur
Suzur on 7 Nov 2023
Edited: Suzur on 7 Nov 2023
CHAT GPT write me this, but it doesn't work:
For question 1, you need to write a function that calculates the moving average of a vector x using a window of size n. Here is one possible way to do that:
function y = mvavg(x,n)
% MVAVG Moving average of a vector
% Y = MVAVG(X,N) returns a vector Y of the same length as X, where each element of Y is the average of N consecutive elements of X centered at the corresponding position. If the window size is larger than the available elements at the ends, the average is taken over the existing elements only.
% Initialize output vector
y = zeros(size(x));
% Loop over each element of x
for i = 1:length(x)
% Find the indices of the window
idx = i-n/2:i+n/2;
% Remove indices that are out of bounds
idx = idx(idx > 0 & idx <= length(x));
% Calculate the average of the window
y(i) = mean(x(idx));
end
end
To use this function, you need to save it in a file named mvavg.m in your current folder or MATLAB path. Then you can load the data file airtemp.mat and call the function on the variables air1, air2, and t. For example:
load airtemp.mat % Load the data
n = 4; % Number of data points per day
y1 = mvavg(air1,n); % Moving average of air1
y2 = mvavg(air2,n); % Moving average of air2
yt = mvavg(t,n); % Moving average of t
To plot the original data and the moving averages on one figure, you can use the plot function with different line styles and colors. For example:
plot(t,air1,'b-',yt,y1,'b--',t,air2,'r-',yt,y2,'r--') % Plot the data
xlabel('Time (days)') % Label the x-axis
ylabel('Temperature (°C)') % Label the y-axis
legend('air1','air1 (moving average)','air2','air2 (moving average)') % Add a legend
For question 2, you need to plot the temperature of the two stations over time using different colors depending on the daily change. To do this, you can use a loop to check the daily average temperature and assign a color to each day. Then you can use the scatter function to plot the data with markers. For example:
% Initialize color vectors
c1 = zeros(length(t),3); % Color vector for air1
c2 = zeros(length(t),3); % Color vector for air2
% Loop over each day
for i = 1:n:length(t)
% Find the indices of the day
idx = i:i+n-1;
% Remove indices that are out of bounds
idx = idx(idx <= length(t));
% Calculate the average temperature of the day
T1 = mean(air1(idx)); % Average temperature of air1
T2 = mean(air2(idx)); % Average temperature of air2
% Compare the average temperature with the previous day
if i > n % If not the first day
% Calculate the difference with the previous day
dT1 = T1 - mean(air1(i-n:i-1)); % Difference for air1
dT2 = T2 - mean(air2(i-n:i-1)); % Difference for air2
% Assign a color based on the difference
if dT1 > 1 % If air1 increased by more than 1 degree
c1(idx,:) = repmat([1 0 0],length(idx),1); % Red color
elseif dT1 < -1 % If air1 decreased by more than 1 degree
c1(idx,:) = repmat([0 0 1],length(idx),1); % Blue color
else % If air1 changed by no more than 1 degree
c1(idx,:) = repmat([0 0 0],length(idx),1); % Black color
end
if dT2 > 1 % If air2 increased by more than 1 degree
c2(idx,:) = repmat([1 0 0],length(idx),1); % Red color
elseif dT2 < -1 % If air2 decreased by more than 1 degree
c2(idx,:) = repmat([0 0 1],length(idx),1); % Blue color
else % If air2 changed by no more than 1 degree
c2(idx,:) = repmat([0 0 0],length(idx),1); % Black color
end
else % If the first day
% Assign black color
c1(idx,:) = repmat([0 0 0],length(idx),1); % Black color
c2(idx,:) = repmat([0 0 0],length(idx),1); % Black color
end
end
% Plot the data with markers and colors
scatter(t,air1,[],c1,'filled') % Plot air1
hold on % Hold the figure
scatter(t,air2,[],c2,'filled') % Plot air2
hold off % Release the figure
xlabel('Time (days)') % Label the x-axis
ylabel('Temperature (°C)') % Label the y-axis
legend('air1','air2') % Add a legend
The temperature patterns of the two sites are very different. The station in the South China Sea has a relatively stable and moderate temperature throughout the year, with small daily and seasonal variations. The station in the Sahara Desert has a large diurnal cycle, with high temperatures during the day and low temperatures at night. It also has a seasonal cycle, with higher temperatures in summer and lower temperatures in winter. The possible reasons for the differences are related to the different geographic and climatic conditions of the two sites. The South China Sea is a large body of water that has a high heat capacity and can moderate the temperature of the surrounding air. The Sahara Desert is a dry and arid land that has a low heat capacity and can heat up or cool down quickly. The South China Sea is also influenced by the monsoon winds that bring moisture and clouds, which can affect the radiation balance and the temperature. The Sahara Desert is under the influence of the subtropical high pressure, which prevents cloud formation and allows more solar radiation to reach the surface.
  1 Comment
Dyuman Joshi
Dyuman Joshi on 7 Nov 2023
"CHAT GPT write me this, but it doesn't work:"
ChatGPT is not a reliable (programming) resource.

Sign in to comment.


Image Analyst
Image Analyst on 7 Nov 2023
To learn other fundamental concepts, invest 2 hours of your time here:
To get a moving average you can use movmean

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!