Clear Filters
Clear Filters

How does the function "solve" deal with infinite solutions? Why can't it show all the solutions?

3 views (last 30 days)
Thanks for reading!
I read some similar quetions. But none of the answers give the reason behind the chosen terms.
clear;
clc;
syms x y;
eq = sin(x)-sin(y);
st = [x -x+2*pi y -y+2*pi];
sol = solve([eq,st>=0],'Real',true);
sol.x
ans = 
sol.y
ans = 
It makes me very curious about why solve function choose these two terms from all the infinite solutions. Is it a random action? At first, I assume the result might be (0,0) and (2pi,2pi) since it would be more reasonable if the ends are chosen(at least it's not random).
In fact, there would also be problem even there is only finite solusions:
clear;
%%
syms f(x1,x2)
f(x1,x2)=sin(x1).^2+sin(x2).^2;
fx1(x1,x2)=diff(f,x1);
fx2(x1,x2)=diff(f,x2);
g1(x1,x2) = x1;
g2(x1,x2) = -x1+pi*2;
g3(x1,x2) = x2;
g4(x1,x2) = -x2+pi*2;
g=[g1,g2,g3,g4];
%klisi KKT
[x1_sol,x2_sol]=solve([fx1,fx2,g>=0],[x1,x2],'Real',true)
x1_sol = 
x2_sol = 
Clearly, (pi,pi) should also be one of the solutions, so the solution is not enough. In fact, there should be 25 solutions as I can see in the following figure.
In fact, this question comes from KKT conditions. I tried to solve KKT equations by the function "solve" and find it doesn't work when there are infinite solutins. And it can't show the finite answers completely, neither.
If you have tools for solving KKT equation better, please refet tothis question.
Thank you!

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 13 Jun 2023
Edited: Dyuman Joshi on 13 Jun 2023
"Why can't it show all the solutions?"
It does, but that requires proper syntax
syms x y
eq = sin(x)-sin(y);
st = [x -x+2*pi y -y+2*pi];
[solx,soly,params,cons] = solve([eq st>=0],[x y],'ReturnConditions',true)
solx = 
soly = 
params = 
cons = 
There are 2 pairs of solutions and the conditions on the parameters are described in cons.
If you want to check if a particular combination of k and z are valid to obtain a solution, define them as symbolic variables and substitute them in cons to check if the conditions are satisfied or not -
syms k z
simplify(subs(cons,[k z], [1 0]))
ans = 
As you can see that k=1, z=0 doesn't satisfy both the conditions, and can not be used to obtain a solution.
"It makes me very curious about why solve function choose these two terms from all the infinite solutions. Is it a random action?"
From the documentation of solve (in the Tips section, point 3) - If the solution contains parameters and ReturnConditions is true, solve returns the parameters in the solution and the conditions under which the solutions are true. If ReturnConditions is false, the solve function either chooses values of the parameters and returns the corresponding results, or returns parameterized solutions without choosing particular values.
Note the underlined part. The basis of the choice is not mentioned in the documenatation, you might find that information in the code of solve(). (See type or open)
Though I am not sure why solve() only returns 4 pairs of solutions when ('Real',true) is specified. It returns all the solutions when no Name-Value Argument is specified.
syms x1 x2
f(x1,x2)=sin(x1).^2+sin(x2).^2;
fx1(x1,x2)=diff(f,x1);
fx2(x1,x2)=diff(f,x2);
g1(x1,x2) = x1;
g2(x1,x2) = -x1+pi*2;
g3(x1,x2) = x2;
g4(x1,x2) = -x2+pi*2;
g=[g1,g2,g3,g4];
%klisi KKT
%solve call with no Name-Value Argument
[x1_sol,x2_sol]=solve([fx1,fx2,g>=0],[x1,x2])
x1_sol = 
x2_sol = 
numel(x1_sol)
ans = 25

More Answers (0)

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!