matlabnoteschap06
matlabnoteschap06
Regression
• Engineers take experimentally determined data and attempt
to fit curves to it for analysis.
𝑝 = polyfit(𝑥, 𝑦, 1)
Power-Law Functions: 𝑦 𝑥 = 𝑏𝑥 𝑚
𝑝 = polyfit(log10(𝑥), log10(𝑦),1)
𝑤 𝑧 = 10𝑝(2) 𝑧 𝑝(1)
t = 0:0.5:5.0;
w = [6 4.83 3.7 3.15 2.41 1.83 1.49 1.21 0.96 0.73 0.64];
% Linear Fit
figure
plot( t, w,'-o'), xlabel('t'),ylabel('w (Rectilinear Plot)')
Function Discovery
This plot shows that the data is not a Linear Function.
Function Discovery
Now plot the data using log-log coordinates, as shown below.
% Power-Law Fit
figure
loglog( t, w,'-o'), xlabel('t'),ylabel('w (Log-Log Plot)')
Function Discovery
This plot shows that the data is not a Power-Law Function
because it is not linear on log-log coordinates.
Function Discovery
Now plot the data using semi-log y coordinates, as shown
below.
% Exponential Fit
figure
semilogy( t, w,'-o'), xlabel('t'),ylabel('w (Semi-Log Plot)')
Function Discovery
This plot shows that the data is an Exponential Function
because it is linear on semi-log y coordinates.
Function Discovery
Now use the polyfit command to construct an Exponential
Function that can be used to approximate the original data.
Plot the original data and the curve-fit model on the same
graph. Use this model to estimate the value of w at t = 0.25:
% Exponential Fit
p = polyfit(t, log10(w),1); % generates coefficients for curve fit
t2 = linspace(0,5,100); % generates a new t vector for curve fit
w2 = 10^(p(2))*10.^(p(1)*t2); % generates new w vector using t2
% Estimate w at t = 0.25:
t_025 = 0.25;
w_025 = 10^(p(2))*10.^(p(1)*t_025)
figure
plot(t,w,'o',t2,w2,t_025,w_025), xlabel('t'),ylabel('w (Exponential Fit)')
legend('Original Data', 'Curve Fit', ‘w @ t = 2.5 s' )
Function Discovery
w_025 = 5.3410
Regression
The Least-Squares Method minimizes the vertical differences
(Residuals) between the data points and the predictive
equation. This gives the line that best fits the data. For a linear
curve (First Order) fit:
𝑛
𝐽= 𝑚𝑥𝑖 + 𝑏 − 𝑦𝑖 2
𝑖=1
𝑦 𝑥 = 𝑚𝑥 + 𝑏
Regression
Regression
The curve fit can be improved by increasing the order of the
polynomial. Increasing the degree of the polynomial increases
the number of coefficients:
• First Degree: 𝑦 𝑥 = 𝑎1 𝑥 + 𝑎0
• Second Degree: 𝑦 𝑥 = 𝑎2 𝑥 2 + 𝑎1 𝑥 + 𝑎0
• Third Degree: 𝑦 𝑥 = 𝑎3 𝑥 3 + 𝑎2 𝑥 2 + 𝑎1 𝑥 + 𝑎0
• Fourth Degree: 𝑦 𝑥 = 𝑎4 𝑥 4 + 𝑎3 𝑥 3 + 𝑎2 𝑥 2 + 𝑎1 𝑥 + 𝑎0
Regression
Regression
Having a very high-order polynomial doesn’t necessarily mean
a better fit. The objective is to be able to use the equation to
predict values between the data points.
Basic Fitting Interface
Use the previously developed Script File to use the Basic
Fitting Interface.
Basic Fitting Interface
Use the Tools Drop-Down Menu and go to Basic Fitting.
Basic Fitting Interface
Use the Tools Drop-Down Menu and go to Basic Fitting.
Basic Fitting Interface
Check the boxes indicated below. Change the number of
Significant Digits to 5.
Basic Fitting Interface
The Residuals Plot is shown below. The norm of the residuals
is a measure of the “Goodness of Fit.” A smaller value is
preferable.
Problem 6.1:
Problem 6.1:
Problem 6.5:
Problem 6.5:
Problem 6.10:
The following data give the stopping distance d as a function of the
initial speed v, for a certain car model. Using the polyfit command, find
a third-order polynomial that fits the data. Show the original data and the
curve fit on a plot. Using the curve fit, estimate the stopping distance for
an initial speed of 63 mi/hr.
Problem 6.10:
Problem 6.13:
Data on the vapor pressure P of water as a function of temperature T are
given in the following table. From theory we know that ln 𝑃 is
proportional to 1/T. Obtain a curve fit for P(T) from these data using the
polyfit command. Use the fit to estimate the vapor pressure at T = 285 K.
Problem 6.13:
Problem 6.13:
Problem 6.13: