필터 지우기
필터 지우기

Why not get the exact value of this matlab code?

조회 수: 1 (최근 30일)
Sol Elec
Sol Elec 2022년 3월 15일
댓글: Sol Elec 2022년 3월 16일
When I run this code, I got a multiplication and division of number. Why not I get the exact value?
B=1;
L_ref=7.8285;
syms L
Li=1/(1/(L+0.08*B)-0.035/(B^3+1)); % % 1/Li = 1/(L + 0.08 * B) - 0.035/(B^3 + 1)
Cp=0.5176*(116/Li-0.4*B-5)*exp(-21/Li)+0.0068*L; % Cp = 0.5176((116/Li - 0.4 * B - 5)*exp(-21/Li) + 0.0068 * L)
P=diff(Cp,L); % dCp/dL
Kcp=subs(P,{L},{7.8285});
After execution, I got
Kcp = (1178681804896*exp(-14474901/6326800))/3957057343513 + 17/2500
Why not direct get the exact result?

답변 (1개)

Walter Roberson
Walter Roberson 2022년 3월 15일
Why does it not get the exact value? Perhaps because you did not enter exact coefficients.
What is the exact value of 7.8285 ? Is that number exactly representable in a binary floating point system?
In science, when you write 7.8285 you mean that the value is some value that is not precisely known, but is known to be within the range 782845/10000 inclusive and 782855/10000 exclusive. When you have an uncertain input, it makes no sense to ask for an exact output.
Q = @(v) sym(v);
B = Q(1);
L_ref = Q(78285)/Q(10000);
syms L
Li = Q(1)/(Q(1)/(L+Q(8)/Q(100)*B)-Q(35)/Q(1000)/(B^Q(3)+Q(1))) % % 1/Li = 1/(L + 0.08 * B) - 0.035/(B^3 + 1)
Li = 
Cp =Q(5176)/Q(10000)*(Q(116)/Li-Q(4)/Q(10)*B-Q(5))*exp(-Q(21)/Li)+Q(68)/Q(10000)*L % Cp = 0.5176((116/Li - 0.4 * B - 5)*exp(-21/Li) + 0.0068 * L)
Cp = 
P = diff(Cp,L); % dCp/dL
Kcp = subs(P,{L},{L_ref})
Kcp = 
If we intepret each of your floating point numbers as-if it designated an indefinitely precise rational number in base 10, then the above is the "exact" solution.
I have the suspicion, however, that you never wanted the exact solution. I suspect that you wanted the decimal approximation of the solution:
format long g
double(Kcp)
ans =
0.0370283589246007
  댓글 수: 1
Sol Elec
Sol Elec 2022년 3월 16일
Thanks a lot @Walter Roberson for understanding my query.. Yes I wanted the decimal approximation of the solution.

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

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by