You can't use functions that terminate a solve block directly in a program. You first need to turn the solve block into a function. then call that function in a program. In this case it's a little easier to use minerr, because then you just need to define a matrix of residuals. If minerr is set to the Levenberg Marquardt method (which is the default) you don't need to calculate the sums of squared errors (in fact, you should specifically not do so!).
Thank you for your very useful answer