Clear Filters
Clear Filters

Why not get the exact value of this matlab code?

7 views (last 30 days)
When I run this code, I got a multiplication and division of number. Why not I get the exact value?
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
After execution, I got
Kcp = (1178681804896*exp(-14474901/6326800))/3957057343513 + 17/2500
Why not direct get the exact result?

Answers (1)

Walter Roberson
Walter Roberson on 15 Mar 2022
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
ans =
  1 Comment
Sol Elec
Sol Elec on 16 Mar 2022
Thanks a lot @Walter Roberson for understanding my query.. Yes I wanted the decimal approximation of the solution.

Sign in to comment.


Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!