# What is the proper way to operate on interpolating functions?

I am trying to multiply an interpolating function by -1. If I do this Mathematica does not seem to allow any further operations. Bear with me while I generate the Interpolating Function in question. It is a derivative of the output of NDSolve.

Clear["Global*"];
Remove["Global*"];
center = Rectangle[{-.1, -.1}, {.1, .1}];
shield = Rectangle[{-1, -1}, {1, 1}];
reg = RegionDifference[shield, center];
uif = NDSolveValue[{\!$$\*SubsuperscriptBox[\(\[Del]$$, $${x, y}$$, $$2$$]$$u[x, y]$$\) == 0,
DirichletCondition[u[x, y] == 0, x^2 + y^2 >= .5],
DirichletCondition[u[x, y] == 1, x^2 + y^2 < .5]},u, {x, y} \[Element] reg]

partialx = Derivative[1, 0][uif];
NIntegrate[partialx[.5, y], {y, -.5, .5}, AccuracyGoal -> 3]
Plot[partialx[x, -.5], {x, -1, 1}]


At this point everything is fine. NIntegrate and Plot work.

However, this code does not work.

partialxneg = -1*Derivative[1, 0][uif];
NIntegrate[partialxneg[.5, y], {y, -.5, .5}, AccuracyGoal -> 3]
Plot[partialxneg[x, -.5], {x, -1, 1}]


NIntegrate fails with

The integrand (-InterpolatingFunction[{{-1., 1.}, {-1., 1.}}, {4, 4225, 0, {1320, 0}, {3, 3}, {1 ,0}, 0, 0, 0, Automatic, {}, {}, False}, {NDSolveFEMElementMesh[{{-1., -1.}, {-1., -0.875}, <<47>>, {0.875, -1.}, <<1270>>}, <<2>>, {<<24>>[{{<<1>>}, <<49>>, <<94>>}]}]}, {0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,0., 0., 0., 0., <<1270>>}, {Automatic}])[<<1>>] has evaluated to non-numerical values for all sampling points in the region with boundaries {{-0.5, 0.5}}. >>

Also, Plot does not plot anything.

All I did was multiply the derivative by -1.

• As a side note you can use Method -> {"FiniteElement", "MeshOptions" -> {"MaxCellMeasure" -> 0.001}} to get a better resolution of the NDSolve result. – user21 Aug 4 '14 at 6:27

This really has nothing to do with interpolating functions per se; it arises from the way Mathematica deals with functional expressions in general. Consider,

f = #^2 &; g = -1 f; h = (-1 #)& @* f; hx[x_] = -1 f[x];
{f[2], g[2], h[2], hx[2]}

{4, (-(#1^2 &))[2], -4, -4}


Mathematica simply doesn't recognize g as a function, but h, which is based on the composition of functions works fine. ( @* is the operator form of Composition).

Therefore, you could use

partialxneg = -1 # & @* Derivative[1, 0][uif]


or

partialxneg[x_, y_] = -1 (Derivative[1, 0][uif])[x, y]


With either of these definitions, you will get

Plot[partialxneg[x, -.5], {x, -1, 1}]


But that's kind of silly when

Plot[-partialx[x, -.5], {x, -1, 1}]


will do the job.

You don't need to times -1 to the InterpolatingFunction. Using the FullForm, you'll see the structure will change to Times[-1,InterpolatingFunction[...]]. So it's pointless for Mathematica to do the integral and the plot.

Instead, you can use the -1 in the NIntegrate and Plot directly.

NIntegrate[-partialx[.5, y], {y, -.5, .5}, AccuracyGoal -> 3]
Plot[-partialx[x, -.5], {x, -1, 1}]

(*==> 0.716431 and the upsidedown plot*)