Why not get the exact value of this matlab code?
8 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
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)
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)
P = diff(Cp,L); % dCp/dL
Kcp = subs(P,{L},{L_ref})
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Special Values 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!