I have the following code to calculate a numerical integral for any given a, however it takes a very long time, even with adaptivemontecarlo, which is not accurate enough:
Needs["NumericalCalculus`"]
a=15
g[t_] := {-(a + 2*Cos[2*t])*Sin[3*t], (a + 2*Cos[2*t])*Cos[3*t], 2*Sin[2*t]}
dg[t_] := If[t - 2*Pi <= 0, g'[t], g'[2*Pi]];
tfn :=
NDSolveValue[
{t'[s] == 1/Norm[dg[t[s]]], t[0] == 0,
WhenEvent[t[s] == 2*Pi, "StopIntegration"]},
t, {s, 0, 2*Pi + NIntegrate[Norm[g'[t]], {t, 0, 2*Pi}]}];
l := NIntegrate[Norm[D[g[t], t]], {t, 0, 2*Pi}]
c1[s_] := g[tfn[s]]
j[s_] := Normalize[Cross[D[c1[s], s], D[D[c1[s], s], s]]]
v[s_] := Normalize[Cross[j[s], Normalize[D[c1[s], s]]]]
T2[s_, y_] := c1[s] + 0.4*(j[s]*Sin[y] - v[s]*Cos [y])
s := p
y := 500*Pi*p/l
der[x_?NumericQ] := (Norm[ND[T2[s, y], {p, 2}, x]])^2
NIntegrate[der[x], {x, 0, l}, Method -> "AdaptiveMonteCarlo"]/l
How could I improve the efficiency of this code?
Edit
I forgot to mention that the integral will be evaluated for many values of "a", and that's why I used a lot of := signs.
Disclaimer: I have opened a new thread since my question is different this time.
der
. Change thes
in theNDSolveValue
expression to something else, so it doesnt get confused by your later unrleated assignment of s for some other purpose. $\endgroup$NDSolveValue
inModule
:Module[{s}, NDSolveValue[] ]
if you really must re-use the same symbol as the ode independent variable as you use elsewhere. $\endgroup$