Why does solving my equation result in the message "No complex subexpressions allowed in real mode."?

I have an equation with a bunch of complex values in it, and with a variable in it that experimentally must be a real number. I am trying to find this real variable, p_w.
The code is as follows:
%loading the constants
eps_w = load('eps_w.mat');
eps_w = eps_w.eps_w; %complex number
eps_l = load('eps_lat.mat');
eps_l = eps_l.eps_lat; %complex number
eps_exp = load('eps_exp.mat');
eps_exp = eps_exp.refind_eps; %row of 225 complex numbers
%I am trying to solve the equation below and I know that p_w is real and
%equal or greater than 0.
for x = 1:length(eps_exp)
clear p_w
syms p_w
equation = eps_exp(x) - (p_w.*eps_w.^(1.3) + (1-p_w).*eps_l.^(1/3)).^3 == 0 ;
assume(p_w > 0)
p_water(x,:)= solve(equation, p_w, 'Real' , true)
end
Whenever I run this I get the following message:
"Error using mupadengine/feval (line 195): No complex subexpressions allowed in real mode.
Error in solve (line 293) : sol = eng.feval('solve', eqns, vars, solveOptions);
Error in (...) (line 20): p_water(x,:)= solve(equation, p_w, 'Real', true)"
Does this simply mean that there are no possible real solutions to the equation? From my pov there should be, as can be seen from the easier-to-read version of the equation, below. All epsilon values are complex, so there should be a value for p_w using real numbers.
If I am correct, then why am I getting the error message?
Previous attempts:
I have tried to simply manipulate the equation, as shown in one of the answers below, but I only get complex values when I do so.

Answers (2)

Why don't you simply solve for pw ?
pw = (eps_exp^(1/3) - eps_l^(1/3))/(eps_w^(1/3)-eps_l^(1/3))
Note that you use eps_w^(1.3) in your code, not eps_w^(1/3).

3 Comments

Hi, thanks for your help, unfortunately this doesn't work. I should have specified (sorry, I forgot) but I am solely looking for answers with real numbers, and I have tried solving it like that, but I get complex numbers as an answer.
Is there another way of obtaining a real number as an answer, without using "solve"?
Why should there be real solutions for pw if eps_exp, eps_l and eps_w are complex ? Can you give an example ?
I have data from a measurement, , and I have a model that I am trying to fit in it, the model equation is .
I have quantities (which are attached above) for and I am trying to find which values of could make .
The way I thought of doing this was by basically equalling one to the other, as shown below, and try to get a the value for that will get me the subtraction closest to 0 (or exactly 0 if possible).
I believe that could be a real number, because it is solely a factor and all other terms on the left and right side of the equation are complex numbers.

Sign in to comment.

Next time, please report the real problem right at the beginning.
%loading the constants
eps_w = load('eps_w.mat');
eps_w = eps_w.eps_w; %complex number
eps_l = load('eps_lat.mat');
eps_l = eps_l.eps_lat; %complex number
eps_exp = load('eps_exp.mat');
eps_exp = eps_exp.refind_eps; %row of 225 complex numbers
eps_exp = eps_exp.';
A = [real(eps_w.^(1/3)-eps_l.^(1/3))*ones(size(eps_exp));imag(eps_w.^(1/3)-eps_l.^(1/3))*ones(size(eps_exp))];
b = [real(eps_exp.^(1/3)-eps_l.^(1/3));imag(eps_exp.^(1/3)-eps_l.^(1/3))];
pw = A\b
pw = 0.0771

4 Comments

I don't quite understand, so what you have done was simply separate the real and the imaginary part of the complex number and just divide the real part of the values? Is this so? I am not sure this applies, but I will check it.
Thanks for your help.
If you want pw to be real-valued, you have to do it this way. If pw can be complex-valued, it's different.
In principle, it's the least-squares solution of the 225 equations
pw*(eps_w.^(1/3)-eps_l.^(1/3)) = eps_exp.^(1/3)-eps_l.^(1/3)
where pw is constraint to be real-valued.
Oh, ok I have never used the least-squares, so is this what is happening when you calculate A and b?
Thanks once more.
pw - determined as A\b - minimizes the expression
sum_{i=1}^{i=225} (pw*real(eps_w.^(1/3)-eps_l.^(1/3))-real(eps_exp(i).^(1/3)-eps_l.^(1/3)))^2 +
sum_{i=1}^{i=225} (pw*imag(eps_w.^(1/3)-eps_l.^(1/3))-imag(eps_exp(i).^(1/3)-eps_l.^(1/3)))^2

Sign in to comment.

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Asked:

on 12 Sep 2023

Commented:

on 13 Sep 2023

Community Treasure Hunt

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

Start Hunting!