Search code examples
wolfram-mathematicaimplicittest-data

FindFit for implicit functions - mathematica


I would like to fit some test data into some implicit function.

I would like to fit a few parameters to an eliptical equation f(x,y)=a where a is a known variable. My test data and the function are more complex, however I got more data points than variables. It is kind of not possible to convert the equation that I want to fit into an explicit form like f(x)=y Therefore I attached some code to get the basic idea.

Test = {{0, 1}, {0.1, 0.9}, {1.1, 0}};

Ftest = a*x^2 + b*y^2

FindFit[Test, Ftest == 2, {a, b}, {x, y}];

However this leads to an error: Number of coordinates (1) is not equal to the number of variables \ (2). >>


Solution

  • You can pose this as a least squares minimization:

    data = {{0, 1}, {0.1, 0.9}, {1.1, 0}}
    Ftest[x_, y_] := a*x^2 + b*y^2
    fit = FindMinimum[ Total[(Ftest @@@ data - 2)^2] , {a, b}] 
    ContourPlot[ (Ftest[x, y] /. fit[[2]]) == 2 , {x, 0, 1.5}, {y, 0, 
      1.5}, Epilog -> {Red, Point /@ data}]
    

    enter image description here

    to use fit functions you need to solve for y and you end up with:

    fit = NonlinearModelFit[data, Sqrt[2 - a*x^2]/Sqrt[b], {a, b}, x]
    
    Plot[fit[x], {x, 0, 1.2}, Epilog -> {Red, Point /@ data}, 
     AspectRatio -> 1] 
    

    enter image description here