0% found this document useful (0 votes)
3 views

matlabnoteschap06

Uploaded by

Marwa AlFaouri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

matlabnoteschap06

Uploaded by

Marwa AlFaouri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Chapter 6: Model Building and

Regression
• Engineers take experimentally determined data and attempt
to fit curves to it for analysis.

• Linear: 𝑦 𝑥 = 𝑚𝑥 + 𝑏 (𝑚 = slope, 𝑏 = 𝑦-intercept)


• Power: 𝑦 𝑥 = 𝑏𝑥 𝑚
• Exponential: 𝑦 𝑥 = 𝑏(10)𝑚𝑥 or 𝑦 𝑥 = 𝑏𝑒 𝑚𝑥 where
𝑒 is the base of the natural logarithm ln 𝑒 = 1

• Regression uses the Least-Squares Method to find an


equation that best fits the given data.
Linear Function: 𝑦 𝑥 = 𝑚𝑥 + 𝑏
𝑚
Power Function: 𝑦 𝑥 = 𝑏𝑥
Exponential Function:
𝑚𝑥
𝑦 𝑥 = 𝑏(10)
Function Discovery
Linear Functions: Linear on rectilinear plot (x,y)
Power-Law Functions: Linear on log-log plot (log10 x, log10
y)
Exponential Functions: Linear on semi-log y plot (x, log10 y)

Once the function type is determined, use the polyfit function


to determine the curve fit equation.

For an original data set (x,y), the polyfit function returns


coefficients for the linear curve fit model 𝑤 = 𝑝1 𝑧 + 𝑝2

𝑝 = polyfit(𝑥, 𝑦, 1)

where 𝑝1 = 𝑝 1 𝑎𝑛𝑑 𝑝2 = 𝑝(2)


Function Discovery
Linear Functions:
𝑦 𝑥 = 𝑚𝑥 + 𝑏 (𝑚 = slope, 𝑏 = 𝑦-intercept)
𝑝 = polyfit(𝑥, 𝑦, 1)
𝑤 𝑧 = 𝑝 1 𝑧 + 𝑝(2)

Power-Law Functions: 𝑦 𝑥 = 𝑏𝑥 𝑚
𝑝 = polyfit(log10(𝑥), log10(𝑦),1)
𝑤 𝑧 = 10𝑝(2) 𝑧 𝑝(1)

Exponential Functions: 𝑦 𝑥 = 𝑏(10)𝑚𝑥


𝑝 = polyfit(𝑥, log10(𝑦),1)
𝑤 𝑧 = 10𝑝 2 (10)𝑝 1 𝑧
Function Discovery
Open a new MATLAB Script file. Type in the following data:
t 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
w 6.00 4.83 3.70 3.15 2.41 1.83 1.49 1.21 0.96 0.73 0.64

Plot the data using rectilinear coordinates, as shown below.

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

where the equation of a straight line is

𝑦 𝑥 = 𝑚𝑥 + 𝑏
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:

You might also like