将符号数学(sym)的输出转换为浮点数 [英] Convert output from symbolic math (sym) to float

查看:1437
本文介绍了将符号数学(sym)的输出转换为浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于这个问题,但我相信它更通用。

我使用Matlab的符号数学工具箱来解决一个方程:

My question is similar to this question but I believe it to be more general.
I use Matlab's symbolic math toolbox to solve an equation:

MAZ = 0.5;
MAU = 1.0;
XI = 1.0;
ALPHA = 2.0;

DRG = 0.5;
SRG = 1.0;

PHI = 1 / (2 * MAU);

syms L;

f = 1 - DRG - sqrt(1 + (ALPHA * XI - L / (2 * XI * PHI) ) ^ 2 ) / ...
    sqrt(1 + (ALPHA * XI) ^ 2) + L / (4 * PHI * SRG * sqrt(1 + (ALPHA * XI)^2));

a = solve(f,L,'Real',true);

答案是:

a =
5^(1/2)/3 + (10*((4*5^(1/2))/25 + 6/25)^(1/2))/3 + 8/3
5^(1/2)/3 - (10*((4*5^(1/2))/25 + 6/25)^(1/2))/3 + 8/3  

如何自动将这些表达式转换为一个 - 不包含任何符号表达式 - 浮动,以便稍后在代码中访问它们。

How do I automatically convert these expressions for a - which do not contain any symbolic expressions - to floats so that I can access them later in my code?

推荐答案

Matlab的符号数学工具箱包含一个名为 double

此函数转换 solve 函数的结果,这是一个 sym - 加倍:

Matlab's symbolic math toolbox contains a function called double.
This function converts the result of the solve function - which is a sym - to a double:

double(a)

ans =

      5.98921078320145
     0.834834535131742

这篇关于将符号数学(sym)的输出转换为浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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