You are on page 1of 20

Curve fitting-parameter

estimation

Dr. P Swapna Reddy


Asst. Professor
NIT Calicut, Kerala
Introduction

1) Interpolation (connect the data-dots)


o If data is reliable, we can plot it and connect the dots
o This is piece-wise, linear interpolation
o This has limited use as a general function f(x)
o Since its really a group of small f(x)s, connecting one point to the
next it doesn’t work very well for data that has built in random error
(scatter)

2
Condt….

2) Curve fitting - capturing the trend in the data by assigning a single


function across the entire range.
The example below uses a straight line function

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

Our fit is a straight line, so now substitute


f(x)=ax+b

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

put these into matrix form

7
SOLUTION FOR THE EQUATION OF A LINE
USING MATRIX-VECTOR NOTATION
The model prediction of each dependent variable can be written as

𝑦ො = 𝑎𝑥𝑖 + 𝑏

which can be written in matrix-vector form, for N data points, as:


𝑦
ෞ1 𝑥1 1
𝑦
ෞ2 𝑥2 1 𝑎
=
⋮ ⋮ ⋮ 𝑏
𝑦ෞ𝑁 𝑥𝑁 1

• Using compact Matrix-vector notation


෡ = 𝜑𝜃
𝒀

8
Condt….
The objective function is:
𝑁
2
𝑓 𝜃 = ෍ 𝑦𝑖 − 𝑦ො𝑖
𝑖=1

which can be written as:


෡ ]𝑇 [𝒀 − 𝒀
𝑓 𝜃 = [𝒀 − 𝒀 ෡]

The general optimization statement is then:


Minimize ෡ ]𝑇 [𝒀 − 𝒀
𝑓 𝜃 = [𝒀 − 𝒀 ෡]

Subject to ෡ = 𝜑𝜃
𝒀
• Using compact Matrix-vector notation

𝜃 = 𝜑𝑇 𝜑 −1 𝜑 𝑇 𝒀
9
Polynomial curve fitting

o Consider the general form for a polynomial of order j

(1)

10
Polynomial curve fitting

Error - Least squares approach


The general expression for any error using the least squares approach
is

where we want to minimize this error. Now substitute the form of our
eq. (1)

into the general least squares error eq. (2)

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

where all summations above are over i=1,….n

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

Two extreme cases are


• Sr = 0 → r=1 describes a perfect fit (straight line passing through all points).
• Sr = St → r=0 describes a case with no improvement.

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

• Let y = p (1)t + p (2)

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)

You might also like