Trouble with an array

1 次查看(过去 30 天)
Hello,
I have some trouble with an array. I try to set the values for a 3D array (nx x ny x nz) that all inner nodes are set to a defined value, all surface nodes (except the edge nodes) are set to a defined value too. It’s like a small cube in a greater cube.
Here is the code I used:
clc;
clear all;
clf;
format short g
fontSize = 18;
% Domaine
L=0.1;
B=0.1;
H=0.1;
nx=5;
ny=5;
nz=5;
dx=L/(nx-1);
dy=B/(ny-1);
dz=H/(nz-1);
% Nodes
Tn=zeros(nx,ny,nz);
x=linspace(0,L,nx);
y=linspace(0,H,ny);
z=linspace(0,B,nz);
[X,Y,Z]=meshgrid(x,y,z);
K=zeros(nx,ny,nz);
K([1 end],:,:)=alpha;
K(:,[1 end],:)=alpha;
K(:,:,[1 end])=alpha;
% inner nodes
T=zeros(nx,ny,nz);
T(:,:,:)=700;
t=0;
% surrounding surfaces
Tu=600; % bottom
To=900; % top
Tl=400; % left
Tr=800; % rigth
Tv=300; % front
Th=300; % back
% side surfaces
Tn(1,2:nx-2,2:nz-2)=Tv; % front
Tn(ny,2:nx-2,2:nz-2)=Th; % back
Tn(2:ny-2,2:nx-2,1)=Tu; % bottom
Tn(2:ny-2,2:nx-2,nz)=To; % top
Tn(2:ny-2,1,2:nz-2)=Tl; % left
Tn(2:ny-2,nx,2:nz-2)=Tr; % rigth
% merge inner nodes
Tn(2:ny-2,2:nx-2,2:nz-2) = T(2:ny-2,2:nx-2,2:nz-2);
f = figure(1);
f.Position(3:4) = [1080 840];
%XY
subplot(2,2,1)
slice(X,Y,Z,Tn,[],[],[0,H],'cubic');
set(gca,'FontSize',fontSize)
colormap(jet)
xlabel('X -Axis','FontSize', fontSize)
ylabel('Y -Axis','FontSize', fontSize)
zlabel('Z -Axis','FontSize', fontSize)
axis([0 L 0 B 0 H]);
view(30,30);
% XZ
subplot(2,2,2)
slice(X,Y,Z,Tn,[],[0,B],[],'cubic');
set(gca,'FontSize',fontSize)
colormap(jet)
%set ( gca, 'ydir', 'reverse' )
xlabel('X -Axis','FontSize', fontSize)
ylabel('Y -Axis','FontSize', fontSize)
zlabel('Z -Axis','FontSize', fontSize)
axis([0 L 0 B 0 H]);
view(30,30);
% YZ
subplot(2,2,3)
slice(X,Y,Z,Tn,[0,L],[],[],'cubic');
set(gca,'FontSize',fontSize)
colormap(jet)
%set ( gca, 'ydir', 'reverse' )
xlabel('X -Axis','FontSize', fontSize)
ylabel('Y -Axis','FontSize', fontSize)
zlabel('Z -Axis','FontSize', fontSize)
axis([0 L 0 B 0 H]);
view(30,30);
% 3D
subplot(2,2,4)
slice(X,Y,Z,Tn,[0,L/2],[B/2,B],[0],'linear');
set(gca,'FontSize',fontSize)
set(gca,'YDir','normal')
colormap(jet)
set ( gca, 'xdir', 'reverse' )
set ( gca, 'ydir', 'reverse' )
xlabel('X -Axis','FontSize', fontSize)
ylabel('Y -Axis','FontSize', fontSize)
zlabel('Z -Axis','FontSize', fontSize)
axis([0 L 0 B 0 H])
view(-150,30);
hold on
%shading(gca,'interp')
p = get(subplot(2,2,4),'Position');
cb=colorbar('Position', [0.93 0.25 0.025 0.6]);
set(cb,'FontSize',fontSize);
caxis([0, 900]);
The result I achieve is this:
I marked the false areas with a red sign. I indexed from 2:nx-1 and so on, but it's not working.
The result should look like this:
I don't know where the error is. Maybe someone has a clue how to fix this.
Greetings
Steffen

采纳的回答

UDAYA PEDDIRAJU
UDAYA PEDDIRAJU 2024-8-23
Hey Steffen,
As per my understanding the following issue is found and modify in the code accordingly:
Indexing: The indexing for the surface nodes are suggested to use "2:nx-1, 2:ny-1," and "2:nz-1' instead of "2:nx-2, 2:ny-2," and "2:nz-2". This ensures that the edge nodes are excluded from the surface assignments.
  1 个评论
Steffen B.
Steffen B. 2024-8-26
Hey Udaya,
thanks for your helpfull answer.
You are rigth the inedxing was the problem. Now's it's working like intended.
Have a nice day

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for USB Webcams 的更多信息

标签

产品


版本

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by