WAVES


Home

Paper

Code

Links
0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CODE
These matlab programs generate two different wave simulations. The first one is of a linear equation and the second is of a non-linear equation called the Sine-Gordon equation. At the end of each code is a 3D graph. To view the animated graph simply copy each M file into your matlab directory and run either, runprog or runprog2.

Linear equation
% Matlab code

% finitediff.m function U=finitediff(f,g,a,b,c,n,m) %FINITEDIFF approximates solution to the Wave equation, % where f and g are string args. and f=u(x,0) and g=ut(x,0), % a is length of string, b is end time, c is the constant % in the Wave equation, n is the number of steps in x direction % and m is the number of steps in the time direction. Code from % Numerical Methods, Mathews-Fink, 1999, pg.524.
h=a/(n-1); k=b/(m-1); r=c*k/h; r2=r^2; r22=r^2/2; s1=1-r^2; s2=2-2*r^2; U=zeros(n,m); for i=2:n-1 U(i,1)=feval(f,h*(i-1)); U(i,2)=s1*feval(f,h*(i-1))+k*feval(g,h*(i-1))+r22*(feval(f,h*i)+feval(f,h*(i-2))); end for j=3:m for i=2:(n-1) U(i,j)=s2*U(i,j-1)+r2*(U(i-1,j-1)+U(i+1,j-1))-U(i,j-2); end end U=U';

% pro1.m function y=pro1(x) y=sin(pi*x)+sin(2*pi*x);

% pro2.m function yy=pro2(x) yy=0;

% runprog clear n=10; timend=.5; length=1; c=2; U=finitediff('pro1','pro2',length,timend,c,n,n); x=linspace(0,length,n); t=linspace(0,timend,n); figure(1) surf(x,t,U), xlabel('x-axis'), ylabel('time') zlabel('displacement'), title('Wave Equation') shading interp, M=moviein(n); for j=1:n figure(2) plot(x,U(j,:)), axis([0 length -length*2 length*2]) M(:,j) = getframe; end movie(M,4,3)


3D Graph


Non-Linear equation
% sinegordan.m
function U=sinegordan(f,g,a,b,n,m)

%SINEGORDON approximates solution to the Sine-Gordon equation,
%	where f and g are string args. and f=u(x,0) and g=ut(x,0),
%	a is x-max(x>0), b is end time, n is the number of steps in
%	x directionand m is the number of steps in the time
%	direction.


h=a/(n-1);       %Delta x
k=b/(m-1);       %Delta t
r=k/h;
r2=r^2;
r22=r^2/2;
s1=1-r^2;
s2=2-2*r^2;
U=zeros(n,m);

for i=2:n-1,
   U(i,1)=feval(f,h*(i-1));
   U(i,2)=s1*feval(f,h*(i-1))+k*feval(g,h*(i-1))+r22*...
      (feval(f,h*i)+feval(f,h*(i-2))-sin(feval(f,h*(i-1))));
end



% runprog2.m clear n=300; timend=5; length=5; c=2; U=sinegordan('pro1','pro2',length,timend,n,n); x=linspace(0,length,n); t=linspace(0,timend,n); figure(1) surf(x,t,U),axis tight, xlabel('x-axis'), ylabel('time') zlabel('displacement'), title('Sine-Gordon Equation') shading interp, M = moviein(n); for j=1:2:n   figure(2)   plot(x,U(j,:)), axis([0 length -10 10])   M(:,j) = getframe; end movie(M)