4
$\begingroup$

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}]

Testf is a well behaved function

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.

$\endgroup$
  • 1
    $\begingroup$ Use Testf[c1_?NumericQ] := ... when you define your function ? $\endgroup$ – LouisB Jun 4 at 10:07
  • $\begingroup$ I get a completely different plot when I run your code to that shown in the question, which is discontinuous $\endgroup$ – KraZug Jun 4 at 12:18
  • $\begingroup$ I tried Testf[c1_?NumericQ] := ... and also Ulrich below, but it does not change the result or solve the problem. Thanks for looking. $\endgroup$ – Frieder Jun 4 at 14:34
  • $\begingroup$ @KraZug: interesting. I run version 11. Ulrich Neumann below obtained what I obtained. I do not see a reason for a discontinuity. $\endgroup$ – Frieder Jun 4 at 14:36
  • $\begingroup$ @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. $\endgroup$ – KraZug Jun 5 at 4:25
2
$\begingroup$

FindRoot expects a function as first argument. Try

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

enter image description here

FindRoot[tf[x] == 0, {x, 0.3, 0.4}]
(*{x -> 0.335848}*)
$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Frieder Jun 4 at 10:04
  • $\begingroup$ Sorry don't know why. I tried Testf[c1_?NumericQ] := ... , as suggested by @LouisB (thanks), without succes. $\endgroup$ – Ulrich Neumann Jun 4 at 10:09
0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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