Curve fitting with a nonlinear function by writing the equation in a linear form.
An experiment with an RC circuit is used for determining the capacitance of an unknown capacitor. In the circuit, shown on the right and in Fig. 6-8, a 5-M resistor is connected in series to the unknown capacitor C and a battery. The experiment starts by closing the switch and measuring the voltages, \nu_{R}, across the resistor every 2 seconds for 30 seconds. The data measured in the experiment is:
Theoretically, the voltage across the resistor as a function of time is given by the exponential function:
\nu_{R}=\nu e^{(-t /(R C))} (6.17)
Determine the capacitance of the capacitor by curve fitting the exponential function to the data.
It was shown in Fig. 6-9 that, as expected, an exponential function can fit the data well. The problem is solved by first determining the constants b and m in the exponential function \nu=b e^{m t} that give the best fit of the function to the data. This is done by changing the equation to have a linear form and then using linear least-squares regression.
The linear least-squares regression is applied by using the user-defined function LinearRegression that was developed in the solution of Example 6-1. The inputs to the function are the values t_{i} and \ln \left(\left(\nu_{r}\right)_{i}\right). Once b and m are known, the value of C is determined by equating the coefficients in the exponent of e:
\frac{-1}{R C}=m \text { solving for } C \text { gives: } C=\frac{-1}{R m} (6.18)
Table 6-2: Transforming nonlinear equations to linear form.
Nonlinear equation | Linear form | Relationship to Y=a_{1} X+a_{0} | Values for linear least squares regression | Plot where data points appear to fit a straight line |
y=b x^{m} | \ln (y)=m \ln (x)+\ln (b) | \begin{array}{l}Y=\ln (y), \quad X=\ln (x) \\ a_{1}=m, \quad a_{0}=\ln (b)\end{array} | \begin{array}{l}\ln \left(x_{i}\right) \text { and } \\ \ln \left(y_{i}\right)\end{array} | \begin{array}{l}y \text { vs. } x \text { plot on logarith } \\ \text { mic } y \text { and } x \text { axes. } \\ \ln (y) \text { vs. } \ln (x) \text { plot on } \\ \text { linear } x \text { and y axes. }\end{array} |
y=b e^{m x} | \ln (y)=m x+\ln (b) | \begin{array}{l}Y=\ln (y), \quad X=x \\ a_{1}=m, \quad a_{0}=\ln (b)\end{array} | \begin{array}{l}x_{i} \text { and } \\ \ln \left(y_{i}\right)\end{array} | \begin{array}{l}y \text { vs. } x \text { plot on logarith } \\ \text { mic } y \text { and linear } x \text { axes. } \\ \ln (y) \text { vs. } x \text { plot on lin } \\ \text { ear } x \text { and } y \text { axes. }\end{array} |
y=b 10^{m x} | \log (y)=m x+\log (b) | \begin{array}{l}Y=\log (y), \quad X=x \\ a_{1}=m, \quad a_{0}=\log (b)\end{array} | \begin{array}{l}x_{i} \text { and } \\ \log \left(y_{i}\right)\end{array} | \begin{array}{l}y \text { vs. } x \text { plot on logarith } \\ \text { mic } y \text { and linear } x \text { axes. } \\ \log (y) \text { vs. } x \text { plot on lin} \\ \text { ear } x \text { and } y \text { axes. }\end{array} |
y=\frac{1}{m x+b} | \frac{1}{y}=m x+b | \begin{array}{l}Y=\frac{1}{y}, \quad X=x \\ a_{1}=m, \quad a_{0}=b\end{array} | \begin{array}{l}x_{i} \text { and } \\ 1 / y_{i}\end{array} | \begin{array}{l}1 / y \text { vs. } x \text { plot on linear } \\ x \text { and } y \text { axes. }\end{array} |
y=\frac{m x}{b+x} | \frac{1}{y}=\frac{b}{m} \frac{1}{x}+\frac{1}{m} | \begin{array}{l}Y=\frac{1}{y}, \quad X=\frac{1}{x} \\ a_{1}=\frac{b}{m}, \quad a_{0}=\frac{1}{m}\end{array} | \begin{array}{l}1 / x_{i} \text { and } \\ 1 / y_{i}\end{array} | \begin{array}{l}1 / y \text { vs. } 1 / x \text { plot on } \\ \text { linear } x \text { and } y \text { axes. }\end{array} |
The calculations are done by executing the following MATLAB program (script file):
Program 6-2: Script file. Curve fitting with a nonlinear function
texp=2 : 2:30;
vexp=[9.7 8.1 6.6 5.1 4.4 3.7 2.8 2.4 2.0 1.6 1.4 1.1 0.85 0.69 0.6);
vexpLOG=log(vexp);
R=5E6;
[a1,a0]=LinearRegression(texp, vexpLOG)
b=exp(a0)
C=-1/(R*a1)
t=0:0.5:30;
v=b*exp(a1*t);
plot(t,v,texp,vexp,'ro')
Calculate ln(y_{i} of the data points (to be used in the linear regression.
Calculate coefficients a1 and a_{0} with the user-defined Calculate coefficients a_{1} and a_{0} with the user-defined
function LinearRegression in Example 6-1.
Calculate b, since a0 = ln(b) (see Table 6-2).
Calculate C using Eq. (6.18).
a1 ism in the equation \nu = be^{mt}
When the program is executed, the following values are displayed in the Command Window. In addition, the following plot of the data points and the curve-fitting function is displayed in the Figure Window (axes title were added interactively).
a1=
-0.1002
a0 =
2. 4776
b =
11. 9131
c =
1. 9968e-006