# include <math.h> # include <stdio.h> # include <stdlib.h> # include "standing_wave_exact.h" /******************************************************************************/ void standing_wave_exact ( double x, double t, double c, double *u, double *ut, double *utt, double *ux, double *uxx ) /******************************************************************************/ /* Purpose: standing_wave_exact() evaluates an exact solution of the 1D wave equation. Discussion: d^2u/dt^2 = c^2 d^2u/dx^2 u[x,t] = sin(x) * cos(c*t) Licensing: This code is distributed under the MIT license. Modified: 09 April 2025 Author: John Burkardt Input: double x, t: the position and time. double c: the wave speed. Output: double *u, *ut, *utt, *ux, *uxx: the values of the exact solution, its time derivative, and its first and second spatial derivatives at (x,t). */ { *u = sin ( x ) * cos ( c * t ); *ut = - c * sin ( x ) * sin ( c * t ); *utt = - c * c * sin ( x ) * cos ( c * t ); *ux = cos ( x ) * cos ( c * t ); *uxx = - sin ( x ) * cos ( c * t ); return; } /******************************************************************************/ void standing_wave_parameters ( double *c_in, double *xmin_in, double *xmax_in, double *t0_in, double *tstop_in, double *c_out, double *xmin_out, double *xmax_out, double *t0_out, double *tstop_out ) /******************************************************************************/ /* Purpose: standing_wave_parameters() returns parameters for the standing wave equation. Discussion: d^2u/dt^2 = c^2 d^2u/dx^2 u[x,t] = sin(x) * cos(c*t) If input values are specified, this resets the default parameters. Otherwise, the output will be the current defaults. Licensing: This code is distributed under the MIT license. Modified: 09 April 2025 Author: John Burkardt Input: double *c_in: the wave speed. double *xmin_in, *xmax_in: the left and right interval endpoints. double *t0_in: the initial time. double *tstop_in: the final time. Output: double *c_out: the wave speed. double *xmin_out, *xmax_out: the left and right interval endpoints. double *t0_out: the initial time. double *tstop_out: the final time. */ { static double c_default = 0.2; static double t0_default = 0.0; static double tstop_default = 5.0; static double xmax_default = +1.0; static double xmin_default = -1.0; /* New values, if supplied on input, overwrite the current values. */ if ( c_in ) { c_default = *c_in; } if ( xmin_in ) { xmin_default = *xmin_in; } if ( xmax_in ) { xmax_default = *xmax_in; } if ( t0_in ) { t0_default = *t0_in; } if ( tstop_in ) { tstop_default = *tstop_in; } /* The current values are copied to the output. */ if ( c_out ) { *c_out = c_default; } if ( xmin_out ) { *xmin_out = xmin_default; } if ( xmax_out ) { *xmax_out = xmax_default; } if ( t0_out ) { *t0_out = t0_default; } if ( tstop_out ) { *tstop_out = tstop_default; } return; } /******************************************************************************/ void standing_wave_residual ( double x, double t, double c, double *r ) /******************************************************************************/ /* Purpose: standing_wave_residual() computes the residual of the standing wave equation. Discussion: d^2u/dt^2 = c^2 d^2u/dx^2 u[x,t] = sin(x) * cos(c*t) Licensing: This code is distributed under the MIT license. Modified: 09 April 2025 Author: John Burkardt Input: double X, T: the position and time where the solution is evaluated. double c: the wave speed. Output: double *R: the residual at that time and position. */ { double u; double ut; double utt; double ux; double uxx; standing_wave_exact ( x, t, c, &u, &ut, &utt, &ux, &uxx ); *r = utt - c * c * uxx; return; }