使用Meshgrid查找速度值时如何绘制2D对流问题?

时间:2019-03-13 04:17:43

标签: matlab for-loop if-statement nested mesh

我目前正在尝试使用迎风方案求解二阶2D对流方程。首先,任务是绘制quiver()图,然后将其覆盖在contourf()的顶部。当使用速度u和v的数据进入迎风方案时,我得到的直线输出如下所示。但是,我使用的初始条件为phi0 = cos(x)。检查X,Y,u,v的值时,它们在(i,j)位置都具有不同的值。我看到我的phi0和phi在每一列下保持不变,但对流的输出在不同时间应该是不同的。我遵循了我的一维二阶代码,该代码可以完美运行,但似乎无法获得平移的移动图。关于我的设置的任何建议,或者如果您可以指出该图在哪里出了问题,将会有很大帮助!

clear all
clc

%Problem 1
%Part B
%Creating a quiver plot for the 2D vector profile
L=2*pi;%Length Lx=Ly = 2pi
L0=0;
N=31; % Nx=Ny=31
%get a value of dx=dy to know distance between steps 
dx=L/N;
dy=L/N
 x=L0-dx*2:dx:L+dx*2;
 y=L0-dy*2:dy:L+dy*2;

[X,Y]=meshgrid(x,y);

u=cos(X).*sin(Y);
v=-sin(X).*cos(Y);

figure
hold on


%Part B & C using the courtf plot Phi=cos(x)
phi0=cos(X);
contourf(X,Y,phi0)
colormap autumn
colorbar
xlabel('Length from 0 to 2*pi with dx spacing')
ylabel('Length from 0 to 2*pi with dy spacing')
title('Quiver plot on the phi=cos(x) Contour Plot')
quiver(X,Y,u,v)
hold off

figure(2)
plot(x,phi0)
%%
%Writing a code to sovle 2D advection for  part D
t=0; %initial time
tmax=10; %Maximum time
dt=0.01; %time step

phi=phi0;
phip1=phi0;

%phi(:)=phi; %Initial Condition
nsteps = tmax/dt
%Add periodic boundary conditions for both x & y direction
for n=1 : nsteps
    phi(1,:) = phi(end-2,:);
    phi(2,:) = phi(end-3,:);
    phi(end,:) = phi(3,:);
    phi(end-1,:) = phi(4,:);
    %Y ghost cells
    phi(:,1) = phi(:,end-2);
    phi(:,1) = phi(:,end-3);
    phi(:,end) = phi(:,3);
    phi(:,end-1) = phi(:,4);

    for i=3:N+1
        for j=3:N+1

          if u > 0 & v>0
               phip1(i,j)= phi(i,j) - u(i)*dt/(2*dx) * (3*phi(i,j)-4*phi(i-1,j)+phi(i-2,j))- v(j)*dt/(2*dx) *(3*phi(i,j)-4*phi(i,j-1)+phi(i,j-2))
          elseif u <0 & v<0
               phip1(i,j)= phi(i,j) - u(i)*dt/(2*dx) * (-3*phi(i,j)+4*phi(i+1,j)-phi(i+2,j))- v(j)*dt/(2*dx) *(-3*phi(i,j)+4*phi(i,j+1)-phi(i,j+2))
          elseif u >0 & v <0
               phip1(i,j)= phi(i,j) - u(i)*dt/(2*dx) * (3*phi(i,j)-4*phi(i+1,j)+phi(i+2,j))- v(j)*dt/(2*dx) *(-3*phi(i,j)+4*phi(i,j-1)-phi(i,j-2))
          elseif u <0 & v >0
               phip1(i,j)= phi(i,j) - u(i)*dt/(2*dx) * (-3*phi(i,j)+4*phi(i-1,j)-phi(i-2,j))- v(j)*dt/(2*dx) *(3*phi(i,j)-4*phi(i,j+1)+phi(i,j+2))
          end
        end
    end
    t=t+dt;
    phi=phip1;


    plot(x,phi)
    %pause(0.5)
end

1 个答案:

答案 0 :(得分:1)

我看到的问题是您正在针对phi(即1D)绘制x(即2D)。

我不确定要绘制的正确部分是否100%正确,但是类似的内容应该起作用:plot(x,phi0(1,:))。这样做是在phi方向上绘制y的第一片。

enter image description here

编辑

要可视化phi0作为XY的功能,可以使用surf(X,Y,phi0)mesh(X,Y,phi0)