I am wondering if there is a C/C++ library or Matlab code technique to determine real and complex numbers using a minimization solver. Here is a code snippet showing what I would like to do. For example, suppose that I know Utilde, but not x and U variables. I want to use optimization (fminsearch) to determine x and U, given Utilde. Note that Utilde is a complex number.
x = 1.5;
U = 50 + 1i*25;
x0 = [1 20]; % starting values
Utilde = U * (1 / exp(2 * x)) * exp( 1i * 2 * x);
xout = fminsearch(@(v)optim(v, Utilde), x0);
function diff = optim(v, Utilde)
x = v(1);
U = v(2);
diff = abs( -(Utilde/U) + (1 / exp(2 * x)) * exp( 1i * 2 * x ) );
The code above does not converge to the proper values, and xout = 1.7318 88.8760. However, if U = 50, which is not a complex number, then xout = 1.5000 50.0000, which are the proper values.
Is there a way in Matlab or C/C++ to ensure proper convergence, given Utilde as a complex number? Maybe I have to change the code above?
-
If there isn’t a way to do this natively in Matlab, then perhaps one
gist of the question is this: Is there a multivariate (i.e.
Nelder-Mead or similar algorithm) optimization library that is able
to work with real and complex inputs and outputs? -
Yet another question is whether the function is convergent or not. I
don’t know if it is the algorithm or the function. Might I need to change something in theUtilde = U * (1 / exp(2 * x)) * exp( 1i * 2 * x)expression to make it convergent?
The main problem here is that there is no unique solution to this optimization or parameter fitting problem. For example, looking at the expected and actual results above,
Utildeis equivalent (ignoring round-off differences) for the two (x,U) pairs, i.e.Although I have not examined it in depth, I even suspect that for any value of
x, you can find anUthat computes toUtilde(x, U) = Utilde(x = 1.5, U = 50 + 25i).The solution here would thus be to further constrain the parameter fitting problem so that the solver yields any solution that can be considered acceptable. Alternatively, reformulate
Utildeto have a unique value for any (x,U) pair.UPDATE, AUG 1
Given reasonable starting values, it actually seems like it is sufficient to restrict
xto be real-valued. Performing unconstrained non-linear optimization using thedifffunction formulated above, I get the following result:However, changing the starting guess to values more distant from the desired values does yield different solutions, so restricting
xto be real-values does not alone provide a unique solution to the problem.I have implemented this in C#, using the BOBYQA optimizer, but the numerics should be the same as above. If you want to try outside of Matlab, it should also be relatively simple to turn the C# code below into C++ code using the std::complex class and an (unconstrained) nonlinear C++ optimizer of your own choice. You could find some C++ compatible codes that do not require gradient computation here, and there is also various implementations available in Numerical Recipes. For example, you could access the C version of NR online here.
For reference, here are the relevant parts of my C# code: