基于有限差分法的Matlab二维波动方程

基于有限差分法的Matlab二维波动方程,matlab,Matlab,下面是我的Matlab代码,它使用FDM模拟了一个以高斯源为中心的二维波动方程。我使用imagesc函数来输出波动。波浪似乎从中心向外扩散,但速度很慢。好像我把事情搞砸了。输出是非常像素化的。我做错了什么 clc close all clear all c0 = 3e1; % speed of light or any wave e0 = 8.854e-12; % free space permittivity u0 = 1.2566e-6; % free space

下面是我的Matlab代码,它使用FDM模拟了一个以高斯源为中心的二维波动方程。我使用imagesc函数来输出波动。波浪似乎从中心向外扩散,但速度很慢。好像我把事情搞砸了。输出是非常像素化的。我做错了什么

clc
close all
clear all

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

size=170;        % size of free space

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




dx=0.001;        % spatial increment

dt=dx/(c0);   % time increment

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

n =500 ; % total time

% u=zeros(1,size); % initially, E at all points is taken zero
u_n=zeros(size);    %constant time next state of wave
u_p=zeros(size);    %constant time previous state of wave
u = zeros(size);    %constant time present state of wave

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*u_n(i,j)-u_p(i,j)+(cons^2)*( u(i+1,j)+u(i-1,j)-4*u(i,j)+u(i,j+1)+u(i,j-1) );
    end
    end
u_p=u;   % after this iteration present state becomes previous state
u=u_n;   % next step becomes present state

u(size/2,size/2)=exp(-((k-t0)/tp)^2);  % gaussian source position selected at the centre of the matrix
%u(size/2,size/2)=sin(2*pi*0.03*i);
imagesc(u)
A(k)=getframe;
end

我猜变量
dx
在这里是相关的。但你永远不会使用它。看下一段代码

dx=0.001;        % spatial increment

dt=dx/(c0);   % time increment

cons=c0*dt/dx; % constant term of electric and magnetic field equations
你会很快意识到,
cons=1
总是!!因此,您没有使用空间增量

如果我随机将cons改为0.5,我会得到这样一个奇妙的gif:


很好的编码工作

不要发布副本。请关闭您的其他问题!