k = 5.;
tf = 3.;
c[θ1_?NumericQ, θ2_?NumericQ] :=
NIntegrate[(Sin[θ1 + t]^2. Cos[θ2 + t])/E^t, {t, 0., tf},
Method -> {Automatic, SymbolicProcessing -> 0}];
sol = NDSolve[{Derivative[1][θ1][t]θ1'[t] == θ2[t]/E^(Sin[t]/10.) + c[θ1[t], θ2[t]],
Derivative[1][θ2][t] θ2'[t] == (-k Sin[θ1[t]])/E^(Sin[t]/10.),
θ1[0.] == 1., θ2[0.] == 1.}, {θ1, θ2}, {t, 0., tf}]; // AbsoluteTiming
(* {0.0730725, Null} *)
ref = sol[[1, All, -1]] // ListLinePlot
Then, as to the implementation of collocation method, NMinimize
is undoubtedly a bad choice. Just use FindRoot
with the new-in-12.0 "AffineCovariantNewton"
method. I've also turned to Chebyshev–Gauss–Lobatto grid to improve the accuracy of the solution:
CGLGrid[{xl_, xr_}, n_] := 1/2 (xl + xr + (xl - xr) Cos[(π Range[0, n - 1])/(n - 1)]);
npoints = 11;
tpoints = CGLGrid[{0., tf}, npoints];
myd = dMatrixLagrange[tpoints];
SetAttributes[c, Listable];
θ1init = {1.}; θ2init = {1.};
tst = FindRoot[{arg1, arg2} |->
With[{t = tpoints, θ1 = θ1init~Join~arg1, θ2 = θ2init~Join~arg2},
Rest /@ {myd . θ1 - (θ2/E^(Sin[t]/10.) + c[θ1, θ2]),
myd . θ2 - (-k Sin[θ1])/E^(Sin[t]/10.)}],
{#, #} &@{ConstantArray[1., npoints - 1]}];,
Method -> "AffineCovariantNewton"]; // AbsoluteTiming
(* {0.864978426819, Null} *)
ListPlot[{tpoints, #}\[Transpose] & /@ Join[{θ1init, θ2init}, tst, 2],
PlotRange -> All]~Show~ref