Message Boards Message Boards

Fourier analysis: How to plot several functions onto one plot?

Posted 2 years ago

Hi there,

I have spent several days working on this project and trying to learn from others/the online Mathematica manual. After toiling hard on my own, I am now encountering three issues and would appreciate any help as soon as is possible. I am attaching my notebook to this post.

Firstly, if you open the notebook, you will see some data . This is the data that I am analysing. Scroll down to the recon[x] and you will see the Fourier series that Mathematica has generated for me. When I try to transfer this series to other software (e.g. Desmos), it goes funny, so I want to keep things on Mathematica if possible.

issues:

1) How can I draw several lines on the same plot? In this case, I want to draw the Fourier series (recon[x]) and the original data. To this plot, I would then like to add the Fourier series with only the first 5 cosine and 5 sine terms drawn.

2) At the very end, I start to multiply my data by cosine. I would like Mathematica to integrate this new function over the period (I can input these values) and work out the area under the curve. How can I do this?

3) Going back to (1) - is it possible to add a slider, whereby instead of adding the first 5 cosine and sine terms I can add a varying number. This would allow me to see at what point the approximation starts getting close.

I will be happy to clarify anything I've said which makes no sense at all! Really looking forward to receiving your help.

Very best wishes,

Attachments:
POSTED BY: R Khanna
9 Replies
Posted 2 years ago

In / Out numbers are kernel specific and not preserved in a saved notebook.

This error is self-explanatory, take a look at the domain of fun

InterpolatingFunction::dmvali: The integration endpoint 0 in dimension 1 lies outside the range of data in the interpolating function. Extrapolation will be used.

This one is also clear because nx is not defined.

NIntegrate::inumr: The integrand Cos[24.3054 nx] <<1>> has evaluated to non-numerical values for all sampling points in the region with boundaries {{0.30383,0.30837}}.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

UPDATE: Here is my new notebook.

I would like to sort out two errors:

one is Out[333] and the other is In[315]

Thank you.

Attachments:
POSTED BY: R Khanna
Posted 2 years ago

I still do not understand how to 'parameterize the extraction of n terms', Rohit - this is something I do not know how to do and would really appreciate if you could show me what you mean.

Thank you,

POSTED BY: R Khanna
Posted 2 years ago

Thank you. That makes sense. So what is the difference between recon[x] and recon[x-x1?]

POSTED BY: R Khanna
Posted 2 years ago

Crossposted here.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

One more point - the third period seems to be fairly accurate. Any chance we can get the first period (or the entire thing!) to be more accurate?

POSTED BY: R Khanna
Posted 2 years ago

The plot of recon[x - x1] overlays almost exactly with fun[x]. If you zoom in small areas of blue are visible

Plot[{fun[x], recon[x - x1]}, {x, x1, x2}, ImageSize -> 600]

enter image description here

POSTED BY: Rohit Namjoshi
Posted 2 years ago

1) Plot can plot several functions in the same plot

Plot[{fun[x], recon[x]}, {x, x1, x2}]

enter image description here

Use FirstPosition to locate the position of the first Cos and Sin

firstCos = FirstPosition[recon[x], _Cos] // First
firstSin = FirstPosition[recon[x], _Sin] // First
fiveCos = recon[x][[firstCos ;; firstCos + 4]]
fiveSin = recon[x][[firstSin ;; firstSin + 4]]   

recon5[x_] = recon[x][[;; firstCos - 1]] + fiveCos + fiveSin
Plot[{fun[x], recon5[x]}, {x, x1, x2}]

enter image description here

2)

NIntegrate[fun[x]*Cos[x], {x, x1, x2}] (* Change limits as needed *)
(* 0.000955089 *)

3) For the manipulate, parameterize the extraction of n terms as was done for 5 terms above. Try it and if you get stuck, post what you tried.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

Thank you Rohit. That’s just what I was looking for. A quick follow up to number 1.

It seems as if my Fourier series approximation isn’t as close as I thought, which I think might be because my code was quite clunky? Would it be possible for you to offer an alternative such that the Fourier series plot lies even more closely on top of the data plot?

Thanks again.

EDIT - I have isolated one period of the waveform (equal to approximately 0.26seconds) and did the same Fourier analysis on that to see if a smaller data set would lead to more accuracy. The notebook of my results is attached. Still not very close, so it must be something to do with the code itself?

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

Group Abstract Group Abstract