# How to combine and plot interpolating functions defined over two different ranges

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!

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

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]


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
]


\$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]
`