MATLAB Answers

What function fits my data...?

2 views (last 30 days)
William Thielicke
William Thielicke on 18 Oct 2019
Commented: Alex Sha on 19 Oct 2019
Hello, I am a bit unsure if this is the correct place to ask my question, but I'll try anyways:
I am doing laser triangulation, and I recorded the position of a laser point on the camera CMOS chip vs. a displacement.
I have a series of measurement points. y is the distance in mm from the camera (with an unknown offset), and x is the position of the laser point on the camera sensor (in pixels).
y=[0 5.97623500000000 11.9524700000000 17.9287050000000 23.9049400000000 29.8811750000000 35.8574100000000 41.8336450000000 47.8098800000000 53.7861150000000 59.7623500000000 65.7385850000000 71.7148200000000 77.6910550000000 83.6672900000000 89.6435250000000 95.6197600000000 101.595995000000 107.572230000000 113.548465000000 119.524700000000 125.500935000000 131.477170000000 137.453405000000 143.429640000000 149.405875000000 155.382110000000 161.358345000000 167.334580000000 173.310815000000 179.287050000000 185.263285000000 191.239520000000 197.215755000000 203.191990000000 209.168225000000 215.144460000000 221.120695000000 227.096930000000 233.073165000000 239.049400000000 245.025635000000 251.001870000000 256.978105000000 262.954340000000 268.930575000000 274.906810000000 280.883045000000 286.859280000000 292.835515000000 298.811750000000 304.787985000000 310.764220000000 316.740455000000 322.716690000000 328.692925000000 334.669160000000 340.645395000000 346.621630000000 352.597865000000 358.574100000000 364.550335000000 370.526570000000 376.502805000000 382.479040000000 388.455275000000 394.431510000000 400.407745000000 406.383980000000 412.360215000000 418.336450000000 424.312685000000 430.288920000000 436.265155000000 442.241390000000 448.217625000000 454.193860000000 460.170095000000 466.146330000000 472.122565000000 478.098800000000 484.075035000000 490.051270000000 496.027505000000 502.003740000000 507.979975000000 513.956210000000 519.932445000000 525.908680000000 531.884915000000 537.861150000000 543.837385000000 549.813620000000 555.789855000000 561.766090000000 567.742325000000 573.718560000000 579.694795000000 585.671030000000 591.647265000000 597.623500000000 603.599735000000 609.575970000000 615.552205000000 621.528440000000 627.504675000000 633.480910000000 639.457145000000 645.433380000000 651.409615000000 657.385850000000 663.362085000000 669.338320000000 675.314555000000 681.290790000000 687.267025000000 693.243260000000 699.219495000000 705.195730000000 711.171965000000 717.148200000000 723.124435000000 729.100670000000 735.076905000000 741.053140000000 747.029375000000 753.005610000000 758.981845000000 764.958080000000 770.934315000000 776.910550000000 782.886785000000 788.863020000000 794.839255000000 800.815490000000 806.791725000000 812.767960000000 818.744195000000 824.720430000000 830.696665000000 836.672900000000 842.649135000000 848.625370000000 854.601605000000 860.577840000000 866.554075000000 872.530310000000 878.506545000000 884.482780000000 890.459015000000 896.435250000000 902.411485000000 908.387720000000 914.363955000000 920.340190000000 926.316425000000 932.292660000000 938.268895000000 944.245130000000 950.221365000000 956.197600000000 962.173835000000 968.150070000000 974.126305000000 980.102540000000 986.078775000000 992.055010000000 998.031245000000 1004.00748000000 1009.98371500000 1015.95995000000 1021.93618500000 1027.91242000000 1033.88865500000 1039.86489000000 1045.84112500000 1051.81736000000 1057.79359500000 1063.76983000000 1069.74606500000 1075.72230000000 1081.69853500000 1087.67477000000 1093.65100500000 1099.62724000000 1105.60347500000 1111.57971000000 1117.55594500000 1123.53218000000 1129.50841500000 1135.48465000000 1141.46088500000 1147.43712000000 1153.41335500000 1159.38959000000 1165.36582500000 1171.34206000000 1177.31829500000 1183.29453000000 1189.27076500000 1195.24700000000];
x=[599.879427792916 669.403805496829 734.507982583454 793.957914338920 852.338173481085 906.812156862745 962.431716265797 1014.30095873281 1064.53915662651 1114.12381382738 1159.26240808824 1202.22931442080 1244.72289742343 1284.70789865872 1322.19885177453 1361.76739245884 1399.00976138829 1434.70044543430 1468.93040501997 1502.48020954598 1535.47037484885 1566.52059004302 1596.13613550816 1624.76059050064 1652.78768020970 1680.14513981358 1705.64774281806 1731.69461914745 1757.35486160397 1782.05118961788 1805.83234859675 1828.90676691729 1851.14908256881 1873.04921875000 1894.24110671937 1915.63857374392 1935.67927631579 1955.28571428571 1974.13945578231 1992.89205526770 2010.82620087336 2028.57193605684 2046.45855855856 2063.26118721461 2079.57953488372 2095.16353383459 2111.10516252390 2126.29990262902 2141.41878669276 2156.40556660040 2171.01210898083 2185.42959183673 2199.31714876033 2212.54214360042 2225.86933614331 2238.86552828175 2251.58817204301 2264.08487486398 2276.11378555799 2288.05016722408 2300.15919282511 2311.73356009070 2322.75834292290 2334.04608294931 2344.63647058824 2355.32621589561 2365.45748502994 2375.81961259080 2385.68466257669 2395.58478802993 2405.51194968553 2415.19211195929 2424.50709677419 2433.85039370079 2442.55978975033 2451.94101876676 2460.32479784367 2469.04377564980 2477.02200825310 2485.08495821727 2493.23809523810 2501.61276595745 2509.21325648415 2516.91159420290 2524.55964653903 2532.43387815750 2539.51274362819 2547.00455235205 2554.03703703704 2561.36939721793 2568.30685358255 2575.32176656151 2581.71860095390 2588.80317460317 2595.33386327504 2601.73003194888 2608.33006535948 2614.38398692810 2620.22795341098 2626.29950083195 2632.18686868687 2638.30034129693 2644.08233276158 2650.09230769231 2655.41166380789 2661.23529411765 2666.65391304348 2672.29100529101 2677.79322638146 2683.43963963964 2688.31588447653 2693.77396021700 2699.09854014599 2703.78727272727 2708.82935779817 2713.87316176471 2718.63401109057 2723.46728971963 2727.65485074627 2732.76603773585 2737.45419847328 2741.57442748092 2746.32882011605 2750.87719298246 2755.07677165354 2759.44356435644 2763.74749498998 2767.91919191919 2772.09572301426 2775.99168399168 2780.33333333333 2784.10351966874 2788.31524008351 2792.32008368201 2796.25847457627 2800.12903225806 2803.79175704989 2807.75599128540 2811.42825607064 2815.39606126915 2818.85176991150 2822.74610244989 2826.41610738255 2829.72045454545 2833.31292517007 2836.69107551487 2839.91685912240 2843.21658986175 2846.66744730679 2849.82159624413 2853.04716981132 2856.49408983452 2859.42553191489 2862.79616306954 2865.72901678657 2868.73557692308 2872.10436893204 2874.89603960396 2877.98756218905 2881.00997506234 2883.96491228070 2887.29591836735 2889.96212121212 2892.77577319588 2895.44559585492 2898.41361256545 2901.01827676240 2903.77248677249 2906.46010638298 2909.29946524064 2911.84239130435 2914.61788617886 2916.79781420765 2919.80874316940 2922.29972752044 2924.94535519126 2927.51366120219 2929.94505494506 2932.45257452575 2935.03814713896 2937.62534435262 2939.83888888889 2942.13407821229 2944.81232492997 2947.33519553073 2949.63352272727 2952.15492957747 2954.37005649718 2956.65168539326 2959.01424501425 2961.13960113960 2963.19428571429 2965.62500000000 2967.67638483965 2969.80289855072 2971.62170087977 2974.12865497076 2976.33333333333 2978.15339233038 2980.20238095238 2982.32748538012];
This is the calibration measurement, I need to find a suitable function for this dataset, because later, I need to extrapolate the data. I have successfully tried smoothing splines (in Python, because I don't have the Curve fitting toolbox), and they work ok for the above dataset. But this dataset is only a simulation, the real experiment will have more noise and smoothing splines might fail.
Does anyone have a tip for me....? Thanks!

  2 Comments

Baium
Baium on 18 Oct 2019
What about polyfit/polyder/polyval function set?
William Thielicke
William Thielicke on 18 Oct 2019
That gives some results, but they are worse than the smoothing spline in python. I am looking for a "physical function" that correctly explains my simulation, but I don't have a good idea :(

Sign in to comment.

Accepted Answer

Alex Sha
Alex Sha on 19 Oct 2019
The follow function is much simple, but more better:
y = x/(p1+p2*x)+p3
Root of Mean Square Error (RMSE): 0.313525722040886
Sum of Squared Residual: 19.757974054633
Correlation Coef. (R): 0.999999591246961
R-Square: 0.999999182494088
Adjusted R-Square: 0.999999174236453
Determination Coef. (DC): 0.99999918249409
Chi-Square: 0.0768861820713897
F-Statistic: 121099942.96929
Parameter Best Estimate
---------- -------------
p1 16.9216329635141
p2 -0.00486625930554683
p3 -42.7872667344874

  4 Comments

Show 1 older comment
William Thielicke
William Thielicke on 19 Oct 2019
Very nice, thanks a ton!
Walter Roberson
Walter Roberson on 19 Oct 2019
At the moment I do not understand why this is not the equivalent of a ratio between two first degree polynomials. If I have not made a mistake (and I might have at this time of night) that formula should be equivalent to a formula (a*x + b)/(c*x + c*b/(a - 1)) but the fit is completely different.
Alex Sha
Alex Sha on 19 Oct 2019
Hi,Walter, the formual of "y = x/(p1+p2*x)+p3" is sure to be equivalent to "y=(a*x + b)/(c*x + c*b/(a - 1))":
for y=(a*x + b)/(c*x + c*b/(a - 1)), the results are:
Root of Mean Square Error (RMSE): 0.313525722040911
Sum of Squared Residual: 19.7579740546361
Correlation Coef. (R): 0.999999591246962
R-Square: 0.99999918249409
Adjusted R-Square: 0.999999174236455
Determination Coef. (DC): 0.99999918249409
Chi-Square: 0.0768860578252577
F-Statistic: 121099942.964391
Parameter Best Estimate
---------- -------------
a 1.2082139354555
b -724.030425163212
c -0.00486625930282033

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 18 Oct 2019
There are an infinite number of functions that explain your data perfectly to within rounding error. It would be the infinity of the real numbers if matlab supported indefinite precision; with finite precision it is possibly only the infinity of the integers.
Because of this, the probability that any one of them is correct is 1/infinity which is 0.
It is thus not possible to find the "right" function given only the data.

  3 Comments

William Thielicke
William Thielicke on 18 Oct 2019
OK, then I need to re-sentence my question: Does someone have an idea about a "reasonable" function that would fit the data. "Reasonable" in terms of "smallest amount of characters when writing down the function". I am optimistic, that this might also represent the "physically correct" equation. I have given details about the experiment above, it is laser-triangulation just like this: http://www.movimed.com/wp-content/uploads/2018/02/02_Laser_Triangulation_explained-1024x553.png
Walter Roberson
Walter Roberson on 18 Oct 2019
A degree 5 polynomial is not horrible if you permit centering an scaling.
Linear model Poly5:
f(x) = p1*x^5 + p2*x^4 + p3*x^3 + p4*x^2 + p5*x + p6
where x is normalized by mean 2422 and std 571.2
Coefficients (with 95% confidence bounds):
p1 = 7.501 (7.015, 7.986)
p2 = 54.75 (52.38, 57.11)
p3 = 147.3 (144.5, 150)
p4 = 223.6 (220.7, 226.6)
p5 = 353.2 (350.6, 355.8)
p6 = 424.8 (423.6, 426)
Goodness of fit:
SSE: 4230
R-square: 0.9998
Adjusted R-square: 0.9998
RMSE: 4.658
However if you do not center and scale then the curve fitting routines complain that it is badly conditioned.
William Thielicke
William Thielicke on 19 Oct 2019
Thanks Walter, that looks already good, but especially for extrapolation, my feeling is that a polynomial might give surprising reults...

Sign in to comment.

Sign in to answer this question.

Products