Use C Code to Solve a Differential Equation
Use C Code to Solve a Differential Equation
Functions can take C code as input. This shows NDSolve computing Duffing's equation using the Runge–Kutta method.
advancelf = LibraryFunctionLoad["demo_numerical", "duffing_crk4", {{Real, _}, Real, Real}, {Real, _}];
refinelf = LibraryFunctionLoad["demo_numerical", "refine", {Real, {Real, 2}}, {Real, 2}];
v = {{0., 0.01}, {0., 0.}};
dist = 0.025;
Δt = 0.1;
ΔT = 1.0;
Tend = 60;
plots = Table[Do[v = advancelf[refinelf[dist, v], t - Δt, t], {t, T - 1 + Δt, T, Δt}];
points = Transpose[v];
Graphics[Line[points, VertexColors -> Map[Hue, N[Range[Length[points]]] / Length[points]]], Axes -> True, PlotRange -> {{-1.5, 1.5}, {-1, 1}}], {T, ΔT, Tend, ΔT}];Take[plots, {10, 60, 10}]