0

I have been using mathematica recently to mess around with my data. I have a method of calculating an x,y coordinate from 4 or more distance measurements coming from static receivers (also x,y coords).

The function I use to do this most effectively with the data I have is the mathematica function:

NonlinearModelFit[data, Norm[{x, y} - {x0, y0}], {x0, y0}, {x, y},
                  Weights -> 1/distances, Method->"LevenbergMarquardt"]

where...

data = {{548189.217202, 5912779.96059, 93}, {548236.967784, 5912717.80716, 39},
        {548359.406452, 5912752.54022, 88}, {548358.636206, 5912690.89573, 97}};


distances = {93, 39, 88, 97};

x0, y0 is the solution it finds

The mathematica output to the above is:

FittedModel[{"Nonlinear", {x0 -> 548272.0043962265,
                           y0 -> 5.912735710367113*^6},
            {{x, y}, Sqrt[Abs[x - x0]^2 + Abs[y - y0]^2]}},
            {{1/93, 1/39, 1/88, 1/97}}, {{548189.217202, 5.91277996059*^6, 93},
            {548236.967784, 5.91271780716*^6, 39},
            {548359.406452, 5.91275254022*^6, 88},
            {548358.636206, 5.91269089573*^6, 97}},
            Function[Null, Internal`LocalizedBlock[{x, x0, y, y0}, #1], {HoldAll}]]

x0, y0 are my solution.

So I am not fitting a curve but fitting to a point (with weights inversely proportional to the distance). I have looked around on google but am simply not sure where to start with the scipy function scipy.optimize.leastsq algorithm to introduce the weighting functionality...

So why am I doing this if mathematica does it? Well to call mathematicascript (using subprocess module) from python code is too slow for what I want to do with live data so want to try rewriting in python to see if the speed can be improved.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
mjharrison
  • 25
  • 7
  • Not sure I understand the question: you have a function `norm(x,y, x0,y0)`, and you are minimizing the sum of form `norm(x1,y1, x0,y0)/d1 + norm(x2,y2, x0,y0)/d2 + norm(x3,y3, x0,y0)/d3+...` by varying `x0,y0`, where `x1,y1, x2,y2, ...` are the known parameters -- or is it something else? – ev-br Mar 11 '13 at 18:07
  • @mjharrison I have reformatted your code for readability, feel free to revert the edit if my formatting doesn't make sense in Mathematica. – Jaime Mar 11 '13 at 21:27

1 Answers1

1

An equivalent approach (gives exact same x0,y0) in Mathematica, perhaps this is easier to think about porting to python.

    FindMinimum[ 
           Total @ ((1/#[[3]]) (Norm[#[[1 ;; 2]] - {x0, y0}] - #[[3]])^2  & /@ 
                   data) , {x0, y0}]

Note I explicitly put the weight (1/#[[3]]) into the error criteria.

same thing a bit more readable.

      err[{x_, y_, z_}] := (1/z) (Norm[{x, y} - {x0, y0}] - z)^2
      FindMinimum[ Total @ (err  /@ data) , {x0, y0}]
AsukaMinato
  • 1,017
  • 12
  • 21
agentp
  • 6,849
  • 2
  • 19
  • 37