main content

fit a model to complex-凯发k8网页登录

this example shows how to perform nonlinear fitting of complex-valued data. while most optimization toolbox™ solvers and algorithms operate only on real-valued data, least-squares solvers and fsolve can work on both real-valued and complex-valued data for unconstrained problems. the objective function must be analytic in the complex function sense.

do not set the funvalcheck option to 'on' when using complex data. the solver errors. do not use the 'interior-point' algorithm with lsqcurvefit or lsqnonlin; this algorithm is primarily for handling constraints, and has not been validated to work with complex data.

data model

the data model is a simple exponential:

$$y(x) = v_1   v_2 e^{v_3 x}.$$

the $x$ is input data, $y$ is the response, and $v$ is a complex-valued vector of coefficients. the goal is to estimate $v$ from $x$ and noisy observations $y$. the data model is analytic, so you can use it in a complex solution.

artificial data with noise

generate artificial data for the model. take the complex coefficient vector $v$ as [2;3 4i;-.5 .4i]. take the observations $x$ as exponentially distributed. add complex-valued noise to the responses $y$.

rng default % for reproducibility
n = 100; % number of observations
v0 = [2;3 4i;-.5 .4i]; % coefficient vector
xdata = -log(rand(n,1)); % exponentially distributed
noisedata = randn(n,1).*exp((1i*randn(n,1))); % complex noise
cplxydata = v0(1)   v0(2).*exp(v0(3)*xdata)   noisedata;

fit the model to recover the coefficient vector

the difference between the response predicted by the data model and an observation (xdata for $x$ and response cplxydata for $y$) is:

objfcn = @(v)v(1) v(2)*exp(v(3)*xdata) - cplxydata;

use either lsqnonlin or lsqcurvefit to fit the model to the data. this example first uses lsqnonlin.

opts = optimoptions(@lsqnonlin,'display','off');
x0 = (1 1i)*[1;1;1]; % arbitrary initial guess
[vestimated,resnorm,residuals,exitflag,output] = lsqnonlin(objfcn,x0,[],[],opts);
vestimated,resnorm,exitflag,output.firstorderopt
vestimated =
   2.1582   0.1351i
   2.7399   3.8012i
  -0.5338   0.4660i
resnorm =
  100.9933
exitflag =
     3
ans =
    0.0018

lsqnonlin recovers the complex coefficient vector to about one significant digit. the norm of the residual is sizable, indicating that the noise keeps the model from fitting all the observations. the exit flag is 3, not the preferable 1, because the first-order optimality measure is about 1e-3, not below 1e-6.

alternative: use lsqcurvefit

to fit using lsqcurvefit, write the model to give just the responses, not the responses minus the response data.

objfcn = @(v,xdata)v(1) v(2)*exp(v(3)*xdata);

use lsqcurvefit options and syntax.

opts = optimoptions(@lsqcurvefit,opts); % reuse the options
[vestimated,resnorm] = lsqcurvefit(objfcn,x0,xdata,cplxydata,[],[],opts)
vestimated =
   2.1582   0.1351i
   2.7399   3.8012i
  -0.5338   0.4660i
resnorm =
  100.9933

the results match those from lsqnonlin, because the underlying algorithms are identical. use whichever solver you find more convenient.

alternative: split real and imaginary parts

to include bounds, or simply to stay completely within real values, you can split the real and complex parts of the coefficients into separate variables. for this problem, split the coefficients as follows:

$$ \begin{array}{l}
y = {v_1}   i{v_2}   ({v_3}   i{v_4})\exp \left( {({v_5}   i{v_6})x} \right)\\
\ \ = \left( {{v_1}   {v_3}\exp ({v_5}x)\cos ({v_6}x) - {v_4}\exp ({v_5}x)\sin ({v_6}x)} \right)\\
\ \   i \left( {{v_2}   {v_4}\exp ({v_5}x)\cos ({v_6}x)   {v_3}\exp ({v_5}x)\sin ({v_6}x)} \right).
\end{array}$$

write the response function for lsqcurvefit.

function yout = cplxreal(v,xdata)
yout = zeros(length(xdata),2); % allocate yout
expcoef = exp(v(5)*xdata(:)); % magnitude
coscoef = cos(v(6)*xdata(:)); % real cosine term
sincoef = sin(v(6)*xdata(:)); % imaginary sin term
yout(:,1) = v(1)   expcoef.*(v(3)*coscoef - v(4)*sincoef);
yout(:,2) = v(2)   expcoef.*(v(4)*coscoef   v(3)*sincoef);

save this code as the file cplxreal.m on your matlab® path.

split the response data into its real and imaginary parts.

ydata2 = [real(cplxydata),imag(cplxydata)];

the coefficient vector v now has six dimensions. initialize it as all ones, and solve the problem using lsqcurvefit.

x0 = ones(6,1);
[vestimated,resnorm,residuals,exitflag,output] = ...
    lsqcurvefit(@cplxreal,x0,xdata,ydata2);
vestimated,resnorm,exitflag,output.firstorderopt
local minimum possible.
lsqcurvefit stopped because the final change in the sum of squares relative to 
its initial value is less than the value of the function tolerance.
vestimated =
    2.1582
    0.1351
    2.7399
    3.8012
   -0.5338
    0.4660
resnorm =
  100.9933
exitflag =
     3
ans =
    0.0018

interpret the six-element vector vestimated as a three-element complex vector, and you see that the solution is virtually the same as the previous solutions.

related topics

    网站地图