I am trying to use LsqFit to find out a 2D geometric transformation parameter, say a mapping from (x,y) to (xp,yp):
x = [x for x=-2.:2. for y=-2.:2.]
y = [y for x=-2.:2. for y=-2.:2.]
in_data = [x y]
k = -0.02
@. xp = x + k*x*(x^2+y^2)
@. yp = y + k*y*(x^2+y^2)
out_data = [xp yp]
However, from the readme of LsqFit it’s not clear how to accommodate multi-dimensional dependent variable (or is it possible?). I tried to format the dependent variable in the same format as the independent variable, which is a nx2 matrix, as follows:
k0=[0.]
function model(x,p)
@. xp = x[:,1]+p[1]*x[:,1]*(x[:,1]^2+x[:,2]^2)
@. yp = x[:,2]+p[1]*x[:,2]*(x[:,1]^2+x[:,2]^2)
return [xp yp]
end
ret = curve_fit(model, in_data, out_data, k0)
But this doesn’t work and I got the error message:
LoadError: MethodError: no method matching mul!(::Array{Float64,1}, ::LinearAlgebra.Transpose{Float64,Array{Float64,2}}, ::Array{Float64,2}, ::Bool, ::Bool)
I saw this post which addresses the multivariate input variable, but I still can’t figure out how to deal with the output variable.
This example can actually be solved analytically, but I just wanted to find out how to do least squares for 2D points.
Originally posted here.