# include # include # include using namespace std; # include "fsolve.hpp" # include "rk2_implicit.hpp" //****************************************************************************80 void rk2_implicit ( void dydt ( double x, double y[], double dy[] ), double tspan[2], double y0[], int n, int m, double t[], double y[] ) //****************************************************************************80 // // Purpose: // // rk2_implicit() uses rk2_implicit method() + fsolve_be() to solve an ODE. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 14 November 2024 // // Author: // // John Burkardt // // Input: // // dydt: a function that evaluates the right hand side of the ODE, // of the form // void dydt ( double t, double y[], double dy[] ) // // double tspan[2]: the initial and final times. // // double y0[m]: the initial condition. // // int n: the number of steps to take. // // int m: the number of variables. // // Output: // // double t[n+1], y[m*(n+1)]: the times and solution values. // { double dt; double *fm; int i; int info; int j; double theta; double tm; double to; double tol; double *ym; double *yo; fm = new double[m]; ym = new double[m]; yo = new double[m]; dt = ( tspan[1] - tspan[0] ) / ( double ) ( n ); theta = 0.5; tol = 1.0e-05; for ( i = 0; i <= n; i++ ) { if ( i == 0 ) { t[i] = tspan[0]; for ( j = 0; j < m; j++ ) { y[i*m+j] = y0[j]; } } else { to = t[i-1]; for ( j = 0; j < m; j++ ) { yo[j] = y[(i-1)*m+j]; } tm = t[i-1] + theta * dt; for ( j = 0; j < m; j++ ) { ym[j] = y[(i-1)*m+j]; } info = fsolve_be ( dydt, m, to, yo, tm, ym, fm, tol ); if ( info != 1 ) { cout << "\n"; cout << "rk2_implicit(): Fatal error!\n"; cout << " info = " << info << "\n"; exit ( 1 ); } t[i] = t[i-1] + dt; for ( j = 0; j < m; j++ ) { y[i*m+j] = ( 1.0 / theta ) * ym[j] + ( 1.0 - 1.0 / theta ) * y[(i-1)*m+j]; } } } delete [] fm; delete [] ym; delete [] yo; return; }