set default properties of symbolic variables
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Leo Simon
le 9 Oct 2016
Commenté : Walter Roberson
le 9 Oct 2016
Whenever I use the symbolic toolbox, I always want my symbolic variables to be real and non-negative. It's annoying to have to make these specifications each time. Is there any way to set default properties of symbolic variables in startup.m?
Thanks!
Leo
0 commentaires
Réponse acceptée
Walter Roberson
le 9 Oct 2016
Modifié(e) : Walter Roberson
le 9 Oct 2016
No.
At the end of a "syms" you can add the keyword "real" to add the assumption of real-valued to each variable mentioned. You can also add "positive" to add the assumption of positive to each variable mentioned. However, there is no corresponding flag to syms for "nonnegative", so you will need to assume() for that.
You could write a small function
function NNsyms(varargin)
for K = 1 : nargin
evalin('caller', sprintf('syms %s; assume(%s >= 0)', varargin{K}, varargin{K});
end
and then use 'NNsyms' everywhere you would otherwise use syms where you wanted the nonnegative assumption.
I poked into the code and found that you would have to modify a couple of Mathworks-provided routines to support this assumption by way of a key word. It would not be bad, though. Expand some cell arrays used for checking, modify setToMuPADSet.m slightly to add a mapping to
'Dom::Interval([0], infinity)'
where the [] around the 0 is important.
2 commentaires
Walter Roberson
le 9 Oct 2016
Corrected code:
function NNsyms(varargin)
for K = 1 : nargin
evalin('caller', sprintf('syms %s; assume(%s >= 0)', varargin{K}, varargin{K}));
end
To use, you would for example
NNsyms z
after which you would find
>> assumptions(z)
ans =
0 <= z
The bit about keywords was in case someone got energetic enough to modify the source for syms and assume and setToMuPADSet to permit a syntax such as
syms q nonnegative
It is information about what would have to be changed in the Mathworks code. The NNsyms should work once the missing ) is added.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Symbolic Variables, Expressions, Functions, and Preferences dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!