Runge-Kutta method: Difference between revisions
→{{header|C++}}: there really is no need to imbricate lambdas here, as C++ allows variables in them.
(→{{header|C++}}: simplifying a bit) |
(→{{header|C++}}: there really is no need to imbricate lambdas here, as C++ allows variables in them.) |
||
Line 419:
auto rk4(double f(double, double))
{
return [f](double t, double y, double dt ) -> double {
double dy1 { dt
};
▲ ( dy1 + 2*dy2 + 2*dy3 + dy4 ) / 6 ;} (
}
bool is_whole(double t) {
constexpr double WHOLE_TOLERANCE = 1e-12;
return std::fabs(t-round(t)) < WHOLE_TOLERANCE;
}
int main(void)
{
Line 443 ⟶ 439:
Y_START = 1.0,
DT = 0.10;
auto dy = rk4( [](double t, double y) -> double { return t*sqrt(y); } ) ;
for (
double y = Y_START, t = T_START ;
Line 453 ⟶ 449:
if (is_whole(t))
printf("y(%4.1f)\t=%12.6f \t error: %12.6e\n", t, y, std::fabs(y - pow(t*t+4,2)/16));
return 0;
}</lang>
|