The Numerical Solution of Partial Differential Equations with an Example. C Code and Octave Script

3. The Analytical Solution of the Wave Partial Differential Equation

Figure 3-1 shows the solution (37):

Figure 3-1: Analytical Solution of the (34) – (37)

4. The Numerical Solution of the Wave Partial Differential Equation

Consider the following explicit grid stencil of the finite difference method for the wave equation (14) (see also [1]):

Figure 4-1: The explicit method stencil

Figure 4-2 shows the numerical solution (34)-(36):

Figure 4-2: Numerical Solution of the (34) – (36)

5. Conclusions

• In our above example, an explicit stencil of the difference equation was used. Here is an example of an implicit stencil:

Figure 5-1: The implicit method stencil

6. Octave GNU file PartialNumericalDifferentialEquation.m

The .m script generates the analytical and numerical solutions of the partial differential equation (34)-(36) and result plots. To do this, the following functions are defined in this file:

% Numerical Solution Of the Partial Differential Equation with the Explicit Method
% u_tt(x,t) = a_speed^2*u_xx(x,t)
% Input parameters:
% u_0_t – boundary values
% u_L_t – boundary values
% u_x_0 – initial values
% u_t_x_0 – initial values
% u_tt_x_0 – initial values
% a_speed – Partial Differential Equation Parameter
% h – sample step
% Ts – sample time
% L – x space: 0…L with step Ts
% T – time space: 0…T with step h
% Output parameter:
% u_output – output
function [u_output,u_pattern] = NumericalSolutionOfPartialDifferentialEquation(u_0_t, u_L_t, u_x_0, u_t_x_0, u_tt_x_0, a_speed, h, Ts, L, T)

% Float Table to File
% Support C-Code
% Input parameters:
% ParameterVector – data array
% FileNameString – output file name
function FloatParamVector2file(ParameterVector, FileNameString)

7. C Language PartialNumericalDifferentialEquation.c/h

Functions:

/*
Wave Equation: u_tt(x,t) = a_speed^2*u_xx(x,t)
Partial Differential Equation Initialization
Initialization of the working arrays
Input: array_ptr – 2-Dimensional array pointer
Return: void
*/
void PartialDifferentialEquationInit(float* array_ptr)

/*
Wave Equation: u_tt(x,t) = a_speed^2*u_xx(x,t)
Numerical Solution Of Partial Differential Equation
Input
Input: array_ptr – 2-Dimensional array pinter
Return: void
*/
void NumericalSolutionOfPartialDifferentialEquation(float* array_ptr)