Discerning and coding: the exponential equation process

Most physical processes in nature have exponential properties. Radiation half-life is one example. Thermal transitions from hot to cold (or cold to hot, for that matter) are another.
Often, a set of data from some process can be a little noisy or otherwise messy. However, if the data points look exponential, the underlying process is probably exponential, too, and it’s fairly easy to find an equation to describe that exponential behavior, enabling us to go from the mess of Figure 1 to Figure 2’s more orderly outcome.

Figure 1 This hypothetical data seems rather messy.

Figure 2 Intuiting order from seeming chaos enables an exponential curve fit outcome.
Assuming an equation of the form Y = A1 + A2 * exp ( – x / A3 ), we have three unknowns, A1, A2 and A3, for which we take three x-and-y pairs from our data and solve three equations in three unknowns to obtain the associated coefficients.
The resulting equation may not be the mathematical best fit, but it can come pretty darn close. The following example algorithm serves us well in this regard (Figure 3).

Figure 3 An exponential curve algorithm enables determination of equation coefficients.
It may be that someone out there is smart enough to find the value of A3 by a one-step explicit method, but I couldn’t do it. Therefore, the value of A3 is found here using an iterative loop. Then with A3 in hand, the values of A2 and A1 are one-step next-step finds.
The code that I called “expestim.bas” was written a long time ago in GWBASIC. After you stop laughing, please take a look at it. And please also be kind in the comments.
10 CLS:SCREEN 9:COLOR 15,1
20 PRINT "save "+CHR$(34)+"expestim.bas"+CHR$(34):PRINT
30 PRINT "save "+CHR$(34)+"a:expestim.bas"+CHR$(34):PRINT:PRINT
40 PRINT "This program solves for the coefficients A1, A2 and A3 of the"
50 PRINT "expression y = A1 + A2 * exp (- x/ A3 ) based on actual data.":PRINT
60 PRINT "You are first asked for three values of the independent variable X."
70 INPUT "What are the three X-values ? ",X1,X2,X3:PRINT
80 PRINT "Next, you are asked for the three corresponding values of the";
90 PRINT " variable Y.":INPUT "What are the three Y-values ? ",Y1,Y2,Y3:PRINT
100 A3=10:A3MOD=.1:K=(Y1-Y2)/(Y1-Y3)
110 L=(EXP(-X1/A3)-EXP(-X2/A3))/(EXP(-X1/A3)-EXP(-X3/A3))
120 IF K>L THEN A3=A3/(1+A3MOD)
130 IF KA3MOD THEN GOTO 110
150 IF ABS(K-L)/ABS(K)<.01 THEN GOTO 170
160 A3MOD=A3MOD*.1:GOTO 110
170 A2=(Y1-Y3)/(EXP(-X1/A3)-EXP(-X3/A3)):A1=Y1-A2*EXP(-X1/A3)
180 PRINT "Y =";A1;"+";A2;"* exp (-X/";A3;")":PRINT
190 Z1=A1+A2*EXP(-X1/A3):Z2=A1+A2*EXP(-X2/A3):Z3=A1+A2*EXP(-X3/A3)
200 A$="SPECIFIED Y-VALUES WERE : ###.## ###.## ###.##":PRINT USING A$;Y1,Y2,Y3
210 A$="CALCULATED Y-VALUES WERE : ###.## ###.## ###.##":PRINT USING A$;Z1,Z2,Z3
220 PRINT:A$=" ":INPUT "Do you want y-value for another x-value (y/n) ? ",A$
230 IF A$="N" THEN GOTO 40
240 IF A$="n" THEN GOTO 40
250 IF A$="Y" THEN GOTO 280
260 IF A$="y" THEN GOTO 280
270 GOTO 220
280 INPUT "What is the new x-value ? ",X
290 Y=A1+A2*EXP(-X/A3):PRINT "For x =";X;", y =";Y:GOTO 220
John Dunn is an electronics consultant and a graduate of The Polytechnic Institute of Brooklyn (BSEE) and of New York University (MSEE).
Related Content
The post Discerning and coding: the exponential equation process appeared first on EDN.


