# 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}][];
Plot[BandpassFilter[data, {2*Pi*x, 2*Pi*0.5}][], {x, 0.1, 0.5}]
FindRoot[Testf[x] == 0, {x, 0.3, 0.4}] During evaluation of In:= 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:= 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 ? – LouisB Jun 4 '19 at 10:07
• I get a completely different plot when I run your code to that shown in the question, which is discontinuous – SPPearce Jun 4 '19 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. – Frieder Jun 4 '19 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. – Frieder Jun 4 '19 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. – SPPearce Jun 5 '19 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? – Frieder Jun 4 '19 at 10:04
• Sorry don't know why. I tried Testf[c1_?NumericQ] := ... , as suggested by @LouisB (thanks), without succes. – Ulrich Neumann Jun 4 '19 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}][];
Plot[BandpassFilter[data, {2*Pi*x, 2*Pi*0.5}][], {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:= Testf[x /. %]

Out= -2.73219*10^-16


as expected. Thanks to LouisB and KraZug.