如何在Matlab中简化符号和数字混合表达式 [英] How to simplify a symbolic and numeric mixed expression in Matlab
问题描述
我有一个符号和数字混合的表达式:
I have a symbolic and numeric mixed expression:
(3145495418313256125*sin(11334310783410932061962315977/17437937757178560512000000000)*cos(theta))/85568392920039424
其中,theta
是符号变量.我想简化此表达式,以便将所有数字及其数学运算结果都更改为两倍.
where theta
is a symbolic variable. I want to simplify this expression such that all the numeric numbers and their math operation results are changed to double.
推荐答案
就数据类型而言,您不能混合使用浮点数和符号值.但是,您可以使用可变精度算术,以便以十进制形式表示值.使用 vpa
:
In terms of data types, you can't mix floating point and symbolic values. However, you can use variable precision arithmetic so that the values are represented in decimal form. Using vpa
:
syms theta
y1 = (3145495418313256125*sin(11334310783410932061962315977/17437937757178560512000000000)*cos(theta))/85568392920039424
y2 = vpa(y1)
返回
y2 =
22.24607614528243677915796931637*cos(theta)
y2
的数据类型(class
)仍为sym
.请参见 digits
函数来调整有效位数.
The data type (class
) of y2
is still sym
. See the digits
function to adjust the number of significant digits.
如果要在实际浮点中工作,则需要将符号表达式转换为函数.您可以使用名称混乱的 matlabFunction
:自动执行该过程:>
If you want to work in actual floating point you'll need to convert your symbolic expression into a function. You can automate that procedure by using the confusingly-named matlabFunction
:
thetafun = matlabFunction(y1)
使用双精度变量返回函数:
which returns a function using double precision variables:
thetafun =
@(theta)cos(theta).*2.224607614528244e1
然后可以调用匿名函数 thetafun
就像任何功能,例如thetafun(0.5)
.
The anonymous function thetafun
can then be called just like any function, e.g., thetafun(0.5)
.
这篇关于如何在Matlab中简化符号和数字混合表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!