function [] = Wave1d(N,m,T) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Wave1d solves wave equation using Explicit scheme. N: no of x axis mesh intervals, m: no of time % intervals, T: final time % It also compares the solutions for the equation: % u_tt-u_xx=0, u(x,0)=sin(pi x), u_t(x,0)=0, u(0,t)=0=u(1,t) % Exact solution: u(x,t)=sin(pi*x)*cos(pi*t) % Run with typing in command window: a) Wave1d(100,100,1) b) Wave1d(50,500,0.1) c) Wave1d(10,10,0.1) % d) Wave1d(1000,100,0.1) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% dx=1/N dt=T/m xx=0:dx:1; tt=0:dt:T; uu=@(x,t) sin(pi*x).*cos(pi*t); [XX,TT]=meshgrid(xx,tt); U=uu(XX,TT); l=dt/dx; CFL=l e=ones(N-1,1); A=spdiags([l*l*e 2*(1-l*l)*e l*l*e],[-1 0 1],N-1,N-1); u=zeros(m+1,N+1); u(1,:)=sin(pi*xx); b=u(1,2:N); u(2,2:N)=(A/2)*b'; for n=2:m b=u(n,2:N)'; temp=A*b-u(n-1,2:N)'; u(n+1,2:N)=temp'; end mesh(xx,tt,u) xlabel('x'); ylabel('t'); title('Solution of Wave equation : Explicit') Error_Wave =norm(u-U,Inf)