Matlab中变量的符号声明

发布于 2024-12-02 13:45:32 字数 250 浏览 1 评论 0原文

我想将我的变量编写为其他变量之间的操作。

例如,如果我输入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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼前雾蒙蒙 2024-12-09 13:45:32

您可以使用符号工具箱来完成此操作。下面是一个例子:

syms a b c %# declare a b c to be symbolic variables
a = b + c;

b=3;c=4; %# now set values for b and c
eval(a)  %# evaluate the expression in a

ans =

    7

b=5;c=9; %# change the values of b and c
eval(a)

ans =

    14

所以 a 的定义仍然是 b + c (您可以通过在命令窗口中键入 a 来检查这一点),并且何时您使用eval对其进行评估,它使用bc的当前值来计算a。请注意,bc 不再是符号变量,而是转换为双精度数。然而,a 仍然是,并且定义成立,因为默认情况下,符号变量中的表达式保持未计算状态。

You can do this using the symbolic toolbox. Here's an example:

syms a b c %# declare a b c to be symbolic variables
a = b + c;

b=3;c=4; %# now set values for b and c
eval(a)  %# evaluate the expression in a

ans =

    7

b=5;c=9; %# change the values of b and c
eval(a)

ans =

    14

So the definition of a is still b + c (you can check this by typing a at the command window) and when you evaluate it using eval, it uses the current value of b and c to calculate a. Note that b and c are no longer symbolic variables and are converted to doubles. However a still is, and the definition holds because by default, the expressions in symbolic variables are held unevaluated.

烙印 2024-12-09 13:45:32

如果您没有符号工具箱,则可以使用匿名函数来实现类似的结果。

b=2; c=4; 
a=@()(evalin('caller','b+c')); 
a(), 

ans =

     6

b=1; 

a()


ans =

     5

并不理想,但可能会有所帮助。

您可以使用以下函数使这变得更容易:

function [ anonFunction ] = AnonEval( expression )
%AnonEval Create an anonymous function that evaluates an expression
   anonFunction = @()(evalin('caller',expression)); 
end

b=2,c=4, 
a=AnonEval('b+c'); 
a(),
b=1; 
a()

If you don't have the symbolic toolbox, you could use an anonymous function to achieve a similar result.

b=2; c=4; 
a=@()(evalin('caller','b+c')); 
a(), 

ans =

     6

b=1; 

a()


ans =

     5

Not ideal but may be helpful.

You could make this easier with the following function:

function [ anonFunction ] = AnonEval( expression )
%AnonEval Create an anonymous function that evaluates an expression
   anonFunction = @()(evalin('caller',expression)); 
end

b=2,c=4, 
a=AnonEval('b+c'); 
a(),
b=1; 
a()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文