What codes can be used to retain the decimals before the defined symbolic variable

3 views (last 30 days)
Hi there,
I want to ask, what codes can be used to retain the decimals before the defined symbolic variable? For instance, a symbolic result
0.0029068360163364559412002563476562*dis_r1 - 0.005666737824361646885806820250764*dis_r2 + 0.056999838488600009997475659418*dis_r3 – 0.001770474043114554640876434632446*dis_t2 - 0.045184740924660363181608602189954*dis_t3 + 6.4455841527204101204420278888018e-37*for_q1 - 0.03001164382667592667175426868198*for_q2
has been obtained, where dis_r1, dis_r2, dis_r3, for_q1, and for_q2 are defined symbolic variables. I want to keep three decimals before them.
Many thanks!
  1 Comment
Walter Roberson
Walter Roberson on 18 May 2025
I am seeing oddities,
ttt = sym('0.0029068360163364559412002563476562')
ttt = 
0.0029068360163364559412002563476561999999999999898364193126146969365354139543467646154929826959060832116354844314835836893706755290622822940349578857421875
length(char(ttt))
ans = 156
Except most of the time it returns something of length 104, or 136, but sometimes 36...

Sign in to comment.

Accepted Answer

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 18 May 2025
Here is one of the viable options with digits() and vpa():
syms dis_r1 dis_r2 dis_r3 for_q1 for_q2
dis_t2 = 1;
dis_t3 = 2;
% Equation:
EQN = 0.0029068360163364559412002563476562*dis_r1 - 0.005666737824361646885806820250764*dis_r2 +...
0.056999838488600009997475659418*dis_r3 - 0.001770474043114554640876434632446*dis_t2 - ...
0.045184740924660363181608602189954*dis_t3 + 6.4455841527204101204420278888018e-37*for_q1 -...
0.03001164382667592667175426868198*for_q2;
% Solve the equation:
Solution = solve(EQN, dis_r1);
% Display the solution:
disp(Solution);
%% This is how to set up specific decimal places!
% Set the desired number of decimal places
Precision = 5; % The number of decimal places
digits(Precision); % Set the global precision
% Convert solution to variable precision:
VPA_Solution = vpa(Solution);
% Compare Solution vs. VPA_Solution
disp(VPA_Solution)

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!