6
$\begingroup$

I have defined interpolating functions over two different ranges.

A = Interpolation[{{1, 10}, {2, 20}, {3, 20}.{4, 2}}]
B = Interpolation[{{2, 15}, {3, 5}, {4, 1}, {5, 2}}]

Now, I want to combine these in a single plot as follows:

Plot[{A[x], B[x]}, {x, 1, 5}]

It results in an error—"InterpolatingFunction: Input value \!\(\*RowBox[{\" {\", \" 1.0000817142857144 \", \"} \"}]\) \ lies outside the range of data in the interpolating function. Extrapolation will be used". The possible reason for this error could be that x=1 lies outside the interpolation region of B.

Is there a way to plot these interpolated functions together?

Thanks!

$\endgroup$
1
  • $\begingroup$ Why not avoid extrapolation by using a common plot range? e.g. Plot[{A[x], B[x]}, {x, 2, 4}] $\endgroup$ Commented Sep 17, 2022 at 0:15

3 Answers 3

8
$\begingroup$

You can define how to handle extrapolation:

a = Interpolation[{{1, 10}, {2, 20}, {3, 20}, {4, 2}}, 
   "ExtrapolationHandler" -> {Indeterminate &, 
     "WarningMessage" -> False}];
b = Interpolation[{{2, 15}, {3, 5}, {4, 1}, {5, 2}}, 
   "ExtrapolationHandler" -> {Indeterminate &, 
     "WarningMessage" -> False}];

Plot[{a[x], b[x]}, {x, 0, 6}, PlotRange -> All]

enter image description here

$\endgroup$
5
$\begingroup$

Would this work?

a = Interpolation[{{1, 10}, {2, 20}, {3, 20} . {4, 2}}]
b = Interpolation[{{2, 15}, {3, 5}, {4, 1}, {5, 2}}]

ListLinePlot[
  {a, b},
  PlotRange -> All, InterpolationOrder -> 2
]

plot of both

$\endgroup$
3
$\begingroup$
$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global`*"]

a = Interpolation[{{1, 10}, {2, 20}, {3, 20}, {4, 2}}];
b = Interpolation[{{2, 15}, {3, 5}, {4, 1}, {5, 2}}];

Define functions that are conditioned to only evaluate in the appropriate interval

ac[x_ /; 1 <= x <= 4] := a[x]
bc[x_ /; 2 <= x <= 5] := b[x]

Plot[{ac[x], bc[x]}, {x, 0, 6}, PlotRange -> All]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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