Message Boards Message Boards

Why do i get different outputs for equivalent commands?

Dear all, first i'd like to apologize if my post is out of topic or in the wrong form but is my first experience with both Mathematica and this forum. I have a problem extracting the first Fourier coefficient of a function, which you can see in the image enter image description here ![][2]

G(w) is a lorentzian function, lambda is a wavelenght, z is the space dependance, w the frequency and t the time

My problem is that if i define the function (the same separeting the terms) and then try to extract the first coefficient ( by definitio, using NIntegrate and the defining integral of the fourier coefficient), i get something different than if i just put the whole definition of the function in the integral. I hope my problem is clear, i don't know which result i should trust because it gives two different output just substituting to a function its expression, which is of course nonsense. Do you know what i can do?? Thank you very much.

POSTED BY: Matteo Di Loreto
4 Replies

Thanks a lot for your reply...I try to explain better, what i actually need to look at is the modulation with the spacing z of the resonance curve of my phenomenon, which is not simply a lorentzian g[w] but has the other terms due to some interference effects. What i do is the following: defining a lorentzian function in such a way it represents my experimental curve: (i give you the exact numerical values)

G[w_] :=14000000000/(100000000+(-680000Pi+w)^2)

then i define the second term as

Term2[w_,t_,z:]:=Cos[4 Pi (z+ G[w]*Sin[t])]

where i just include Sin[t] instead Sin[wt] to normalize the period in the calculation of the first fourier coefficient. Then i calculate this term as:

Coef1[w_,z_]:=NIntegrate[Term2[w,z,t]*Sin[t],{t,0,2Pi}]

and what i'm interested in are the plots so i look at

Table[Plot[Coef1[w, z], {w, 2 \[Pi] 330000, 2 \[Pi] 350000}, 
  PlotRange -> All], {z, 10100, 10600, 50}]

My point is that doig in the previous way i get some shapes, while if i avoid to define the terms separetely but give everything in one line, like:

Table[Plot[
  NIntegrate[
   Cos[ 4 Pi (z + G[w]*Sin[t])/(980)]*Sin[t], {t, 0, 2 Pi}], {w, 
   2 \[Pi] 330000, 2 \[Pi] 350000}, PlotRange -> All], {z, 10100, 
  10600, 50}]

what i get are different plots! the same thing happens for the third part of my expression. So far i'm not really able to know what i should obtain, thing that will need further studies, and each of the three terms must have a numerical coefficient in front which has to be determined with further experiments, but still i don't know which of the two results i should work on since they should be the same. Hope now is more clear... and thanks a lot for your help.

POSTED BY: Matteo Di Loreto

If you could post the Mathematica code for your two approaches that would help. Use the code formatting tool at the top of the posting area to format that code. But, to help this along, here is your function (with lambda set to one for simplicity):

testFunction[t_, w_, z_] := 
 g[w] + Cos[4 \[Pi] (z + g[w] Sin[w t])] + 
  g[w] Sin[w t] Cos[4 \[Pi] (z + g[w] Sin[w t])]

Let's evaluate it for a particular set of values so that we can experiment with numerical integration:

testFunction[t, 1, 1] /. g -> Identity

gives

1 + Cos[4 \[Pi] (1 + Sin[t])] + Cos[4 \[Pi] (1 + Sin[t])] Sin[t]

Multiplying this by Sin[t] to get the first Fourier coefficient:

NIntegrate[
 Sin[t] (1 + Cos[4 \[Pi] (1 + Sin[t])] + 
    Cos[4 \[Pi] (1 + Sin[t])] Sin[t]), {t, 0, 2 \[Pi]}]

which gives

1.06691

Now try multiplying though with the Sin[t] funciton (which of course is the same thing as the above:

NIntegrate[(Sin[t] + Sin[t] Cos[4 \[Pi] (1 + Sin[t])] + 
   Sin[t] Cos[4 \[Pi] (1 + Sin[t])] Sin[t]), {t, 0, 2 \[Pi]}]

which gives:

1.06691

Now do the NIntegrate term-by term:

NIntegrate[Sin[t], {t, 0, 2 \[Pi]}] + 
 NIntegrate[Sin[t] Cos[4 \[Pi] (1 + Sin[t])], {t, 0, 2 \[Pi]}] + 
 NIntegrate[Sin[t] Cos[4 \[Pi] (1 + Sin[t])] Sin[t], {t, 0, 2 \[Pi]}]

giving

1.06691

This gives some warnings that look like *NIntegrate failed to converge to prescribed accuracy after 9 \ recursive bisections in t near...*, so let's examine the individual terms. This one

NIntegrate[Sin[t], {t, 0, 2 \[Pi]}]

gives

2.72352 10^-16

and generates the above message because it is so close to zero. Similarly for this one:

NIntegrate[Sin[t] Cos[4 \[Pi] (1 + Sin[t])], {t, 0, 2 \[Pi]}]

giving

-6.52256 10^-16

Finally this one gives the original answer:

NIntegrate[Sin[t] Cos[4 \[Pi] (1 + Sin[t])] Sin[t], {t, 0, 2 \[Pi]}]

giving

1.06691

If this represents an example of your calculations perhaps you dropped a factor of something some where? Or perhaps the particular case you computed (e.g., higher order terms in the Foruier series) involved a detailed cancelation between two terms which caused numerical issues?

POSTED BY: David Reiss

thanks, i edited the post right now, it should be visible!

POSTED BY: Matteo Di Loreto

I'm afraid your picture isn't appearing in the post. When you insert the picture, make certain not to delete the line at the bottom.

POSTED BY: Jesse Friedman
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