Nothing Special   »   [go: up one dir, main page]

Linear Regression Fitting of A Nonlinear Expression (Example 1)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 6

Linear Regression Fitting of a

Nonlinear Expression (Example 1)

Consider the first order rate expression for a chemical


reaction:
r  kC A

For a constant volume batch reactor the mass balance is:

dCA
  kC A
n

dt
Rearranging the expression and then integrating from
an initial concentration of CA0 to a final concentration CA
gives:

CA
dCA t


CA0 CA
   kdt
0

CA
ln  kt or C A  C A 0e  kt

C A0
Based on the data below find the rate constant k for the first order reaction:

CA (gmole/liter) Time (sec)


0.100 0
0.0892 500
0.0776 1000
0.0705 1500
0.0603 2000
0.0542 2500
0.0471 3000
Ca=[0.1; 0.0892; 0.0776; 0.0705; 0.0603; 0.0542; 0.0471];
t=0:500:3000
t=t'
n=7; %data points
y=log(Ca./Ca(1));%transform y data
x=t

a1=(n*sum(x.*y)-sum(x)*sum(y))/(n*sum(x.^2)-sum(x)^2);
a0=sum(y)/n-a1*sum(x)/n;

fprintf('\nThe slope of the regression line equals %.6f\n',a1);


fprintf('\nThe intercept of the regression line equals %.6f\n',a0);
fprintf('\nThus k equals %.7f\n',-a1);
S=sum((a0+a1.*x-y).^2);
stderror=sqrt(S/(n-2));
fprintf('\nThe standard error of the regression line is %.6f\n',stderror);

Sb=sum((y-sum(y)/n).^2);
r=sqrt((Sb-S)/Sb);
fprintf('\nThe correlation coefficient of the regression line equals %.6f\n\n',r);
We could also solve example using polyfit

Ca=[0.1; 0.0892; 0.0776; 0.0705; 0.0603; 0.0542; 0.0471];


t=0:500:3000
t=t'
n=7; %data points
y=log(Ca./Ca(1));%transform y data
x=t
fprintf('\nComparing the results with the use of the polyfit function \n');

coeff=polyfit(x, y, 1);
fprintf('\nThe slope of the regression line equals %.6f\n',coeff(1));
fprintf('\nThe intercept of the regression line equals %.6f\n',coeff(2));
fprintf('\nThus k equals %.7f\n',-coeff(1));

You might also like