# Using NIntegrate over 1 variable to find function of 2 variables

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

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

and what I get from Matlab

• 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. – MassDefect Jan 21 at 0:07
• @MassDefect that did the trick, I didn't even realize I wasn't seeing the whole graph! – Bo Johnson Jan 21 at 0:27

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


• 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? – Bo Johnson Jan 21 at 0:34
• 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}] – Anton Antonov Jan 21 at 2:00
• This is really helpful, I was getting some pretty jagged plots but using the LocalAdaptive strategy makes the data look much better! Thanks @AntonAntonov – Bo Johnson Jan 21 at 2:38