Problem with 3D Meshgrid – Not understanding why some planes are 0 which results in my dx becoming 0
Hello,
For context: Trying to make a Navier Stokes solver in Matlab, starting with 1D just to see if it will work.
To start I am using the meshgrid do discretize the spatial domain into grids. This is done with the following code:
L = 1 % Length of the cube side
n = 100 % Num of gridpoints
x = linspace(0, L, n);
y = linspace(0, L, n);
z = linspace(0, L, n);
[X, Y, Z] = meshgrid(x,y,z);
Now this works and I get three 100x100x100 matrices for X, Y and Z. The problem Im having is understanding how it actually works in generating each individual element. I understand that it reallocates the points in a grid formation from the input values, which is how I want it. However when testing it with this code:
for t=1:T
for i = 2:n-1
for j = 2:n-1
for k = 2:n-1
dx = X(i+1,j,k) – X(i,j,k);
dy = Y(i+1,j,k) – Y(i,j,k);
dz = Z(i+1,j,k) – Z(i,j,k);
if dx <= 1e-10
continue;
else
disp(dx)
end
end
end
u_convection = -0.5/dx * (U(i+1)^2 – U(i-1)^2);
u_diffiusion = nu/dx^2 * (U(i+1) – 2*U(i) + U(i-1));
U_new(i) = U(i) + dt*(u_convection + u_diffiusion);
end
end
U = U_new;
end
The dx terms result in 0 for all the iterations. At this point im just trying for 1D just to make sure it works so dont mind the nested loops, those are for later usage.
From what I can see the X matrix has alot of concecutive values, like X(1,88,56) through to X(99,88,56) all have the value 0.8788. Thus it is logical that dx results in 0.
Could somone please explain why we have the same values in the same "vector" of X, as I dont really understand the documentation of gridmesh(). Also if anybody has any ideas to make the dx term work that would be appriacated as well.
Thanks in advance!Hello,
For context: Trying to make a Navier Stokes solver in Matlab, starting with 1D just to see if it will work.
To start I am using the meshgrid do discretize the spatial domain into grids. This is done with the following code:
L = 1 % Length of the cube side
n = 100 % Num of gridpoints
x = linspace(0, L, n);
y = linspace(0, L, n);
z = linspace(0, L, n);
[X, Y, Z] = meshgrid(x,y,z);
Now this works and I get three 100x100x100 matrices for X, Y and Z. The problem Im having is understanding how it actually works in generating each individual element. I understand that it reallocates the points in a grid formation from the input values, which is how I want it. However when testing it with this code:
for t=1:T
for i = 2:n-1
for j = 2:n-1
for k = 2:n-1
dx = X(i+1,j,k) – X(i,j,k);
dy = Y(i+1,j,k) – Y(i,j,k);
dz = Z(i+1,j,k) – Z(i,j,k);
if dx <= 1e-10
continue;
else
disp(dx)
end
end
end
u_convection = -0.5/dx * (U(i+1)^2 – U(i-1)^2);
u_diffiusion = nu/dx^2 * (U(i+1) – 2*U(i) + U(i-1));
U_new(i) = U(i) + dt*(u_convection + u_diffiusion);
end
end
U = U_new;
end
The dx terms result in 0 for all the iterations. At this point im just trying for 1D just to make sure it works so dont mind the nested loops, those are for later usage.
From what I can see the X matrix has alot of concecutive values, like X(1,88,56) through to X(99,88,56) all have the value 0.8788. Thus it is logical that dx results in 0.
Could somone please explain why we have the same values in the same "vector" of X, as I dont really understand the documentation of gridmesh(). Also if anybody has any ideas to make the dx term work that would be appriacated as well.
Thanks in advance! Hello,
For context: Trying to make a Navier Stokes solver in Matlab, starting with 1D just to see if it will work.
To start I am using the meshgrid do discretize the spatial domain into grids. This is done with the following code:
L = 1 % Length of the cube side
n = 100 % Num of gridpoints
x = linspace(0, L, n);
y = linspace(0, L, n);
z = linspace(0, L, n);
[X, Y, Z] = meshgrid(x,y,z);
Now this works and I get three 100x100x100 matrices for X, Y and Z. The problem Im having is understanding how it actually works in generating each individual element. I understand that it reallocates the points in a grid formation from the input values, which is how I want it. However when testing it with this code:
for t=1:T
for i = 2:n-1
for j = 2:n-1
for k = 2:n-1
dx = X(i+1,j,k) – X(i,j,k);
dy = Y(i+1,j,k) – Y(i,j,k);
dz = Z(i+1,j,k) – Z(i,j,k);
if dx <= 1e-10
continue;
else
disp(dx)
end
end
end
u_convection = -0.5/dx * (U(i+1)^2 – U(i-1)^2);
u_diffiusion = nu/dx^2 * (U(i+1) – 2*U(i) + U(i-1));
U_new(i) = U(i) + dt*(u_convection + u_diffiusion);
end
end
U = U_new;
end
The dx terms result in 0 for all the iterations. At this point im just trying for 1D just to make sure it works so dont mind the nested loops, those are for later usage.
From what I can see the X matrix has alot of concecutive values, like X(1,88,56) through to X(99,88,56) all have the value 0.8788. Thus it is logical that dx results in 0.
Could somone please explain why we have the same values in the same "vector" of X, as I dont really understand the documentation of gridmesh(). Also if anybody has any ideas to make the dx term work that would be appriacated as well.
Thanks in advance! meshgrid, matlab, navier stokes, derivative, fluids, finite difference method MATLAB Answers — New Questions