set default properties of symbolic variables

4 Ansichten (letzte 30 Tage)
Leo Simon
Leo Simon am 9 Okt. 2016
Kommentiert: Walter Roberson am 9 Okt. 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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 9 Okt. 2016
Bearbeitet: Walter Roberson am 9 Okt. 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 Kommentare
Leo Simon
Leo Simon am 9 Okt. 2016
Thanks @Walter Roberson. I couldn't make it NNsym work. Would you mind posting an MWE for NNsym please? I'm afraid I don't understand the key-word part.
Walter Roberson
Walter Roberson am 9 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by