Demo entry 6734625

N

   

Submitted by anonymous on Apr 21, 2018 at 05:04
Language: Matlab. Code size: 1.4 kB.

L=2*pi;                  %边界条件
grid_num=20;             %网格数为20个
dt=0.01;                 %时间步长为0.01                
dx=L/grid_num;
u=zeros(5000,20);
u1=zeros(1,20);
u2=zeros(1,20);
test=zeros(1,6);
for j=1:1:grid_num
    u(1,j)=sin(j*dx);    %初值条件
end

%%三步三阶龙格库塔方法
for n=1:1:2000
    for k=1:1:grid_num
        u1(n,k)=u(n,k)+[u(n,mod(20+k-4,20)+1) u(n,mod(20+k-3,20)+1) u(n,mod(20+k-2,20)+1) u(n,mod(20+k-1,20)+1) u(n,mod(20+k-0,20)+1) u(n,mod(20+k+1,20)+1)]*b/dx*dt*(-1);
    end    
    for h=1:1:grid_num
        u2(n,h)=0.75*u(n,h)+0.25*(u1(n,h)+[u1(n,mod(20+h-4,20)+1) u1(n,mod(20+h-3,20)+1) u1(n,mod(20+h-2,20)+1) u1(n,mod(20+h-1,20)+1) u1(n,mod(20+h-0,20)+1) u1(n,mod(20+h+1,20)+1)]*b/dx*dt*(-1));
    end    
    for j=1:1:grid_num
        u(n+1,j)=1/3*u(n,j)+2/3*(u2(n,j)+ [u2(n,mod(20+j-4,20)+1) u2(n,mod(20+j-3,20)+1) u2(n,mod(20+j-2,20)+1) u2(n,mod(20+j-1,20)+1) u2(n,mod(20+j-0,20)+1) u2(n,mod(20+j+1,20)+1)]*b/dx*dt*(-1));
        test(j,:)=[(mod(20+j-4,20)+1), (mod(20+j-3,20)+1), (mod(20+j-2,20)+1), (mod(20+j-1,20)+1), (mod(20+j-0,20)+1), (mod(20+j+1,20)+1)];
    end
end  

%%绘图比较数值解和精确解之间的差别
z=2*pi/20:2*pi/20:2*pi;
figure;plot(z(1,:),u(2000,:),'-r');
hold on
plot(z(1,:),sin(z-20),'-b');title('t=20数值解与精确解比较');
hold on
legend('数值解','精确解');

figure;plot(z(1,:),u(2000,:),'-r');
hold on
plot(z(1,:),sin(z-50),'-b');title('t=50数值解与精确解比较');
hold on
legend('数值解','精确解');

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).