Solving non-linear integral system with complex numbers
5 views (last 30 days)
Show older comments
Eggstrema
on 10 Oct 2021
Edited: Walter Roberson
on 11 Oct 2021
I am trying to solve this nonlinear system of equations numerically:
with the following code:
% param
W = 1e-3;
S = 0.5e-3;
t = 0.1e-3;
% initial guess
a0 = W/2;
b0 = W/2+t/pi;
c0 = W/2+S-t/pi;
d0 = W/2+S;
s0 = [a0,b0,c0,d0];
% eqs and solve
fun1 = @(s) integral(@(z) sqrt((z.^2-s(1).^2).*(z.^2-s(4).^2)./((z.^2-s(2).^2).*(z.^2-s(3).^2))),-s(1),s(1))-W;
fun2 = @(s) integral(@(z) sqrt((z.^2-s(1).^2).*(z.^2-s(4).^2)./((z.^2-s(2).^2).*(z.^2-s(3).^2))),s(1),s(2))+1i*t/2;
fun3 = @(s) integral(@(z) sqrt((z.^2-s(1).^2).*(z.^2-s(4).^2)./((z.^2-s(2).^2).*(z.^2-s(3).^2))),s(2),s(3))-S;
fun4 = @(s) integral(@(z) sqrt((z.^2-s(1).^2).*(z.^2-s(4).^2)./((z.^2-s(2).^2).*(z.^2-s(3).^2))),s(3),s(4))-1i*t/2;
s = fsolve(@(s) [fun1(s),fun2(s),fun3(s),fun4(s)],s0)
However, I am getting unexpected results because the relationship of the solutions should be and that they are all real-valued. The command line also tells me that that "equation solved, solver stalled" or "equation solved, possible inaccuracies" depending on my input parameters W, S, and t. Any help would be appreciated, thanks!
2 Comments
Walter Roberson
on 11 Oct 2021
I just realized that you do not appear to have stated that the s values are strictly positive. You said they are real-valued and s1 < s2 < s3 < s4, but is it possible for them to be negative?
Accepted Answer
Walter Roberson
on 10 Oct 2021
Edited: Walter Roberson
on 10 Oct 2021
fsolve() is completely unable to handle constraints.
format long g
% param
W = 1e-3;
S = 0.5e-3;
t = 0.1e-3;
% initial guess
a0 = W/2;
b0 = W/2+t/pi;
c0 = W/2+S-t/pi;
d0 = W/2+S;
s0 = [a0,b0,c0,d0];
% eqs and solve
fun1 = @(s) integral(@(z) sqrt((z.^2-s(1).^2).*(z.^2-s(4).^2)./((z.^2-s(2).^2).*(z.^2-s(3).^2))),-s(1),s(1))-W;
fun2 = @(s) integral(@(z) sqrt((z.^2-s(1).^2).*(z.^2-s(4).^2)./((z.^2-s(2).^2).*(z.^2-s(3).^2))),s(1),s(2))+1i*t/2;
fun3 = @(s) integral(@(z) sqrt((z.^2-s(1).^2).*(z.^2-s(4).^2)./((z.^2-s(2).^2).*(z.^2-s(3).^2))),s(2),s(3))-S;
fun4 = @(s) integral(@(z) sqrt((z.^2-s(1).^2).*(z.^2-s(4).^2)./((z.^2-s(2).^2).*(z.^2-s(3).^2))),s(3),s(4))-1i*t/2;
residue = @(s) norm([fun1(s), fun2(s), fun3(s), fun4(s)]);
A = [1 -1 0 0; 0 1 -1 0; 0 0 1 -1]; b = eps*ones(3,1);
Aeq = []; beq = [];
lb = zeros(1,4); ub = [];
[bests, fval] = fmincon(residue, s0, A, b, Aeq, beq, lb, ub);
string(bests)
string(fval)
4 Comments
Walter Roberson
on 11 Oct 2021
Edited: Walter Roberson
on 11 Oct 2021
I realized that the integration parts of fun2 and fun4 produced pure real numbers times sqrt(-1), and then you have a pure imaginary part being added to that. That is equivalent to sqrt(-1) times (sum of two real). When you norm() that, then the sqrt(-1) disappears . instead of trying to integrate with complex parts, you can integrate the negative of the parts (inside the sqrt) and remove the 1i* from the constant, transforming the two parts into pure real, which is easier to deal with.
In the below, I am not certain why I am getting imaginary parts in the back-substitutions, especially since fun3 should be sqrt(positive*negative / (negative * positive)) which should simplify to sqrt(positive) with no possibility for complex parts.
If you set digits to 18 or more, then the vpa() in the back substitution fails, implying that it could not meet precision goals. I do not know at the moment why that would be happening.
When you compare fval here the previous, remember that the previous is sqrt() relative to this one -- the one below is equivalent to norm^2
W = 1e-3;
S = 0.5e-3;
t = 0.1e-3;
% initial guess
a0 = W/2;
b0 = W/2+t/pi;
c0 = W/2+S-t/pi;
d0 = W/2+S;
s0 = [a0,b0,c0,d0];
% eqs and solve
fun1 = @(s) int(@(z) sqrt((z.^2-s(1).^2).*(z.^2-s(4).^2)./((z.^2-s(2).^2).*(z.^2-s(3).^2))),-s(1),s(1))-W;
fun2 = @(s) int(@(z) sqrt(-(z.^2-s(1).^2).*(z.^2-s(4).^2)./((z.^2-s(2).^2).*(z.^2-s(3).^2))),s(1),s(2))+t/2;
fun3 = @(s) int(@(z) sqrt((z.^2-s(1).^2).*(z.^2-s(4).^2)./((z.^2-s(2).^2).*(z.^2-s(3).^2))),s(2),s(3))-S;
fun4 = @(s) int(@(z) sqrt(-(z.^2-s(1).^2).*(z.^2-s(4).^2)./((z.^2-s(2).^2).*(z.^2-s(3).^2))),s(3),s(4))-t/2;
residue = @(s) fun1(s).^2 + fun2(s).^2 + fun3(s).^2 + fun4(s).^2;
syms S [1 4] real
R = matlabFunction(residue(S), 'vars', {S})
A = [1 -1 0 0; 0 1 -1 0; 0 0 1 -1]; b = -realmin*ones(3,1);
Aeq = []; beq = [];
lb = zeros(1,4); ub = [];
[bests, fval] = fmincon(R, s0, A, b, Aeq, beq, lb, ub)
string(bests)
digits(17)
vpa(fun1(sym(bests)))
vpa(fun2(sym(bests)))
vpa(fun3(sym(bests)))
vpa(fun4(sym(bests)))
string(ans)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!