7
$\begingroup$

I've got a simple interpolation function using data from an external program. For values above 3,000, the function reaches a limit.

FreeElectronFractionData = {{3000.000, 1.0829044}, {2999.700, 1.0828636}, {2999.400, 1.0828634}, {2999.100, 1.0828633}, ... {2.701, 0.0000003}, {2.401, 0.0000003}, {2.101, 0.0000003}, {1.801, 0.0000003}, {1.501, 0.0000003}, {1.201, 0.0000003}, {0.901, 0.0000003}, {0.601, 0.0000003}, {0.301, 0.0000003}, {0.000, 0.0000003}};

FreeElectronFraction := Interpolation[FreeElectronFractionData];

When I give the FreeElectronFraction function a value greater than 3,000, I get the message:

FreeElectronFraction[4000]
Input value {4000} lies outside the range of data in the interpolating function. Extrapolation will be used.

I don't want this. I know that anything over 3,000 has a value of 1.0829. What I want is something like:

FreeElectronFractionData = {{∞, 1.0829044}, {3000.000, 1.0829044}, {2999.700, 1.0828636}, ...

But $\infty$ doesn't appear to be a real number, so Mathematica rejects it. How do you handle the case of a simple interpolation of a data set with a limit?

$\endgroup$
9
$\begingroup$

You can use the undocumented argument "ExtrapolationHandler", which I learned about here, together with ConditionalExpression as recommended by @CarlWoll in the comments:

freeElectronFractionData = {{3000.000, 1.0829044}, {2999.700, 
    1.0828636}, {2999.400, 1.0828634}, {2999.100, 1.0828633}, {2.701, 
    0.0000003}, {2.401, 0.0000003}, {2.101, 0.0000003}, {1.801, 
    0.0000003}, {1.501, 0.0000003}, {1.201, 0.0000003}, {0.901, 
    0.0000003}, {0.601, 0.0000003}, {0.301, 0.0000003}, {0.000, 
    0.0000003}};

freeElectronFraction = Interpolation[
   freeElectronFractionData,
   "ExtrapolationHandler" -> {
     ConditionalExpression[1.0829044, # > 3000] &,
     "WarningMessage" -> False
     }
   ];

Plot[freeElectronFraction[x], {x, 0, 5000}]

Mathematica graphics

$\endgroup$
  • $\begingroup$ @DonaldAirey Posted something better now. $\endgroup$ – C. E. Aug 24 at 15:20
  • $\begingroup$ Yeah, that looks more like it. I saw the "ExtrapolationHandler", but like you said, there wasn't enough documentation behind it that I could use it. Good work. $\endgroup$ – Quarkly Aug 24 at 15:27
  • 2
    $\begingroup$ I would use ConditionalExpression[1.0829044, #>3000]& instead, so that you don't return 1.0829044 for numbers below 0. $\endgroup$ – Carl Woll Aug 24 at 15:29
  • $\begingroup$ @CarlWoll Good idea, I updated to add that. $\endgroup$ – C. E. Aug 24 at 15:32
  • $\begingroup$ This solution still returns an error when you give it $\infty$ as an argument. $\endgroup$ – Quarkly Aug 24 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.