# include # include # include # include # include "fsolve.h" # include "rk2_implicit.h" /******************************************************************************/ void rk2_implicit ( void dydt ( double t, double y[], double dy[] ), double tspan[2], double y0[], int n, int m, double t[], double y[] ) /******************************************************************************/ /* Purpose: rk2_implicit() uses a Runge-Kutta order 2 implicit method + fsolve_be() to solve an ODE. Licensing: This code is distributed under the MIT license. Modified: 09 November 2023 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 = ( double * ) malloc ( m * sizeof ( double ) ); yo = ( double * ) malloc ( m * sizeof ( double ) ); ym = ( double * ) malloc ( m * sizeof ( double ) ); 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 ) { printf ( "\n" ); printf ( "rk2_implicit(): Fatal error in fsolve_be()!\n" ); printf ( " info = %d\n", info ); 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]; } } } free ( fm ); free ( ym ); free ( yo ); return; }