2

I wanted to fit a geometric mapping parameter with some input/output (x,y) points. The model is very simple:

xp = x .+ k.*x.*(x.^2+y.^2)
yp = y .+ k.*y.*(x.^2+y.^2)

k is the only parameter, (x,y) is an input point and (xp,yp) is an output point. I formulated the input/output data array as:

x = [x for x=-2.:2. for y=-2.:2.]
y = [y for x=-2.:2. for y=-2.:2.]
in_data = [x y]
out_data = [xp yp]

However I'm confused about how to turn this into the LsqFit model, I tried:

k0=[0.]
@. model(x,p) = [x[:,1]+p[1]*x[:,1]*(x[:,1]^2+x[:,2]^2) x[:,2]+p[1]*x[:,2]*(x[:,1]^2+x[:,2]^2)]
ret = curve_fit(model, in_data, out_data, k0)

but got an error:

DimensionMismatch("dimensions must match: a has dims (Base.OneTo(25), Base.OneTo(2)), must have singleton at dim 2")

So the question is: is it possible to use LsqFit for multi-variate output? (even though this particular problem can be solved analytically)

2
  • It seems to me that the minimization isn't well defined in your case? In terms of model fit, how would you compare a model for which the predicted (x, y) is off of your input data by (0.5, 0.5) compared to one that is off by (0.25, 0.75)? You should probably think about the appropriate norm to compute a scalar distance notion. Commented Jun 30, 2020 at 6:08
  • Isn't it just the 2 norm/Euclidean distance between input/output data points? Commented Jun 30, 2020 at 15:59

1 Answer 1

1

OK Just figured out the correct way to do this. The vector output variable needs to be stacked together to form a 1D array. So the only changes needed is:

out_data = [xp; yp]
Sign up to request clarification or add additional context in comments.

1 Comment

I think you also need to form a 1D column vector with the in_data as well, i.e., in_data = [x; y] Additionally, the model() function needs to reflect that change too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.