set default properties of symbolic variables
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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 个评论
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 Exchange 中查找有关 Symbolic Variables, Expressions, Functions, and Preferences 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!