필터 지우기
필터 지우기

How to Convert symbolic variables to numeric

조회 수: 283 (최근 30일)
Guendouz walid
Guendouz walid 2023년 1월 25일
댓글: Steven Lord 2023년 1월 25일
Greetings!
How to convert a symbolic expression into a numerical expression?
best regards,
Walid.
  댓글 수: 3
Guendouz walid
Guendouz walid 2023년 1월 25일
Thank you.
Star Strider
Star Strider 2023년 1월 25일
Reference: How to calculate the derivative of vector and specifically my Answer.

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2023년 1월 25일
If it contains no symbolic variable use double.
two = sym(2);
sqrt2 = sqrt(two)
sqrt2 = 
x = double(sqrt2)
x = 1.4142
If it does contain a symbolic variable you can approximate the numeric parts with vpa. You could also use vpa even if it doesn't contain a symbolic variable.
z = vpa(sqrt2)
z = 
1.4142135623730950488016887242097
syms y
polynomialInY = y.^2 + sqrt2*y - 1
polynomialInY = 
vpa(polynomialInY)
ans = 
In this case trying to convert polynomialInY to a double array will error. You could subs a value for y into that expression (to eliminate the symbolic variable) then convert the result.
double(polynomialInY)
Error using symengine
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.

Error in sym/double (line 872)
Xstr = mupadmex('symobj::double', S.s, 0);
  댓글 수: 5
Walter Roberson
Walter Roberson 2023년 1월 25일
It is safest to convert every number to sym() inside a symbolic expression.
In the case of a symbolic expression raised to an integer power, MATLAB can be counted on to work out itself that the power needs to be converted to symbolic... Likewise, symbolic expression divided by an integer is reliably converted. But in both cases, only if the expression is already symbolic.
sym((sym(138)/sym(490))^sym(10)) %safest
ans = 
sym((sym(138)/sym(490))^10) %pretty safe
ans = 
sym((sym(138)/490)^10) %pretty safe
ans = 
sym((138/490)^10) %unsafe !
ans = 
You have to be careful in converting scientific notation
sym(6.02139280e-23) %unsafe !
ans = 
sym(6.02139280)*10^-23 %less unsafe but still inaccurate
ans = 
sym(602139280)*sym(10)^-31 %safe
ans = 
sym(602139280)*10^-23 %looks safe but is not really
ans = 
The 10^-23 in the last example is not internally being handled by 10^-23 being exactly represented as would be the case for sym(10)^-23 . Instead, the 10^-23 is being handled by sym() examining the number being passed in and doing a continued fraction analysis to determine whether it is "sufficiently close to" a "nice number".
Steven Lord
Steven Lord 2023년 1월 25일
The description for the 'r' value for the flag input argument to the sym function lists the forms that it recognizes for "modest-sized integers p and q". This value for the flag input is the default which is why:
x = sym(sqrt(2))
x = 
works to give . 2 counts as "modest-sized".

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Assumptions에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by