11
$\begingroup$

I want to trace the curve for the given data set.

This is my code

     pts = {{465.302, 247.796}, {100.732, 247.01}, {119.224, 42.4657}, {325.255, 
  17.7339}, {318.756, 283.584}, {499.567, 63.6977}, {310.721, 
  287.444}, {26.9471, 180.403}, {395.927, 273.221}, {368.045, 
  271.637}, {394.756, 24.373}, {187.579, 19.9764}, {379.001, 
  17.2288}, {545.629, 105.618}, {550.008, 137.879}, {388.283, 
  271.924}, {283.586, 11.0365}, {145.416, 258.876}, {9.03499, 
  134.851}, {534.812, 102.146}, {178.031, 268.363}, {447.15, 
  257.755}, {349.99, 276.643}, {73.4742, 56.8668}, {544.916, 
  101.512}, {288.193, 280.948}, {42.0569, 213.622}, {186.703, 
  269.109}, {378.495, 274.047}, {228.13, 281.019}, {51.1296, 
  72.1907}, {147.055, 268.347}, {45.4008, 206.851}, {208.656, 
  275.126}, {469.567, 248.686}, {308.452, 279.432}, {40.7974, 
  208.963}, {337.657, 282.141}, {443.21, 42.952}, {449.687, 
  258.946}, {311.266, 276.839}, {152.425, 269.8}, {419.123, 
  33.8306}, {79.9482, 53.6496}, {276.149, 9.86281}, {272.033, 
  7.11489}, {97.5072, 49.8366}, {262.92, 281.142}, {25.6081, 
  106.191}, {379.908, 274.569}, {543.667, 98.5575}, {160.077, 
  27.2338}, {398.438, 19.9315}, {265.113, 277.998}, {352.039, 
  11.3923}, {223.75, 279.167}, {550.373, 146.266}, {361.002, 
  11.6061}, {381.973, 17.7216}, {524.548, 206.913}, {47.1688, 
  82.1359}, {400.572, 265.738}, {302.766, 284.17}, {24.9839, 
  113.105}, {386.87, 267.605}, {543.044, 110.881}, {544.145, 
  118.1}, {72.178, 228.569}, {201.287, 17.4099}, {537.593, 
  96.732}, {245.844, 282.46}, {125.95, 35.7945}, {126.276, 
  35.1486}, {283.323, 12.9579}, {432.03, 32.0569}, {490.156, 
  229.188}, {187.804, 270.442}, {442.752, 40.183}, {410.278, 
  263.768}, {21.8946, 179.793}, {154.48, 23.3589}, {69.2665, 
  225.007}, {376.301, 276.076}, {466.387, 52.4701}, {520.09, 
  77.7554}, {189.943, 271.459}, {124.29, 250.907}, {281.935, 
  11.6622}, {247.094, 8.95544}, {389.127, 17.8674}, {68.6922, 
  229.083}, {30.9424, 191.098}, {161.086, 31.3124}, {216.199, 
  17.6549}, {475.856, 238.632}, {247.733, 17.1029}, {498.519, 
  224.687}, {408.9, 269.554}, {20.7042, 188.348}, {451.979, 
  44.9689}, {30.7126, 98.6237}, {373.218, 273.415}, {492.192, 
  58.8905}, {308.128, 286.02}, {443.24, 38.9625}, {60.6258, 
  222.16}, {41.5267, 80.9147}, {236.23, 284.485}, {544.81, 
  193.11}, {416.114, 29.3403}, {54.0816, 224.732}, {142.162, 
  256.687}, {165.4, 28.8549}, {15.773, 134.739}, {538.801, 
  202.256}, {332.078, 284.059}, {353.132, 14.7648}, {278.279, 
  282.172}, {526.683, 204.325}, {61.4494, 219.364}, {475.606, 
  237.591}, {178.634, 276.961}, {124.782, 40.4963}, {258.218, 
  279.259}, {289.924, 280.441}, {177.697, 276.02}, {546.535, 
  106.27}, {305.448, 12.5493}, {417.535, 28.6531}, {15.9444, 
  129.405}, {226.386, 282.084}, {107.532, 47.8901}, {184.42, 
  20.8464}, {303.016, 14.5519}, {92.1222, 47.4099}, {553.302, 
  154.452}, {17.0436, 124.738}, {15.8854, 135.147}, {18.5364, 
  176.525}, {247.784, 11.4122}, {229.798, 278.079}, {484.061, 
  56.0596}, {205.851, 13.477}, {214.129, 10.7242}, {85.6134, 
  242.559}, {123.336, 256.978}, {96.8295, 55.244}, {333.557, 
  285.583}, {230.976, 280.373}, {253.041, 16.7594}, {19.1676, 
  109.802}, {554.128, 159.326}, {455.361, 40.7296}, {517.993, 
  75.6124}, {128.245, 38.9897}, {252.325, 8.19425}, {151.285, 
  262.697}, {441.277, 37.0819}, {15.4735, 172.528}, {370.261, 
  273.103}, {20.3036, 167.98}, {306.387, 8.86591}, {10.7912, 
  157.721}, {464.234, 253.389}, {248.294, 282.49}, {504.109, 
  221.291}, {77.1577, 240.759}, {489.979, 236.638}, {476.421, 
  237.917}, {105.401, 40.3942}, {479.418, 50.3713}, {351.19, 
  18.4029}, {536.189, 189.566}, {56.21, 75.3998}, {20.4849, 
  122.668}, {122.235, 42.563}, {422.536, 260.39}, {266.302, 
  284.858}, {464.772, 40.2306}, {387.285, 278.219}, {107.814, 
  41.0823}, {442.766, 259.048}, {454.204, 44.0058}, {528.349, 
  88.016}, {269.989, 11.8865}, {34.5577, 100.702}, {436.206, 
  260.15}, {538.176, 195.335}, {228.174, 12.8474}, {27.2441, 112.313},
  {221.57, 18.0464}, {231.513, 279.885}, {401.089, 270.572}, {439.9, 
  36.1922}, {188.533, 15.7943}, {360.085, 16.9698}, {22.98, 
  187.373}, {75.8475, 57.0842}, {364.183, 278.657}, {407.266, 
  27.5398}, {255.345, 278.215}, {524.146, 211.964}, {167.79, 
  21.6328}, {325.392, 278.029}, {210.571, 20.5786}, {134.737, 
  34.2591}, {89.602, 54.9735}, {551.263, 117.395}, {317.205, 
  10.4819}, {329.518, 16.9544}, {558.44, 145.311}, {125.7, 
  39.5867}, {481.071, 237.54}, {243.18, 282.807}, {496.341, 
  225.601}, {455.069, 37.2108}, {282.097, 287.254}, {282.487, 
  13.6979}, {162.886, 265.388}, {219.283, 20.5863}, {452.148, 
  249.101}, {493.321, 59.9754}, {512.818, 216.94}, {39.1416, 
  91.0125}, {251.071, 284.862}, {556.502, 143.996}, {541.606, 
  193.46}, {548.117, 174.13}, {550.339, 167.337}, {148.208, 
  31.7878}, {535.255, 103.158}, {96.8475, 240.15}, {109.504, 
  256.249}, {18.8648, 167.318}, {231.479, 15.4998}, {422.7, 
  265.448}, {239.009, 284.592}, {59.9833, 72.9196}, {396.71, 264.941},
  {178.13, 273.65}, {163.102, 272.844}, {471.25, 51.5322}, {291.099, 
  5.8935}, {312.323, 16.8829}, {209.496, 281.543}, {30.554, 
  99.8663}, {99.138, 47.9444}, {549.832, 146.061}, {499.16, 
  59.3727}, {475.705, 51.8307}, {344.309, 276.729}, {443.58, 
  252.146}, {230.523, 276.124}, {226.899, 278.722}, {262.362, 
  17.402}, {363.855, 11.8984}, {12.1561, 146.561}, {133.904, 
  39.2059}, {497.374, 64.7685}, {90.1903, 57.6868}, {137.696, 
  26.9482}, {62.0312, 75.8927}, {285.149, 14.6837}, {448.484, 
  38.2635}, {132.798, 257.957}, {50.4869, 73.3527}, {426.976, 
  267.593}, {8.66257, 158.774}, {178.69, 276.365}, {137.446, 
  31.084}, {425.264, 36.2503}, {282.6, 14.8515}, {375.604, 
  18.6047}, {549.023, 164.189}, {540.398, 198.418}, {530.539, 
  89.5846}, {88.3028, 49.2858}, {458.625, 48.4402}, {187.563, 
  270.068}, {416.335, 269.791}, {484.147, 232.806}, {29.2683, 
  102.471}, {238.71, 281.789}, {96.4815, 250.023}, {17.1013, 
  115.931}, {207.999, 273.452}, {210.403, 17.5497}, {509.899, 
  66.3078}, {42.2069, 85.9688}, {47.9787, 212.531}, {420.237, 
  30.7798}, {15.0482, 143.138}, {375.78, 279.715}, {421.687, 
  29.3802}, {230.66, 17.2373}, {352.842, 278.854}, {16.633, 
  114.365}, {429.668, 255.528}, {201.304, 20.4621}, {256.545, 
  281.11}, {64.7614, 68.6319}, {237.643, 9.94991}, {559.796, 
  148.963}, {319.699, 277.684}, {20.9321, 117.817}, {468.063, 
  47.8955}, {499.741, 69.329}, {417.194, 25.0924}, {102.675, 
  39.9647}, {108.555, 256.231}, {198.2, 15.5882}, {497.102, 
  233.738}, {394.98, 23.1966}, {460.588, 43.9541}, {14.8324, 
  140.164}, {233.131, 11.3155}, {113.474, 248.203}, {28.4194, 
  186.293}, {14.7, 134.36}, {494.66, 60.3701}, {350.545, 
  277.697}, {219.984, 19.9239}, {484.729, 244.139}, {51.4992, 
  80.0979}, {537.291, 95.0876}, {128.789, 252.439}, {122.28, 
  38.9316}, {186.056, 277.165}, {65.7549, 224.443}, {173.935, 
  26.7053}, {541.124, 180.923}, {306.021, 279.904}, {15.3222, 
  138.105}, {185.841, 278.291}, {127.048, 37.8384}, {468.983, 
  250.595}, {530.001, 211.434}, {55.0502, 78.4593}, {37.0245, 
  196.488}, {477.551, 244.24}, {296.013, 286.34}, {529.772, 
  93.176}, {184.913, 271.522}, {507.766, 68.6946}, {140.926, 
  30.4062}, {431.863, 33.0485}, {230.038, 9.12564}, {309.197, 
  282.328}, {554.096, 121.979}, {171.371, 24.9378}, {308.336, 
  9.18316}, {79.4051, 239.439}, {545.299, 100.732}, {514.833, 
  214.542}, {356.856, 13.0061}, {517.816, 84.9898}, {284.289, 
  284.336}, {171.784, 273.039}, {366.461, 15.6313}, {352.427, 
  281.581}, {225.607, 18.817}, {25.3003, 190.18}, {323.462, 
  284.304}, {75.2392, 57.8409}, {527.443, 82.8886}, {440.453, 
  41.8566}, {29.3609, 193.342}, {74.9634, 61.9223}, {558.773, 
  159.527}, {26.9852, 188.329}, {559.62, 147.743}, {129.347, 
  36.4634}, {409.779, 30.896}, {73.1597, 59.8505}, {465.424, 
  249.259}, {41.9467, 89.3623}, {47.2739, 86.4977}, {220.584, 
  275.888}, {413.35, 28.2137}, {235.712, 283.017}, {421.992, 
  263.72}, {93.6369, 238.182}, {547.273, 129.125}, {209.45, 
  272.191}, {321.587, 17.437}, {238.685, 9.76702}, {45.1402, 
  88.351}, {196.799, 21.6793}, {338.091, 16.8859}, {311.161, 
  15.1921}, {367.131, 277.12}, {190.915, 17.0876}, {29.3558, 
  196.305}, {475.513, 247.84}, {76.8984, 59.2354}, {475.917, 
  243.902}, {226.69, 284.785}, {299.745, 15.4357}, {102.394, 
  252.598}, {538.07, 105.176}, {269.752, 8.81818}, {480.457, 
  242.972}, {539.959, 193.981}, {149.172, 28.4105}, {90.7731, 
  242.446}, {418.281, 29.6794}, {86.0118, 60.0169}, {549.199, 
  120.943}, {26.0357, 190.247}, {380.708, 17.8098}, {189.625, 
  23.5462}, {489.574, 229.305}, {101.304, 251.321}, {11.753, 
  155.419}, {169.47, 267.773}, {289.964, 15.879}, {179.18, 
  273.113}, {404.775, 26.465}, {479.374, 239.821}, {443.412, 
  255.279}, {559.408, 141.156}, {503.839, 227.549}, {477.721, 
  56.7715}, {203.543, 16.9369}, {554.106, 116.562}, {530.594, 
  83.6009}, {558.078, 162.95}, {359.572, 13.0296}, {33.1991, 
  203.17}, {189.232, 24.4352}, {364.847, 271.466}, {64.3696, 
  220.982}, {550.47, 169.292}, {183.707, 269.145}, {547.798, 
  124.055}, {475.595, 237.944}, {254.887, 10.2161}, {30.1837, 
  93.8457}, {407.629, 28.9718}, {530.981, 200.279}, {18.9379, 
  114.71}, {15.0161, 121.044}, {144.415, 35.8059}, {33.5156, 
  101.305}, {133.144, 40.1251}, {74.4527, 57.735}, {169.355, 
  272.777}, {237.579, 276.271}, {41.7276, 89.4028}, {310.732, 
  285.575}, {24.4119, 104.173}}

