Matlab Fundamental 13
Matlab Fundamental 13
In this chapter, you will perform common data analysis tasks such as
Computing trailing averages and other moving window operations
Finding linear correlations between data sets
Fitting and evaluating polynomial models
Introduction: Moving The electricity usage data shows both a long-term trend and a short-term
Window Operations seasonality. In such a situation it is common to calculate summary statistics, such
the mean, on a moving subset of the data.
You can use This code creates and plots two vectors.
the movmean function to calculate x = 0:0.2:6
the centered k-point moving y = sin(x)
average of an array. plot(x,y,'o-')
TASK hold on
Plot ym9 as a function of x with plot(x,ym9,'.-')
point markers and a solid line. Add hold off
the plot to the same figure
using holdon and hold off.
Notice the change in the shape of the curve of the moving movmin Moving minimum
average near the ends of the data. A 17-point window needs
8 points on either side of the current point. For the first and movmax Moving maximum
last 8 data points, the movmean function by default shrinks
the window as needed. You can change this behavior by movsum Moving sum
specifying the optional 'Endpoints' parameter. Try filling the
movmean Moving mean
window with zeros or simply discarding any values too close
to the endpoints to fit the entire 17-point window. movmedia Moving median
n
You may also want to try applying other statistical
functions such the minimum or maximum: movstd Moving standard deviation
movvar Moving variance
When you are finished, you may move on to the next section.
You can find a moving average using a noncentered window. This code creates and plots two vectors.
x = 0:0.2:6
dN = movmean(d,[kb,kf]) y = sin(x)
plot(x,y,'o-')
kb is the number of trailing points to include, and kf is the
number of leading points. trail4max = movmax(y,[3 0])
TASK
Create a vector named trail4max that contains the trailing 4-
point maximum of y (i.e., the maximum of the current point
and the three previous points).
TASK hold on
Plot trail4max as a function of x with point markers and a plot(x,trail4max,'.-')
solid line. hold off
yyaxis left
plot(t,y1)
yyaxis right
plot(t,y2)
Similarly,
yyaxis right
makes the axis on the right active again.
yyaxis right
ylabel('y_2')
ylim([0 600])
xlabel('x')
yyaxis left
plot(x1,y1)
yyaxis right
plot(x2,y2)
yyaxis left
plot(dates,usagesmooth(:,1:3)) -> hoàn tòan có thể viết theo kiểu
matrix
yyaxis right
plot(dates,usagesmooth(:,4))
plotmatrix
Creates separate scatter plots for each pair of
columns in the input matrix.
Correlation Coefficient
In addition to visualizing the relationship between the variables, you can quantify the strength of the linear relationship
numerically by calculating the correlation coefficient.
The MATLAB function corrcoef computes the linear correlation using the data from the input matrix. The correlation
coefficient has a value between +1 and -1.
A correlation of +1 or -1 indicates a perfect linear relationship between the variables.
+1 means that an increase in one variable is associated with an increase in the other.
-1 means that an increase in one variable is associated with a decrease in the other.
A correlation of 0 indicates that the variables are not linearly related.
corrcoef
Compute the correlation coefficients from the input matrix.
Quiz Corrcoef([a,b,c])
Given three column vectors a, b, and c, which of
the following commands can be used to find the
coefficient of correlation between the three
vectors?
correlation([a b c])
corrcoef([a b c])
corrcoef(a,b,c)
TASK This code import, organizes, and plots the usage data.
Use plotmatrix to create a matrix of plots of all
edata = readtable('electricity.csv');
the sectors (columns) in usage against each
other. dates = edata.Date;
usage = edata{:,2:end};
Use corrcoef to quantify these correlations by sectors = edata.Properties.VariableNames(2:end);
calculating the corresponding correlation plot(dates,usage)
coefficients. Store the result in a matrix
legend(sectors,'Location','northwest')
called usagecorr
plotmatrix(usage)
usagecorr = corrcoef(usage)
Summary: Linear Correlation
You can investigate relationships between variables visually and computationally:
Plot multiple series together. Use yyaxis to add another vertical axis to allow for different scales.
Plot variables against each other. Use plotmatrix to create an array of scatter plots.
Calculate linear correlation coefficients. Use corrcoef to calculate pairwise correlations.
>> c = polyfit(x,y,n)
Suppose that you have two vectors x and y.
x = 0:5;
y = [2 1 4 4 3 2];
plot(x,y)
z = polyfit(x,y,3)
13.4 Polynomial Fitting: (4/7) Fit a Line
cf = polyfit(xData,yData,n)
TASK
Fit a first degree polynomial in terms of x to the
vector y. Use the function polyfit and store the
resulting coefficients in a vector named c.
yEval = polyval(cf,xEval)
TASK
Now, use the function polyval to find the value of
the fitted polynomial at each of the x values. Store
the result in yFit.
TASK hold on
Plot the polynomial values yFit against x as a red plot(x,yFit,'r')
line on top of the existing graph. hold off
c = polyfit(yr,penguins,3)
13.4 Polynomial Fitting: (5/7) Centering and Scaling
You can avoid the numerical precision limitations by centering and scaling [c,~,sc] =
the x data when using polyfit and polyval. To do this, ask for a third output polyfit(yr,penguins,3)
from polyfit:
[c,~,sc] = polyfit(x,y,deg)
TASK
Use centering and scaling to fit a third degree polynomial to the
vector penguins as a function of yr. Store the polynomial coefficients in a vector
named c and the scaling information in a variable called sc.
When evaluating the polynomial, pass the vector of scaling penguinfit = polyval(c,yr,[],sc)
coefficients sc to polyval as a fourth input:
yFit = polyval(c,xFit,[],sc)
TASK
Use polyval to evaluate the fitted polynomial at each of the yr values. Store the
result in penguinfit.
Electricity Usage
The datetime vector dates contains the months for his code imports, organizes, and plots the usage data.
which the electricity usage is recorded. To perform edata = readtable('electricity.csv');
polynomial fitting, you must first convert the dates dates = edata.Date;
to elapsed times. residential = edata.Residential;
TASK plot(dates,residential,'.-')
Create a vector t that contains, for each date, the
number of days elapsed since the first data point. t = days(dates - dates(1))
TASK plotmatrix(numdata)
Create a matrix of the scatter plots of the variables
cc = corrcoef(numdata)
in numdata (weight, horsepower, and fuel economy) in a
single figure.
TASK p = polyfit(wt,econ,1)
Determine the best-fit line (i.e., a first degree polynomial
econFit = polyval(p,wt)
fit) for fuel economy (in L/100km) as a function of vehicle
weight.
Note that you do not need to use centering and scaling for
the fit.
TASK scatter(wt,econ)
Create a scatter plot of fuel economy against weight, and
hold on
add the best-fit line as a red line.
plot(wt,econFit,'r')
hold off