set default properties of symbolic variables

1 回表示 (過去 30 日間)
Leo Simon
Leo Simon 2016 年 10 月 9 日
コメント済み: Walter Roberson 2016 年 10 月 9 日
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

採用された回答

Walter Roberson
Walter Roberson 2016 年 10 月 9 日
編集済み: Walter Roberson 2016 年 10 月 9 日
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 件のコメント
Leo Simon
Leo Simon 2016 年 10 月 9 日
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 2016 年 10 月 9 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumbers and Precision についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by