0
$\begingroup$

I'm attempting to numerically solve the following in order to get a function of 2 variables, just looking at the real part of

$$\psi(x,t)=\frac{1}{\pi\sqrt{2}}\int_{-100}^{100}\frac{\sin{(k)}}{k}e^{i\left[kx-\frac{k^2}{2}t\right]}\,dk$$

where I have specific values for $t=0,2,4$ and I'd like to plot the function from $x=-10$ to $x=10$. The way I've tried so far is

func[x_, t_] := Re[Sin[k]/k*Exp[I*(k*x - k^2/2*t)]]
y = Table[NIntegrate[func[x, 0], {k, -100, 100}], {x, -10, 10, 0.01}]

I get several errors when running this but still get some results out

enter image description here

The plotted results aren't what I was expecting. I've done this numerical integration in Matlab where I specified in the integration function to expect an array:

x=-10:0.01:10;
func = @(k,c) sin(k)./(pi*sqrt(2).*k).*cos(k.*c-k.^2./2*0);
real_0 = integral(@(k)func(k,x),-100,100,'ArrayValued',true);

I'm pretty new to Mathematica and don't know what the equivalent to this Matlab expression would be.

Here are the results I get from Mathematica

enter image description here

and what I get from Matlab

enter image description here

$\endgroup$
  • $\begingroup$ They look like they might be the same to me. In the Mathematica code where you plot the graph (I assume you use something like ListLinePlot) add a line that says PlotRange -> Full in order to see the full graph. Mathematica tends to zoom in so that you don't miss seeing "interesting" behaviour near the x-axis. $\endgroup$ – MassDefect Jan 21 at 0:07
  • 1
    $\begingroup$ @MassDefect that did the trick, I didn't even realize I wasn't seeing the whole graph! $\endgroup$ – Bo Johnson Jan 21 at 0:27
2
$\begingroup$

The problem with your plot is that did not use PlotRange->All.

First slight modifications of your NIntegrate code:

AbsoluteTiming[
 xs = Range[-10, 10, 0.01]; 
 ys = Table[
   NIntegrate[func[x, 0], {k, -100, 100}, 
    Method -> {Automatic, "SymbolicProcessing" -> 0}, 
    MaxRecursion -> 100,
    PrecisionGoal -> 4, AccuracyGoal -> 4], {x, xs}];
 ]

(* {42.5655, Null} *)

(The precision goal change from 6 to 4 does not have significant effect on the computation speed; the accuracy goal change to 4 does.)

ListLinePlot[Transpose[{xs, ys}], PlotRange -> All, PlotTheme -> "Detailed"]

enter image description here

$\endgroup$
  • $\begingroup$ I see how not using PlotRange -> All would affect my plot. When I did your approach I still got some NIntegrate and General errors, although not as many as when I attempted my first approach. Are these things to worry about or can they safely be ignored? $\endgroup$ – Bo Johnson Jan 21 at 0:34
  • $\begingroup$ I only saw warning messages. As far as I know MATLAB uses local adaptive integration strategy (or at least it did 12 years ago.) So, you can use NIntegrate's "LocalAdaptive" method and you will get less number of messages or none. E.g. try this: ys = Table[ NIntegrate[func[x, 0], {k, -100, 100}, Method -> {"LocalAdaptive", "SymbolicProcessing" -> 0}, PrecisionGoal -> 8, AccuracyGoal -> 6, MaxRecursion -> 100], {x, xs}] $\endgroup$ – Anton Antonov Jan 21 at 2:00
  • $\begingroup$ This is really helpful, I was getting some pretty jagged plots but using the LocalAdaptive strategy makes the data look much better! Thanks @AntonAntonov $\endgroup$ – Bo Johnson Jan 21 at 2:38

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.