Hello all,

I have been working on an optimization problem with a matrix variable in R, but I haven't managed to get a numerical solution, as I am receiving an error message. Below is a simple example representing my code:

```
Xrs1 = matrix(runif(n=10*3, min=0, max=1), nrow=10, ncol=3)
Xrs2 = matrix(runif(n=40*3, min=0, max=1), nrow=40, ncol=3)
Xdr2 = matrix(runif(n=40*2, min=-5, max=5), nrow=40, ncol=2)
d = fields::rdist(Xrs1, Xrs2)
dr=\(Xdr1) fields::rdist(Xdr1, Xdr2)
f=\(Xdr1) sum( (dr(Xdr1) - d)^2 )
optim(par=matrix(0, nrow=10, ncol=2), fn=f, method="BFGS")
```

I receive an error, despite the fact that the function f works perfectly with the initial solution or any other numerical matrix with that dimensions.

I would appreciate any help.

Thank you in advance for your time and support.