MATLAB: How does one specify a real datatype when attempting to fit real parameters to a complex function with lsqcurvefit

complex functionimaginary parameterlsqcurvefitMATLABOptimization Toolbox

I have a complex data set that I'd like to determine a model for. I'm using the optimization toolbox to calculate parameters in a complex function. I've been working with the lsqcurvefit solver. I have four parameters I wish to determine the optimum value for which I expect to be real numbers, but when using the solver find that they often have a large imaginary component.
The code I've written returns an array "C" with input of initial conditions array "C0" and x and y data, where x is real and y is complex. Attached is a sample script to illustrate the issue. Note: I've suppressed the actual contents of x and y for brevity.
Thus far, I've attempted using real(C(i)) to account for the presence of the imaginary components in the model. While this should work in theory to optimize the real components alone it doesn't suppress the imaginary component entirely which I suspect costs additional computational resources.
In summary, I'd like to limit the optimization search to only consider real values of the parameters.

Best Answer

You won't get complex values for C if you fit real(y) to real(f(x)) and imag(y) to imag(f(x)) simultaneously.
In this case, you only work with real numbers.
Best wishes
Torsten.