I need a bit of help speeding up this code:
sol = NDSolveValue[{I k'[s] == 20 (s k[s] - (1 - s) (1 - k[s]) (1 + k[s])),
k[0] == 1/2}, k, {s, 0, 1}];
func[s_] := Abs[ NIntegrate[(1 - sl) (1 - sol@sl),{sl,0,s}]];
Plot[func[s], {s, 0, 1}]
So, Plot evaluates NIntegrate multiple times, and wastes a lot of time redoing calculations from zero to s in small increments.
Is there a way to run NIntegrate once from beginning to end and store its intermediate values in something like an interpolating function?
I am using Mathematica 10, and StepMonitor is not a valid option for NIntegrate. :(
Answer
Look at Help->Find Selekcted Function->NIntegrate->Properties and Relations ->"NDSolve can be used instead of NIntegrate". Using NDSolve you only once calculate an Interpolated Function up to your max s, that can be used for Plot.
NDSolve[{D[f[sl], sl] == (1 - sl) (1 - sol@sl), f[0] == 0}, f,
{sl, 0, smax}][[1, 1]]
Comments
Post a Comment