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 the Utilde = U * (1 / exp(2 * x)) * exp( 1i * 2 * x)
expression to make it convergent?
An complex number is represented by “ x + yi “. Python converts the real numbers x and y into complex using the function complex(x,y). The real part can be accessed using the function real() and imaginary part can be represented by imag().
The complex library implements the complex class to contain complex numbers in cartesian form and several functions and overloads to operate with them. real() – It returns the real part of the complex number. imag() – It returns the imaginary part of the complex number.
Complex Numbers Arithmetic Since complex is a native data type in Python, you can plug complex numbers into arithmetic expressions and call many of the built-in functions on them. More advanced functions for complex numbers are defined in the cmath module, which is part of the standard library.
In MATLAB®, i and j represent the basic imaginary unit. You can use them to create complex numbers such as 2i+5 . You can also determine the real and imaginary parts of complex numbers and compute other common values such as phase and angle.
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, Utilde
is equivalent (ignoring round-off differences) for the two (x
, U
) pairs, i.e.
Utilde(x = 1.5, U = 50 + 25i) = Utilde(x = 1.7318, U = 88.8760)
Although I have not examined it in depth, I even suspect that for any value of x
, you can find an U
that computes to Utilde(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 Utilde
to 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 x
to be real-valued. Performing unconstrained non-linear optimization using the diff
function formulated above, I get the following result:
x = 1.50462926953244
U = 50.6977768845879 + 24.7676554234729i
diff = 3.18731710515855E-06
However, changing the starting guess to values more distant from the desired values does yield different solutions, so restricting x
to 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:
class Program
{
private static readonly Complex Coeff = new Complex(-2.0, 2.0);
private static readonly Complex UTilde0 = GetUTilde(1.5, new Complex(50.0, 25.0));
static void Main(string[] args)
{
double[] vars = new[] {1.0, 25.0, 0.0}; // xstart = 1.0, Ustart = 25.0
BobyqaExitStatus status = Bobyqa.FindMinimum(GetObjfnValue, vars.Length, vars);
}
public static Complex GetUTilde(double x, Complex U)
{
return U * Complex.Exp(Coeff * x);
}
public static double GetObjfnValue(int n, double[] vars)
{
double x = vars[0];
Complex U = new Complex(vars[1], vars[2]);
return Complex.Abs(-UTilde0 / U + Complex.Exp(Coeff * x));
}
}
The documentation for fminsearch
says how to deal with complex numbers in the limitations section:
fminsearch
only minimizes over the real numbers, that is,x
must only consist of real numbers andf(x)
must only return real numbers. Whenx
has complex variables, they must be split into real and imaginary parts.
You can use the functions real
and imag
to extract the real and imaginary parts, respectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With