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

조회 수: 3 (최근 30일)
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
Walter Roberson
Walter Roberson 2025년 5월 18일
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...

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

채택된 답변

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2025년 5월 18일
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)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by