Professional Documents
Culture Documents
estimation
2
Condt….
3
Linear curve fitting (linear regression)
Given the general form of a straight line f(x) =
ax + b
o How can we pick the coefficients that best
fits the line to the data?
o First question: What makes a particular
straight line a ‘good’ fit?
o Why does the blue line appear to us to fit
the trend better?
• Consider the distance between the data and
points on the line
• Add up the length of all the red and blue
verticle lines
• This is an expression of the ‘error’ between
data and fitted line
• The one line that provides a minimum error is
then the ‘best’ straight line
4
Quantifying error in a curve fit
5
Condt….
o The ‘best’ line has minimum error between line and data points
o This is called the least squares approach, since we minimize the
square of the error.
Finding the minimum of a function
1) derivative describes the slope
2) slope = zero is a minimum
6
Condt….
o Solve for the a and b so that the previous two equations both = 0
7
SOLUTION FOR THE EQUATION OF A LINE
USING MATRIX-VECTOR NOTATION
The model prediction of each dependent variable can be written as
𝑦ො = 𝑎𝑥𝑖 + 𝑏
8
Condt….
The objective function is:
𝑁
2
𝑓 𝜃 = 𝑦𝑖 − 𝑦ො𝑖
𝑖=1
Subject to = 𝜑𝜃
𝒀
• Using compact Matrix-vector notation
𝜃 = 𝜑𝑇 𝜑 −1 𝜑 𝑇 𝒀
9
Polynomial curve fitting
(1)
10
Polynomial curve fitting
where we want to minimize this error. Now substitute the form of our
eq. (1)
11
Polynomial curve fitting
where: n=data points given, i=the current data point being summed,
j=the polynomial order
12
Polynomial curve fitting
To minimize error, take the derivative with respect to each
coefficient a0, ak k=1,…j set each to zero
13
Polynomial curve fitting
Matrix form
14
How good is the best line?
15
Condt….
The improvement obtained by using a regression line instead of the mean
gives a measure of how good the regression fit is.
1. coefficient of determination
2. correlation coefficient
Usually an r value close to 1 represents a good fit. But be careful and always
plot the data points and the regression line together to see what is going on.
16
Matlab Routines
• The MATLAB routine polyfit is used to fit data to an nth order polynomial,
and the routine polyval is used to evaluate an nth order polynomial.
• Let x=independent variable vector, y=dependent variable vector, and n=
order of polynomial.
• The best-fit polynomial coefficients are found from:
p = polyfit ( x, y, n)
• where the elements of the p vector are ordered from the highest power
on down.
• Now, we wish to compare the experimental data with the best fit lin
(model). The line is generated using the polyval function
Given a polynomial p and an independent vector x1, the resulting
dependent vector y1 can be found from:
y1 = polyval ( p, x1 )
17
Batch Reactor example
• Consider a batch reactor with a single first-order reaction, 𝐴 → 𝐵. The
model is:
dC A
= − kC A
dt
where CA = concentration of A, k=rate constant, and t=time.
• Separating variables and integrating:
ln C A = ln C A0 − kt
y = ln C A
• where p (1) = −kt
p (2) = ln C A0 18
Batch Reactor example
• The batch reactor data are shown in
Time (min) 0 1 2 3 4 5
CA, kgmol/m3 8.47 5.0 2.95 1.82 1.05 0.71
>>t=0:1:5;
>>t =t';
>>CA = [8.47;5.00;2.95;1.82;1.05;0.711];
>>y =log (CA) ;
>> p=polyfit(t,y,1)
• p=-0.5017, 2.1098
19
Condt….
• Now, we wish to compare the experimental data with the best fit line
(model).
• The line is generated using the polyval function.
• The model and experirnent are compared in Figure
>> ymod = polyval(p,t);
>> plot(t,ymod, t, y, ‘o')
2.5
1.5
ln Ca
0.5
-0.5
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 20
Time (min)