ListPlot[{pts[[All, {1, 2}]]}, PlotStyle -> {Red}, PlotRange -> Full]

enter image description here

I am using LeastSquares to fit the data, however, it's not working as desired. I am unable to find the problem with the following code:

{x, y} = {pts[[All, 1]], pts[[All, 2]]};
m = Map[x \[Function] {1, x, x^2, x^3, x^4, x^5, x^6, x^7}, x];
{a, b, c, d, e, f, g, h} = 
 LeastSquares[m, y, Tolerance -> 10^-8]
ff = Fit[pts[[All, {1, 2}]], {1, s, s^2, s^3, s^4, s^5, s^6, s^7}, s]

Show[Plot[
  a x^0 + b x^1 + c x^2 + d x^3 + e x^4 + f x^5 + g x^6 + 
   h x^7, {x, -600, 600}, 
  PlotRange -> {{0, 600}, {0, 300}}], 
 ListPlot[{pts[[All, {1, 2}]]}, PlotStyle -> {Red}], ImageSize -> 600]

![![enter image description here

Do I need to try other ways of fitting the curve? Please help me solve this problem.

$\endgroup$
2
  • 1
    $\begingroup$ Please provide some points of your dataset in copyable form (can't access your link). Thanks! $\endgroup$ Commented Jan 5, 2023 at 8:40
  • $\begingroup$ I have edited it. $\endgroup$ Commented Jan 5, 2023 at 8:48

2 Answers 2

18
$\begingroup$

You have to fit implicitly, for example fit a conic section (or ellipse )

nlm = NonlinearModelFit[Map[{#[[1]], #[[2]], 0} &,pts], {1, a, b, c, d, e} . {1, x, y , x^2, x y, y^2} , {a, b, c, d,e}, {x, y}]

nlm[x, y] (*1 - 0.00601378 x + 0.0000104909 x^2 - 0.0123825 y + 2.96388*10^-7 x y + 0.0000417392 y^2*)

Show[{ListPlot[pts], ImplicitRegion[nlm[x, y] == 0, {x, y}] // Region}]

enter image description here

$\endgroup$
10
  • $\begingroup$ What if it is not a closed loop! Can I use ImplicitRegion in that case? $\endgroup$ Commented Jan 5, 2023 at 9:03
  • 1
    $\begingroup$ Why not? Alternatively try ContourPlot[nlm[x,y]==0,...] $\endgroup$ Commented Jan 5, 2023 at 9:07
  • $\begingroup$ Doesn't really affect the main point of the answer, but one could use Append[0] /@ pts as a more expressive alternative to Map[{#[[1]], #[[2]], 0} &,pts] $\endgroup$ Commented Jan 5, 2023 at 9:15
  • $\begingroup$ ContourPlot[nlm[x,y]==0,...] this works. Thank you. Is there a way to increase the accuracy? $\endgroup$ Commented Jan 5, 2023 at 9:18
  • 1
    $\begingroup$ NonlinearModelFit[...,WorkingPrecision -> 100] works fine $\endgroup$ Commented Jan 5, 2023 at 9:37
10
$\begingroup$

NB: see comment by @JimB. Original post was wrong.

Updated Post

I have upvoted @UlrichNeumann answer. For what it is worth you can use LinearModelFit. For example:

d = {#1^2, #1, #2, #1 #2, #2^2} & @@@ pts;
lm = LinearModelFit[d, {a, b, c, e}, {a, b, c, e}];
v = {1, x^2, x, y, x y}
exp = y^2 - lm["BestFitParameters"] . v;
{u1, u2} = 
  y^2 - lm["SinglePredictionBands"] /. Thread[{1, a, b, c, e} -> v];
ContourPlot[{exp == 0, u1 == 0, u2 == 0}, {x, 0, 600}, {y, 0, 300}, 
 Epilog -> Point[pts], AspectRatio -> Automatic]

enter image description here

I leave the original post for transparency.

Original Post

I have upvoted @UlrichNeumann answer. For what it is worth you can use LinearModelFit. For example:

d = {#1^2, #1, #2, #1 #2, #2^2} & @@@ pts;
lm = LinearModelFit[d, {a, b, c, e}, {a, b, c, e}];
exp = y^2 - lm["BestFitParameters"] . {1, x^2, x, y, x y}
ci = lm["ParameterConfidenceIntervalTableEntries"];
exp2 = y^2 - ci[[All, 3, 1]] . {1, x^2, x, y, x y}
exp3 = y^2 - ci[[All, 3, 2]] . {1, x^2, x, y, x y}
ContourPlot[{exp == 0, exp2 == 0, exp3 == 0}, {x, 0, 600}, {y, 0, 
  300}, Epilog -> Point[pts], PlotLabel -> Row[{exp, "=0"}], 
 AspectRatio -> Automatic]

enter image description here

$\endgroup$
3
  • $\begingroup$ +1 But your approach for obtaining a 95% confidence interval is a bit "unusual" because of two reasons: (1) the parameter estimators are not independent as shown with lm["CorrelationMatrix"] // MatrixForm, and (2) even if the parameter estimators were independent, using 95% confidence intervals for each parameter doesn't necessarily result in a 95% confidence interval for the ellipse. (And as of this moment, I have to confess I don't know the correct way of doing so.) $\endgroup$ Commented Jan 6, 2023 at 18:58
  • $\begingroup$ @JimB yes I had a lot of reservation about ci and have to admit did not give it enough thought and on reflection there are other ways. Thank you for your comment. I think my dumb “ plug and play” was wrong as you allude to and needed to be called out. I think the only relevance of answer as you can use ordinary least squares in a linear model. $\endgroup$ Commented Jan 7, 2023 at 0:31
  • $\begingroup$ The good thing is that you thought that a confidence region (or some measure of precision) was necessary for a curve fit. That doesn't always happen in this forum. $\endgroup$ Commented Jan 7, 2023 at 2:21

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.