# Numerical equation solving with FindRoot and BandpassFilter

I have problems combining filtering of a data list and FindRoot. Here is an example:

data := {1, 2, 3, 1, 2, 3, 2, 3, 4, 1, 2, 3};
Testf[c1_] := BandpassFilter[data, {2*Pi*c1, 2*Pi*0.5}][[1]];
Plot[BandpassFilter[data, {2*Pi*x, 2*Pi*0.5}][[1]], {x, 0.1, 0.5}]
FindRoot[Testf[x] == 0, {x, 0.3, 0.4}]

During evaluation of In[277]:= BandpassFilter::freq: {2 [Pi] x,3.14159} should be a vector of two non-negative, ordered real numbers or a list of such vectors.
During evaluation of In[277]:= FindRoot::nveq: The number of equations does not match the number of variables in FindRoot[Testf[x]==0,{x,0.3,0.4}].

FindRoot[Testf[x] == 0, {x, 0.3, 0.4}]

Please let me know why BandpassFilter does not work as other functions here and how to fix this code.

• Use Testf[c1_?NumericQ] := ... when you define your function ? Commented Jun 4, 2019 at 10:07
• I get a completely different plot when I run your code to that shown in the question, which is discontinuous Commented Jun 4, 2019 at 12:18
• I tried Testf[c1_?NumericQ] := ... and also Ulrich below, but it does not change the result or solve the problem. Thanks for looking. Commented Jun 4, 2019 at 14:34
• @KraZug: interesting. I run version 11. Ulrich Neumann below obtained what I obtained. I do not see a reason for a discontinuity. Commented Jun 4, 2019 at 14:36
• @Frieder, yes, on version 11.3 your code gives your plot, on version 12 I get discontinuous sections. Also on version 11.3 putting ?NumericQ works provided I have cleared the original definition. Commented Jun 5, 2019 at 4:25

FindRoot expects a function as first argument. Try

tf = FunctionInterpolation[Testf[x], {x, .1, .5}]
Plot[tf[x], {x, .1, .5}]

FindRoot[tf[x] == 0, {x, 0.3, 0.4}]
(*{x -> 0.335848}*)
• Thank you. This approximation gives Testf[0.335848] evaluates to -0.00182745, which is reasonable, but still an approx. only. Why does the original idea not work? Commented Jun 4, 2019 at 10:04
• Sorry don't know why. I tried Testf[c1_?NumericQ] := ... , as suggested by @LouisB (thanks), without succes. Commented Jun 4, 2019 at 10:09

The use of ?NumericQ indeed helps. Be careful to clear Testf first though:

Clear[Testf]
data := {1, 2, 3, 1, 2, 3, 2, 3, 4, 1, 2, 3};
Testf[c1_?NumericQ] := BandpassFilter[data, {2*Pi*c1, 2*Pi*0.5}][[1]];
Plot[BandpassFilter[data, {2*Pi*x, 2*Pi*0.5}][[1]], {x, 0.1, 0.5}]
FindRoot[Testf[x] == 0, {x, 0.3, 0.4}]

Output is then the same plot as in the question and:

{x -> 0.336227}

Then:

In[61]:= Testf[x /. %]

Out[61]= -2.73219*10^-16

as expected. Thanks to LouisB and KraZug.