用有限差分法求解二维波动方程的Matlab程序

时间:2015-09-08 19:56:29

标签: matlab wave

以下是我的Matlab代码,使用有限差分法模拟使用高斯源的二维波动方程。我无法正确解决它,出于某种原因,我得到了奇怪的结果。如果你能指出我的代码错误的地方,那将会很有帮助。谢谢。

c0=3e8;         % speed of light or any wave
e0=8.854e-12;   % free space permittivity
u0=1.2566e-6;   % free space permeability

size=200;        % size of free space

s=size;            % s determines the position of the source in the free space

source_position=size/s; % if s=2, source is at the middle of the free space
                        % if s=1, source is at the end of the free space
                        % if s=size, source is at the beginning of the free space etc.

dx=0.001;        % spatial increment

dt=dx/(c0);   % time increment

cons=c0*dt/dx; % constant term of electric and magnetic field equations

n=200; % total time

% u=zeros(1,size); 
u_n=zeros(size); %next time
t=zeros(size);   %present time
u_p=zeros(size); %previous time

[u,v] = meshgrid(1:200,1:200);

t0=15;   % t0 of Gaussian source 
tp=5;   % tp of Gaussian source

for k=1:n
    for i=2:size-1
    for j=2:size-1      
        u_n(i,j)= 2*t(i,j)-u_p(i,j)+(cons^2)*( u(i+1,j)+u(i-1,j)-2*u(i,j)-2*v(i,j)+v(i,j+1)+v(i,j-1) );
    end
    end
u_p=u;
t=u_n;

u(source_position)=exp(-((k-t0)/tp)^2);
% u(source_position)=sin(2*pi*0.03*i);
surf(u,v,t)
A(k)=getframe;
end

0 个答案:

没有答案