Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
742 views
in Technique[技术] by (71.8m points)

matlab - Convert output from symbolic math (sym) to float

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);

The answer is:

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...