How can I fit a complex valued function to my data?

I am trying to find parameters which would fit a complex valued function to my data, which is real. The function that I want to use is:
y = c*x*1i + x^2;

 채택된 답변

MathWorks Support Team
MathWorks Support Team 2019년 8월 29일
편집: MathWorks Support Team 2019년 8월 29일
The Curve Fitting Toolbox does not accept complex valued functions. To carry out the fit, use the LSQNONLIN function or the FMINSEARCH function. The objective function passed to LSQNONLIN or FMINSEARCH should take the real and complex parts of the parameters separately and return the real and complex parts of the residuals separately.
For example, create the function 'myfun.m'.
function F = myfun(c,xdata,ydata)
% c is a two-element vector whose components are the real
% and imaginary parts of the parameter respectively
errors = (c(1) + c(2)*1i)*xdata*1i + xdata.^2 - ydata;
F = [real(errors(:)); imag(errors(:))];
Note that here, xdata and ydata may be complex, but the input and output are real.
To use this with LSQNONLIN:
[x,resnorm] = lsqnonlin(@(c)myfun(c,xdata,ydata),[1 3])
To use this with FMINSEARCH, add
F = sum(F.^2);
to the bottom of 'myfunc.m' and replace 'lsqnonlin' with 'fminsearch' in the above syntax.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Fit Postprocessing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by