??? 03/27/04 03:33 Read: times |
#67483 - RE: Curve fitting Kai Responding to: ???'s previous message |
Dear Raghunathan,
I'm sorry if I had neglected too many details in my last reply! Nagarajan gave us the following calibration curve: ![]() This calibration curve seems to have been fabricated in the following manner: Apply a certain 'calibrated' weight, which is listed in the first column, named 'WT (X)', and determine resulting counts delivered by the electronics, which is listed in the third column, named 'ACTUAL CT (X)' If the calibration curve would not show any error, then we would get exactly those values, which are listed in the second column, named 'IDEAL CT'. But, of course, calibration curve 'ACTUAL CT (X)' is not identical with ideal curve 'IDEAL CT', and the difference between both curves, namely 'ACTUAL CT (X)' - 'IDEAL CT' is listed in the fourth column, named 'ERR CT (Y)'. Now, I assumed, that Nagarajan wishes to get a fit curve to 'ACTUAL CT (X)' as a function of 'WT (X)'. This fit curve I named 'WHOLE_curve _fit'. Because Nagarajan named his X-axis 'WT (X)', I called the data of the first column 'X'. Then, following a procedure I will explain some lines later, I got following fit curve for his calibration curve: WHOLE_curve-fit = 2001.9 * X -0.003167 * X^2 In the first reply I also wrote: Y = 2001.9 * X -0.003167 * X^2 to make the things simpler. So, if we now put certain X into this fit curve, we get a result, which is wished to be as close as possibble to the data listed in third column, named 'ACTUAL CT (X)'. An example: With X = 60, we get Y = 120103. According entry of table is '120114'. So, we get a deviation of 120103 - 120114 = -11. If we insert X = 360 we get Y = 720274, which corresponds to a deviation of 720274 - 720289 = -15. Inserting X = 540 yields Y = 1080103. Deviation now is 1080103 - 1080090 = 13. THEN, I noticed, that Nagarajan is NOT wanting to make a fit curve to whole calibration curve 'ACTUAL CT (X)' but only to the error term, which is listed in fourth column, named 'ERR CT (Y)'. But because the error term is just 'ERR CT (Y)' = 'ACTUAL CT (X)' - 'IDEAL CT', then by simply subtracting data of 'IDEAL CT'-column from WHOLE_curve_fit will deliver a suited fit curve to error curve 'ERR CT (Y)'. And because 'IDEAL CT' = 2000 * 'WT (X)' you finally get: ERROR_fit = WHOLE_curve_fit - IDEAL_curve = 2001.9 * X -0.003167 * X^2 - 2000 * X = 1.9 * X - 0.003167 * X^2 This fit curve to 'ERR CT (Y)' Nagarajan now can compare with his fit curves he fabricated and those of Joseph, because he never made a fit curve to 'ACTUAL CT (X)' but to 'ERR CT (Y)'! Let's insert the same examples as above: X = 60 -> ERROR_fit = 103. Deviation to corresponding data of 'ERR CT (Y)'-column, namely '114': 103 - 114 = -11 X = 360 -> ERROR_fit = 274. Deviation to corresponding data of 'ERR CT (Y)'-column, namely '289': 274 - 289 = -15 X = 540 -> ERROR_fit = 103. Deviation to corresponding data of 'ERR CT (Y)'-column, namely '90': 103 - 90 = 13. This shows, that the curve ERROR_fit = 1.9 * X - 0.003167 * X^2 can indeed be used as a fit curve to 'ERR CT (Y)'. ----------------------------------------------------------------------------- Now, how did I get this fit curve? Nagarajan stated, that he wants to fabricate a parabola fit curve, but he has trouble to get the right coefficients. His plot of 'ERR CT (Y)' (dark blue curve 'WT & ERR CT'), which I repeat here: ![]() shows, that parabola fit curve will indeed give rather good approximation, very probably. But how to get this parabola? First, we must think about the criteria our fit curve must satisfy. A mathematican will probably use least squares fit methode, also called methode of maximum likely hood. By this, you modify coefficients in order to minimize a certain error sum, which is calculated by taking the difference of actual curve and fit curve for each table entry, squaring it and finally taking the sum over all table entries. But this methode can show some unwished results, sometimes: 1. At X = 0 fit curve will not necessarily also be zero. But this is very unsatisfying. Think about a weighing application, where the display shows a non zero reading without any applied weight. This is hard to tolerate by the customer. 2. At X = 'fullscale' fit curve will not necessarily also produce 'fullscale'. This is also unsatisfying. 3. There's a certain probability of the existence of some 'deserters' of fit curve. Means, the very most data points coincide very well, but one or two show a much farer deviation. This can also be seen in the above plot ('FIT WT' curve). So, in typical measuring applications, we are interested in different criteria then a pur mathematican is interested in. We want a fit curve, which is not showing 'offset errors', 'gain errors' and 'deserters'. The methode I used is the following: WHOLE_curve-fit = 2000 * X + a0 + a1 * X + a2 * X^2 where a0 + a1 * X + a2 * X^2 represents the parabola term. Because offset error shall be eliminated, a0 = 0 is choosen. This gives WHOLE_curve_fit = 2000 * X + a1 * X + a2 * X^2 Now you can input two certain points, by the help of the plot above. First point is choosen in order to eliminate gain error: 0 = a1 * 600 + a2 * (600)^2 Second choosen point is the origin of parabola: X = 300, Y = 303 303 = a1 *300 + a2 * (300)^2 This gives a1 = 2.02 and a2 = -0.003366. But these coefficients are only starting points for a further going computer calculation, in order to achieve minimal deviations in combination with symmetry referring to sign (eliminating of 'deserters'). Of course, any other methode can be used to get these approximated coefficients a1 and a2. Fine evaluation of coefficients a1 and a2 is now done by the help of simple 'qbasic' programm, which slightly modifies a1 and a2 in order to find the least maximum deviation, while at the same time taking care, that maximum deviation with positive sign is equal to that with negative sign. This will eliminate annoying 'deserters'. By the way, in order to achieve elimination of gain error, a1 and a2 must not be changed independently. 0 = a1 * 600 + a2 * (600)^2 must always be valid. So, only a1 needs to be modified and a2 is automatically set to a2 = -a1 / 600. By this methode I got the final coefficients a1 = 1.9 and a2 = -0.003167 leading to a deviation from calibration curve of less than +-23. But nevertheless, if this deviation is higher than tolerable, then all the efforts were for nothing! Of course, X^3 term could be added, but this makes calculation not easier... If you have a look at the plot above again, then you will see, how idealy a piece wise linear interpolation would solve the problem! Yes, even a some coarser grid would result in deviations of less than about +-10! Piece wise interpolation is a very powerful tool, which is underestimated, very often. Kai |