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 CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by