How can I use Curve Fitting Toolbox for fitting complex data points?

21 views (last 30 days)
My problem consists of 4 parameters, two are real and two are imaginary (creating an array of two complex variables). Essentially I want to approximate a function of the form:
f(x, y, z, w) = (x + iy) + (w + iz)
How can I accomplish this using functions from the Curve Fitting Toolbox? How can I use Curve Fitting Toolbox for fitting such complex data points?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 26 Apr 2019

Here is an example showing how to approximate the function:

f(x, y, z, w) = (x + iy) + (w + iz)

To accomplish this, we can use the "lsqcurvefit" function from the Optimization Toolbox. The reason being Curve Fitting Toolbox does not handle complex data as mentioned here:

https://www.mathworks.com/help/curvefit/fit.html#bto2vuv-1-x

Also, not all the functions in the Optimization Toolbox handle complex data as well as discussed here:

https://www.mathworks.com/help/optim/ug/complex-numbers-in-optimization-toolbox-solvers.html

The following documentation link describes some approaches for handling complex data using the functions in Optimization Toolbox:

https://www.mathworks.com/help/optim/ug/fit-model-to-complex-data.html

The example shown here is based on splitting real and imaginary part of the complex data as discussed in the above link under the Section

"Alternative: Split Real and Imaginary Parts"

%% Create target data
tmpData = 100*rand(10, 4);
targetData = tmpData(:, 1) + 3i*tmpData(:, 2) + 0.5*tmpData(:, 3) - 2*tmpData(:, 4);
% goal is to approximate x + yi + w + zi
%% Split target data into real and imaginary parts
uData = [real(targetData) imag(targetData)];
%% Input data
inputData = ones(10, 4);
% these are all ones because the function to approximate can be written as:
% x * 1 + y * 1 * 1i + z * 1 + w * 1 * 1i
% we want to find x, y, z, w
% so the input data are all ones
%% Starting assumption for x, y, z, w values
coeff0 = zeros(size(inputData));
%% Create options for the function call
opts = optimoptions(@lsqcurvefit, 'Algorithm', 'levenberg-marquardt');
%% Call the optimization function
coeffVals = lsqcurvefit(@curveFunc, coeff0, inputData, uData, [], [], opts);
%% Get the predicted data based on output of the optimization routine
predictedData = curveFunc(coeffVals, inputData);
%% Plot the original data and predicted data
figure; hold on;
plot(uData(:, 1), uData(:, 2), 'g*');
plot(predictedData(:, 1), predictedData(:, 2), 'bo');
function uout = curveFunc(v, xdata)
    % this part is broken down to show how the "curveFunc" relates to the function that we are trying to approximate
    tmpUout = v(:, 1) .* xdata(:, 1); % a values
    tmpUout = tmpUout + 1i * v(:, 2) .* xdata(:, 2); % b values
    tmpUout = tmpUout + v(:, 3) .* xdata(:, 3); % c values
    tmpUout = tmpUout + 1i * v(:, 4) .* xdata(:, 4); % d values
    uout = [real(tmpUout) imag(tmpUout)];
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!