How to tell vpa command to consider digits after decimal point' only?

조회 수: 3 (최근 30일)
Mohammad Shojaei Arani
Mohammad Shojaei Arani 2021년 12월 28일
답변: Walter Roberson 2021년 12월 28일
Hello friends,
I have a simple question, I think! I have a symbolic expression S with numeric coefficients. I need to round the coefficients
up to some digits 'AFTER DECIMAL POINT' only (note that I do not know, beforehand, how many digits my numbers have, so, I want to keep all digits before decimal point and a few after that). So, if I use vpa(S,4) then it only considers 4 digits in total and does not respect my what I want. Let me use a simple example as bellow:
syms x y
f=34344.4545*x+45.6566*y;
now, vpa(S,4) gives me
34340.0*x + 45.66*y
and this is not what I want since the first coefifcient is badly rounded. Of course, in this exmple I could use vpa(S,8) to get 34344.454*x + 45.6566*y but please note that "I do not know, beforehand, how many digits my coefficients have in general".
Any idea?
Thanks in advance!
Babak

답변 (1개)

Walter Roberson
Walter Roberson 2021년 12월 28일
syms x y
f = 34344.4545*x+45.6566*y;
newF = mapSymType(f, 'constant', @(v) round(v,4))
newF = 

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by