Why can Matlab not determine the sign of the square root of a positive expression?

75 vues (au cours des 30 derniers jours)
MaggieD
MaggieD le 10 Nov 2024 à 15:45
Commenté : Paul le 10 Nov 2024 à 21:06
I am trying to determine the sign of a rather complex expression and was hoping for some help from Matlab's Symbolic Toolbox. However, I encountered some behavior that I cannot really make sense of. Below is an example with a simplified expression to illustrate the issue.
The following works as expected, i.e. Matlab can determine that the square root of a positive expression is positive:
clear all
syms beta c f2 sigma f1 b
d=(1 + (1/beta - c*f2/sigma)*b)^2 - 4*(1/beta + c*f1/sigma)*b;
assume(d>0)
assumptions
isAlways(sqrt(d)>0)
ans = logical
1
However, if I divide instead of multiply by b, Matlab can no longer determine the sign of the square root. This is particularly confusing because Matlab had no additional information regarding b in the first example.
clear all
syms beta c f2 sigma f1 b
d=(1 + (1/beta - c*f2/sigma)/b)^2 - 4*(1/beta + c*f1/sigma)/b;
assume(d>0)
assumeAlso(b>0)
assumptions
isAlways(sqrt(d)>0)
Warning: Unable to prove '0 < (((1/beta - (c*f2)/sigma)/b + 1)^2 - (4/beta + (4*c*f1)/sigma)/b)^(1/2)'.
ans = logical
0
While experimenting around, I also noticed the following: going back to the first example but now assuming explicitly that all variables are real, Matlab again can no longer determine the sign of the square root.
clear all
syms beta c f2 sigma f1 b real
d=(1 + (1/beta - c*f2/sigma)*b)^2 - 4*(1/beta + c*f1/sigma)*b;
assumeAlso(d>0)
assumptions
isAlways(sqrt(d)>0)
ans = logical
1
What am I missing here?
Edit: I realized that I was overwriting the assumptions that all variables are real in the last example. Fixing that and going over it again, I am even more confused. Example 3 works out fine, however, when taking out the b, we end up with the issue I originally meant to illustrate, i.e. once I add the assumption that all variables are real, Matlab fails to determine the sign. Now the question regarding this point is two-fold: why does the assumption that all variables are real prevent Matlab to determine the sign? And why does this issue only arise if I take out the b?
clear all
syms beta c f2 sigma f1 b real
d=(1 + (1/beta - c*f2/sigma))^2 - 4*(1/beta + c*f1/sigma);
assumeAlso(d>0)
assumptions
isAlways(sqrt(d)>0)
Warning: Unable to prove '0 < ((1/beta - (c*f2)/sigma + 1)^2 - 4/beta - (4*c*f1)/sigma)^(1/2)'.
ans = logical
0
  5 commentaires
Walter Roberson
Walter Roberson le 10 Nov 2024 à 20:24
Note that clear all does not clear assumptions on variables.
You can reset assumptions using assume(EXPRESSION,'clear') . Or reset(symengine)
Paul
Paul le 10 Nov 2024 à 21:06
clear all should and used to clear all the assumptions. But it doesn't anymore in 2024b and in some previous releases.
syms t real
assumptions
disp(char(ans))
in(t, 'real')
clear all
assumptions
disp(char(ans))
in(t, 'real')
This behavior is exactly counter to assumptions - Tips
  • To clear all objects in the MATLAB workspace and close the Symbolic Math Toolbox™ engine associated with the MATLAB workspace resetting all its assumptions, use this command.
clear all
Code that relies on this documented behavior is possibly broken. Tech Support has already confirmed it's an issue (which to me sounds like a bug), as previously discussed in this thread.
To clear all assumptions, I do
assume(assumptions,'clear');
assumptions
ans = Empty sym: 1-by-0
disp(char(ans))
[]

Connectez-vous pour commenter.

Réponses (1)

Umar
Umar le 10 Nov 2024 à 20:24

Hi @MaggieD,

In MATLAB's Symbolic Toolbox, assumptions play a crucial role in how expressions are evaluated. They help define the domain and properties of symbolic variables, influencing the results of operations performed on them. The behavior you are observing stems from how MATLAB interprets these assumptions in relation to real numbers and divisions. When you declare a variable as real (e.g., syms b real), MATLAB restricts its analysis to real values only. This means that any expressions involving these variables must also adhere to real number properties. In your case, when b is included in a multiplication scenario, it does not pose any ambiguity since multiplying two positive expressions will yield a positive result. However, when you divide by b, even under the assumption that b > 0, MATLAB cannot automatically deduce that the resultant expression remains positive without further information about how other terms behave—especially since division can introduce complexities not present in multiplication. The change from multiplication to division alters the nature of how expressions are evaluated:

Multiplication Example:

    d=(1 + (1/beta - c*f2/sigma)*b)^2 - 4*(1/beta + c*f1/sigma)*b;

Here, if d > 0, it can be inferred that sqrt(d)>0 under your assumption.

Division Example:

     d=(1 + (1/beta - c*f2/sigma)/b)^2 - 4*(1/beta + c*f1/sigma)/b;

This introduces potential for d to approach zero or even become negative depending on values of other variables, thus complicating the determination of sqrt(d).

Why Removing b Affects Behavior

When you remove b from your expression and still assume all variables are real, it can lead to undefined or ambiguous conditions in certain mathematical contexts, the expression:

   d=(1 + (1/beta - c*f2/sigma))^2 - 4*(1/beta + c*f1/sigma);

may still yield conditions where positivity isn't guaranteed because the absence of b removes a multiplicative factor that could ensure positivity under certain conditions. Thus, without additional constraints or specific values for your parameters, MATLAB cannot conclude definitively about the sign.

In my opinion, the inherent complexity when dealing with symbolic expressions highlights a limitation in automatic reasoning within MATLAB's symbolic engine. It’s often beneficial to manually analyze critical points or ranges for parameters involved. My suggestion would be that you might consider breaking down your expressions into simpler components and using isAlways with specific assumptions on individual components rather than relying solely on global assumptions. As you continue experimenting, try gradually building up your expressions and assumptions, checking results at each step. This can help pinpoint where ambiguity arises.

In nutshell, understanding how symbolic assumptions interact with different operations is key in leveraging MATLAB’s capabilities effectively.

Hope, this helps.

If further clarification or assistance is needed with specific examples or additional concepts, feel free to ask!

Tags

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by