Group Abstract Group Abstract

Message Boards Message Boards

0
|
11.4K Views
|
5 Replies
|
9 Total Likes
View groups...
Share
Share this post:

Plot FFT of an interpolating function

Posted 12 years ago
POSTED BY: Phil R
5 Replies

Hi, Phil,

Fourier spectra are supposed to be different for different sample length. Read about fourier frequency resolution and Nyquist frequency to convert x axis into frequency. Also, take a look at Fourier[] documentation, FourierParameters and Applications section in particular.

I.M.

POSTED BY: Ivan Morozov

Hi, Phil,

Not sure what is wrong with your code (you forget to include definitions for m and k1). Here is a simple example for 2 uncoupled harmonic oscillators:

DATA = Reap[
    NDSolve[
       {
         x1'[s] == y1[s],
         y1'[s] ==  - 16. x1[s],
         x2'[s] == y2[s],
         y2'[s] ==  - 25. x2[s],
         x1[0] == 0.0,
         y1[0] == 1.0,    
         x2[0] == 1.0,
         y2[0] == 0.0,    
         WhenEvent[Mod[s,1.] == 0.,Sow[{x1[s],x2[s]}]]
       },
       {},
       {s,0,1000},
       MaxSteps -> Infinity,
       Method -> {
         "ImplicitRungeKutta",
         "DifferenceOrder" -> 10,
         "Coefficients" -> "ImplicitRungeKuttaGaussCoefficients",
         "ImplicitSolver" -> {
          "Newton", 
          AccuracyGoal -> MachinePrecision, 
          PrecisionGoal -> MachinePrecision, 
          "IterationSafetyFactor" -> 1
         }
       }
    ]
][[2,1]] // Transpose ;
ListLogPlot[Abs[Fourier/@DATA],PlotRange->All,Frame->True,ImageSize-> 500]

Note, that I do not use interpolating function, but WhenEvent[] function to generate trajectory sample at s = 1,2, ...

I.M.

POSTED BY: Ivan Morozov
Posted 12 years ago

Hi Ivan, k1 and m are two numeric variables. I tried to modify my code as you suggested and now it seems to work. That example was very useful. Thank you so much. There's only an issue: why if I change s step, the result is totally different? For example i tried to change in your code [Mod[s,0.1] == 0. and the new plot that I obtain is that: enter image description here

which is totally different from that: enter image description here

How values on the x axis are related to the frequencies of the system?

POSTED BY: Phil R

Forget to mention that NDSolveValue[] can be used to generate data instead of WhenEvent[]. The latter is probably more suitable for more sophisticated ''events''. The modified code is a little bit cleaner and tiny bit faster:

DATA =  NDSolveValue[
       {
         x1'[s] == y1[s],
         y1'[s] ==  - 16. x1[s],
         x2'[s] == y2[s],
         y2'[s] ==  - 25. x2[s],
         x1[0] == 0.0,
         y1[0] == 1.0,    
         x2[0] == 1.0,
         y2[0] == 0.0
       },
       {x1[#],x2[#]} & /@ Table[s,{s,1.,1000.,1.}],
       {s,0,1000},
       MaxSteps -> Infinity,
       Method -> {
         "ImplicitRungeKutta",
         "DifferenceOrder" -> 10,
         "Coefficients" -> "ImplicitRungeKuttaGaussCoefficients",
         "ImplicitSolver" -> {
          "Newton", 
          AccuracyGoal -> MachinePrecision, 
          PrecisionGoal -> MachinePrecision, 
          "IterationSafetyFactor" -> 1
         }
       }
]  // Transpose ;
ListLogPlot[Abs[Fourier/@DATA],PlotRange->All,Frame->True,ImageSize-> 500]

I.M.

POSTED BY: Ivan Morozov

Consider this new feature in Mathematica if you want to include {m, k1} as a set of variables for your ODE's.

POSTED BY: Shenghui Yang
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard