Solve the first-order wave equation using the central difference scheme (with Matlab code)
∂ 2 u ∂ t 2 − ∂ 2 u ∂ x 2 = 0 , 0 < x < 1 , t > 0 , \frac{\partial^2u}{\partial t^2}-\frac{\partial^2u}{\partial x^2}=0,0<x<1,t>0, ∂t2∂2 and−∂x2∂2 and=0,0<x<1,t>0 ,
the initial boundary value condition is:
u ( 0 , x ) = 2 sin ( π x ) , u\left(0,x\right)=2sin\left(\pi x\right),u(0,x)=2 s in( π x ),
u t ( 0 , x ) = 0 , 0 < x < 1 , u_t\left(0,x\right)=0,0<x<1, ut(0,x)=0,0<x<1,
u ( t , 0 ) = u ( t , 1 ) = 0 , t ≥ 0. u\left(t,0\right)=u\left(t,1\right)=0,t\geq0. u(t,0)=u(t,1)=0,t≥0.
精确解: u = s i n ( π ( x − t ) ) + s i n ( π ( x + t ) ) 。 u=sin\left(\pi\left(x-t\right)\right)+sin\left(\pi\left(x+t\right)\right)。 u=sin( p(x−t))+sin( p(x+t))。
(1) Takeτ\tauτ =0.05,h=0.1 Calculate the solution of t = 0.5,1.0,1.5,2.0.
(2) Takeτ \tauτ =h=0.1, calculate the solution of t = 0.5,1.0,1.5,2.0.
untie:
(1) Problem analysis
Step 1: Meshing
Take the time step τ = TN \tau=\frac{T}{N}t=NTand space step size h = l J h=\frac{l}{J}h=Jl, note the rectangular area G = 0 ≤ t ≤ T , 0 ≤ x ≤ l G = {0 \le t \le T, 0 \le x \le l}G=0≤t≤T,0≤x≤l . Use two families of parallel straight lines to divide this area G into a rectangular grid. Grid nodes are represented by coordinate points(tn, xj) \left(t_n,x_j\right)(tn,xj) means. G h G_hGhRepresents the set of points within the grid. Γ h = G / G h = G − G h \Gamma_h=G/G_h=G-G_hCh=G / Gh=G−GhRepresents a set of boundary points. Use u (tn, xj) u\left(t_n,x_j\right)u(tn,xj) represents the true solution,ujn u_j^nujnExpressed in (tn, xj) \left(t_n,x_j\right)(tn,xj) is the approximate value. Define the network ratior 2 = τ 2 h 2 r^2=\frac{\tau^2}{h^2}r2=h2t2。
Step 2: Difference equation, central difference format
u j n + 1 − 2 u j n + u j n − 1 = r 2 ( u j + 1 n − 2 u j n + u j − 1 n ) u_j^{n+1}-2u_j^n+u_j^{n-1}=r^2\left(u_{j+1}^n-2u_j^n+u_{j-1}^n\right) ujn+1−2 andjn+ujn−1=r2(uj+1n−2 andjn+uj−1n)
⇒ u j n + 1 = r 2 ( u j − 1 n + u j + 1 n ) + 2 ( 1 − r 2 ) u j n − u j n − 1 ( 1 ) \Rightarrow u_j^{n+1}=r^2\left(u_{j-1}^n+u_{j+1}^n\right)+2\left(1-r^2\right)u_j^n-u_j^{n-1} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (1) ⇒ujn+1=r2(uj−1n+uj+1n)+2(1−r2)ujn−ujn−1 ( 1 )
由初值实作得:uj 0 = sin ( π xj ) , uj 1 − uj − 1 2 τ = 0. u_j^0=sin\left(\pi x_j\right),\frac{u_j^1 -u_j^{-1}}{2\tau}=0.uj0=sin( π xj),2 squj1−uj−1=0.
(1)式中令n=0, 得: u j 1 = r 2 ( u j − 1 0 + u j + 1 0 ) + 2 ( 1 − r 2 ) u j 0 − u j − 1 ,消去 u j − 1 u_j^1=r^2\left(u_{j-1}^0+u_{j+1}^0\right)+2\left(1-r^2\right)u_j^0-u_j^{-1},消去u_j^{-1} uj1=r2(uj−10+uj+10)+2(1−r2)uj0−uj−1, erasure uj−1,得:
u j 1 = r 2 ( s i n ( π x j − 1 ) + s i n ( π x j + 1 ) ) + ( 1 − r 2 ) s i n ( π x j ) . u_j^1=r^2\left(sin\left(\pi x_{j-1}\right)+sin\left(\pi x_{j+1}\right)\right)+\left(1-r^2\right)sin\left(\pi x_j\right). uj1=r2(sin( π xj−1)+sin( π xj+1))+(1−r2)sin( π xj).
令 U n = ( u 1 n , u 2 n , ⋯ , u J − 1 n ) T , {\ U}^n=\left(u_1^n,u_2^n,\cdots,u_{J-1}^n\right)^T, Un=(u1n,u2n,⋯,uJ−1n)T, A = [ 2 ( 1 − r 2 ) r 2 0 ⋯ 0 0 0 r 2 2 ( 1 − r 2 ) r 2 ⋯ 0 0 0 ⋯ ⋯ ⋯ ⋯ ⋯ ⋯ 0 0 0 0 ⋯ r 2 2 ( 1 − r 2 ) r 2 0 0 0 ⋯ 0 r 2 2 ( 1 − r 2 ) ] A=\left[\begin{matrix}2\left(1-r^2\right)&r^2&0&\cdots&0&0&0\\r^2&2\left(1-r^2\right)&r^2&\cdots&0&0&0\\\cdots&\cdots&\cdots&\cdots&\cdots&\cdots&0\\0&0&0&\cdots&r^2&2\left(1-r^2\right)&r^2\\0&0&0&\cdots&0&r^2&2\left(1-r^2\right)\\\end{matrix}\right] A=
2(1−r2)r2⋯00r22(1−r2)⋯000r2⋯00⋯⋯⋯⋯⋯00⋯r2000⋯2(1−r2)r2000r22(1−r2)
is a J − 1 × J − 1 J-1×J-1J−1×J−1,的单位矩阵,特别地, U 0 = ( 2 s i n ( π x 1 ) , 2 s i n ( π x 2 ) , ⋯ , 2 s i n ( π x J − 1 ) ) T , U 1 = ( u 1 1 , u 2 1 , ⋯ , u J − 1 1 ) T . {\ U}^0=\left(2sin\left(\pi x_1\right),2sin\left(\pi x_2\right),\cdots,2sin\left(\pi x_{J-1}\right)\right)^T,{\ U}^1=\left(u_1^1,u_2^1,\cdots,u_{J-1}^1\right)^T. U0=( 2 s in( π x1),2 s in( π x2),⋯,2 s in( π xJ−1))T, U1=(u11,u21,⋯,uJ−11)T.
(2) Computer program
function [U1,E]=shuangqv(J,N)
x0=0;xJ=1;h=(xJ-x0)/J;x=[x0:h:xJ]';
t0=0;tN=2;tau=(tN-t0)/N;t=[t0:tau:tN]';
r=tau^2/h^2;
u(:,1)=2*sin(pi*x(2:J,1));
for j=1:J-1
u(j,2)=r*(sin(pi*x(j))+sin(pi*x(j+2)))+(1-r)*2*sin(pi*x(j+1));
end
U(:,1)=[u(:,2);u(:,1)];
A=diag(ones(1,J-1))*(2-2*r)+(diag(ones(1,J-2),1)+diag(ones(1,J-2),-1))*r;
E=diag(ones(1,J-1));
B=[A,-E;E,zeros(J-1,J-1)];
for n=2:N+1
U(:,n)=B*U(:,n-1);
end
U1=U(J:2*(J-1),:);
U1=[zeros(1,N+1);U1;zeros(1,N+1)];
for j=1:J+1
for n=1:N+1
V(j,n)=sin(pi*(x(j)-t(n)))+sin(pi*(x(j)+t(n)));
end
end
E=max(max(abs(U1-V)));
end
%调试
clear all;clc;close all;
[U1,E1]=shuangqv(10,40);
[U2,E2]=shuangqv(10,20);
(3) Question results
( τ \yearWhen τ =h=0.1, the images of the analytical solution and the numerical solution)
It can be seen from the image that the images of the analytical solution and the numerical solution are highly consistent. Next we look at the maximum error in the two cases:
maximum error | |
---|---|
τ \yearτ =0.05,h=0.1 | 0.029747384249724 |
τ \yearτ =0.1,h=0.1 | 1.665334536937735e-15 |
( τ \yearNumerical solution corresponding to the corresponding time when τ
=0.05, h=0.1) (τ \tauNumerical solution corresponding to the corresponding time when τ =0.1,h=0.1)