如何基于算术运算符MATLAB访问符号表达式 [英] How to access symbolic expression based on arithmetic operators MATLAB

查看:97
本文介绍了如何基于算术运算符MATLAB访问符号表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的符号表达式是众多表达式中的一个示例

The symbolic expression below is one example out of many expressions

expr = x + (x/z)*log(C + x/y);

对于上述表达式,我需要按以下方式解决

for the above expression I need to solve as below

步骤1:

var1 = x/y % accessing expression one operation at a time
result1 = applySomeFunction(var1) 

第2步:

var2 = var1+C
result2 = someConstantValue*result1+ applySomeFunction(var2);

第3步:

var3 = log(var2)
result3 = someConstantValue*result2 + applySomeFunction2(var3);

第四步:

var4 = var3*x
result4 = someConstantValue*result3 + applySomeFunction2(var34);

. . . 直到表达式的结尾.

. . . until the end of expression.

有没有一种基于操作来提取和访问符号子表达式的方法?

Is there a way to extract and access symbolic subExpressions based upon operation?

我尝试将其转换为字符串,但是使用括号时掩盖错误太多,效率不高.

I tried by converting into string but there are so many masking error with usage of parenthesis and not so efficient.

推荐答案

符号变量是使用syms命令定义的.

symbolic variables are defined with syms command.

MATLAB代码:

syms x y z C v1 v2 v3 v4;
v1 = x/y;
v2 = v1+C;
v3 = log(v2);
v4 = v3*x;
.......

或者如果您只需要expr,

or if u just need expr then,

syms x y z C expr;
expr = x + (x/z)*log(C + x/y);   

然后探索这些变量:例如(expr中的x = 1,y = 2,z = 3,C = 4)

And then to explore those variables: e.g (x=1,y=2,z=3,C=4 in expr)

subs(expr,{x,y,z,C},{1,2,3,4});  

这将给出:

expr = 1 + (1/3)*log(9/2);

但是,如果您不想使用MATLAB内置的微分或积分或其他一些符号函数,我建议您改用匿名函数.它们更快,更容易使用.

But I would recommend you use the anonymous function instead if you don't want to use MATLAB inbuilt differentiation or Integration or some other symbolic functions. They are much faster and easy to work with.

expr = @(x,y,z,C) x + (x/z)*log(C + x/y);
expr(1,2,3,4)

这将得出结果:1.501359 ...

This will give result: 1.501359...

这篇关于如何基于算术运算符MATLAB访问符号表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