Round coefficients of symbols

22 ビュー (過去 30 日間)
Brian
Brian 2013 年 6 月 19 日
回答済み: Carlos 2022 年 12 月 8 日
I have a solution matrix with equations and coefficients in front of symbols. I want to round them to a certain decimal.
example: X=[1.89545464564*S+0.00000085*M, 1.00000055*S-0.68129354234*M; 0.00000000345*S+0.00000346*M, 1.00004353*S+1.68129354234*M];
How to round the coefficients that are in front of S and M in my matrix to something like 2 or 3 decimal places.
Thank you!

回答 (4 件)

Andrei Bobrov
Andrei Bobrov 2013 年 6 月 19 日
syms M S
X=[1.89545464564*S+0.00000085*M, 1.00000055*S-0.68129354234*M; 0.00000000345*S+0.00000346*M, 1.00004353*S+1.68129354234*M];
out = vpa(X,4);

Brian
Brian 2014 年 11 月 6 日
That is close, but not quite what I was looking for. The output of your code provided is:
--------------------------------------------------
out = [ 0.00000085*M + 1.895*S, 1.0*S - 0.68129354234042693860828876495361*M;
0.00000346*M + 0.00000000345*S, 1.681*M + 1.0*S]
--------------------------------------------------
I want the 0.00000085 and similar numbers to just say 0, disappear, or say 0.000*S. Also I'm unsure why the 0.68129354234 coefficient does not get rounded. It does round if the sign were positive instead of negative.
My desired output would be:
--------------------------------------------------
out = [ 1.895*S + 0.000*M, 1.000*S - 0.6813*M;
0.000*S + 0.000*M, 1.000*S + 1.681*M]
_______________________________
ANSWER:
I used Digits(4) at the top of the code. This was the best I could do. Rounded everything to 4 significant figures. I was already using vpa earlier in the code as well.
  1 件のコメント
Manuela Sucerquia
Manuela Sucerquia 2020 年 11 月 24 日
Just what i needed! Thanks <3

サインインしてコメントする。


Image Analyst
Image Analyst 2014 年 11 月 7 日
Use the second argument for round(). From the (R2014b) help for round():
Y = round(X,N) rounds to N digits:
N > 0: round to N digits to the right of the decimal point.
N = 0: round to the nearest integer.
N < 0: round to N digits to the left of the decimal point.
  1 件のコメント
amin ya
amin ya 2019 年 7 月 8 日
These are symbolic variables. round doesnot work for syms

サインインしてコメントする。


Carlos
Carlos 2022 年 12 月 8 日
Hope it's not too late:
syms M S
X=vpa([1.89545464564*S+0.00000085*M, 1.00000055*S-0.68129354234*M; 0.00000000345*S+0.00000346*M, 1.00004353*S+1.68129354234*M]);
for i = 1:length(X)
Coef = coeffs(X(i));
for j = 1:length(Coef)
if Coef(j) < 0.001 %you can change 0.001 to adjust precision
X(i) = subs(X(i),Coef(j),0);
end
end
end
X = vpa(X,5) %use vpa becouse the function subs adds unwanted decimals.

カテゴリ

Help Center および File ExchangeConversion 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