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
Best Answer