Matlab中变量的符号声明
我想将我的变量编写为其他变量之间的操作。
例如,如果我输入a = c + b
,那么a保留的值就是c和b之间的和运算的数值结果。
如果c = 4且b = 2,则a保留的值为6。
但我希望a保留符号表达式而不是数值。 每次我在命令窗口中写入a时,matlab都会捕获worspace变量的c的数值和b的数值并对它们求和。
通常,如果您编写 a,matlab 会显示该变量中的数值。 有谁知道该怎么做?
I would like to write my variables as operations between other variables.
For instance if I put a = c + b
then value that a keeps inside is the numeric result of the operation of a sum between c and b.
If c = 4 and b = 2 then, the value that a keeps is 6.
But I would like that a keeps the symbolic expression instead of the numeric value.
and every time I write a in the command windows, matlab cacht the numeric value of c and the numeric value of b of the worspace variable and a sum them.
Normally if you write a, matlab displays the numeric value that is in this variable.
Does anyone know how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用符号工具箱来完成此操作。下面是一个例子:
所以
a
的定义仍然是b + c
(您可以通过在命令窗口中键入a
来检查这一点),并且何时您使用eval
对其进行评估,它使用b
和c
的当前值来计算a
。请注意,b
和c
不再是符号变量,而是转换为双精度数。然而,a 仍然是,并且定义成立,因为默认情况下,符号变量中的表达式保持未计算状态。You can do this using the symbolic toolbox. Here's an example:
So the definition of
a
is stillb + c
(you can check this by typinga
at the command window) and when you evaluate it usingeval
, it uses the current value ofb
andc
to calculatea
. Note thatb
andc
are no longer symbolic variables and are converted to doubles. Howevera
still is, and the definition holds because by default, the expressions in symbolic variables are held unevaluated.如果您没有符号工具箱,则可以使用匿名函数来实现类似的结果。
并不理想,但可能会有所帮助。
您可以使用以下函数使这变得更容易:
If you don't have the symbolic toolbox, you could use an anonymous function to achieve a similar result.
Not ideal but may be helpful.
You could make this easier with the following function: