Message Boards Message Boards

0
|
8182 Views
|
5 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Avoid a calculation precision error?

Posted 6 years ago

While using Mathematica to solve a problem in one of my course, I found a problem which might be related to the precision of calculation. Please see the attached code.The function of L[T] becomes discontinuous at a specific range, and then go negative. However, according to the expression of L[T], it shouldn't go negative at any time, and it should be continuous at all range.I think it might be related to the calculation precision, but I don't know how to fix it.

Attachments:
5 Replies

This will give some estimate of the precision loss (as long as it is less than 100 digits):

With[{f = SetPrecision[L[T], Infinity]},
 Plot[100 - Precision[f], {T, 20, 100}, WorkingPrecision -> 100, 
  GridLines -> {None, {$MachinePrecision}},
  Frame -> True, FrameLabel -> {"T", "Precision loss"}]
 ]

enter image description here

Above the horizontal line is approximately where one would expect the error likely to be greater than the function value. The OP's graph starts to become fuzzy at about the spot where the graph above crosses the line, although full precision loss does not appear until a little while later after T == 80.

enter image description here

POSTED BY: Michael Rogers

There are numerical oscillations of the function -1+Sqrt[1-x] at x->0. It is possible to use the series expansion and retain the first few terms of the series (in our example five)

(*parameter set*)
T0 = 50;
deltaHT0 = 200000;
deltaCp = 3000;
deltaHL = -10000;
Pt = 0.0001;
Lt = 0.00005;
KLT = 10^4;
R = 1.987;
f[x_] := -(x/2) - x^2/8 - x^3/16 - (5 x^4)/128 - (7 x^5)/256
K[T_] := Exp[(-deltaHT0/R)*(1/(T + 273.15) - 
      1/(T0 + 273.15)) + (deltaCp/
      R)*(Log[(T + 273.15)/(T0 + 273.15)] + (T0 + 273.15)/(T + 
         273.15) - 1)]
KL[T_] := KLT*Exp[(-deltaHL/R)*(1/(T + 273.15) - 1/(T0 + 273.15))]
b[T_] := 1 + K[T] + KL[T]*(Pt - Lt)
c[T_] := -Lt*(1 + K[T])
L[T_] := b[T]*f[4*KL[T]*c[T]/b[T]^2]/(2*KL[T])
(*track the results*)
{ Plot[{L[T]}, {T, 20, 100}, PlotLabel -> "L[T]", PlotRange -> All, 
PerformanceGoal -> "Quality"],
Plot[{L[T]}, {T, 78, 86}, PlotLabel -> "L[T]", PlotRange -> All, 
PerformanceGoal -> "Quality"],
Plot[{KL[T]}, {T, 78, 86}, PlotLabel -> "KL[T]"],
Plot[{c[T]}, {T, 78, 86}, PlotLabel -> "c[T]"],
Plot[{b[T]}, {T, 78, 86}, PlotLabel -> "b[T]"]}

Fig

Attachments:

Remember only exact numers have ininity precision(in Mathematica).

{Precision[Pi], Precision[2 + 3/2], Precision[273.15]}
(* {\[Infinity], \[Infinity], MachinePrecision} *)

You can set precision by SetPrecision function.

{Precision[SetPrecision[273.15, 10]], Precision[SetPrecision[273.15, 100]]}
(* {10., 100.}*)
Attachments:
POSTED BY: Mariusz Iwaniuk

It's most likely caused by cancellation error. To avoid this one can define all constants using exact values, then do the plot using non-machine precision so as to track and better control the error. Could all be done as below.

(*parameter set*)
T0 = 50;
deltaHT0 = 200000;
deltaCp = 3000;
deltaHL = -10000;
Pt = 1/10000;
Lt = 5/100000;
KLT = 10^4;
R = 1987/1000;
nn = 27315/100;

K[T_] = Exp[(-deltaHT0/R)*(1/(T + nn) - 1/(T0 + nn)) + (deltaCp/
       R)*(Log[(T + nn)/(T0 + nn)] + (T0 + nn)/(T + nn) - 1)];
KL[T_] = KLT*Exp[(-deltaHL/R)*(1/(T + nn) - 1/(T0 + nn))];
b[T_] = 1 + K[T] + KL[T]*(Pt - Lt);
c[T_] = -Lt*(1 + K[T]);
L[T_] = (-b[T] + Sqrt[b[T]^2 - 4*KL[T]*c[T]])/(2*KL[T]);

This will show the difference between using machine arithmetic vs bignums in the plotting.

GraphicsRow[{Plot[{L[T]}, {T, 20, 100}, PlotLabel -> "L[T]"], 
  Plot[{L[T]}, {T, 20, 100}, PlotLabel -> "L[T]", WorkingPrecision -> 20]}]

enter image description here

POSTED BY: Daniel Lichtblau

Try using exact numbers in the input and increasing the WorkingPrecision for Plot, e.g.enter image description here

POSTED BY: Ilian Gachevski
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract