Error message when trying to plot 3D surface plot with colour bar
3 views (last 30 days)
Show older comments
We are trying to create a surface plot to analyse the concentration of a advection diffusive equation. I'm experiencing two problems one is for my solver and the second is that my surf plot is coming up with an error. I think we've corrected the solver error but as we can't see the plot can't be sure if it's working. I've included the code that defines the mesh grid and matrix that is supposed to create the surface plot, the error message and all the values from the workspace in a picture. I'm n
%mesh grid/ line 18
x = 0:dx:Lx; %x nodes/ line 19
y = 0:dy:Ly; %y nodes/ line 20
t = 0:dt:T; %time intervals/ line 21
[X,Y] = meshgrid(x,y); % Create a meshgrid for X and Y/ line 23
% Initialize conditions and variables/line 25
Nx = length(x); %x nodes/line 26
Ny = length(y); %y nodes/line 27
Nt = length(t); %length of time intervals/ line 28
Cc = zeros(Nx, Ny, Nt); %line 29
Cc(:,:,1)=0; %line 30
for ik=1:length(C) %creating 3D matrix/ line 159
ix=floor(ik/Ny)+1;
iy=ik-(ix-1)*Ny;
if iy==0
iy=Ny;
ix=ix-1;
end
Cc(ix,iy,m+1)=C(ik);
end % line 167
Ccp = permute(Cc, [2, 1, 3]); % line 169
figure % Create a new figure
surf(X,Y,Ccp) % Create a surface plot (this is line 172)
% Enhance the plot with labels and colorbar
title('Concentration at Final Time Step');
xlabel('X');
ylabel('Y');
zlabel('Concentration');
colorbar; % Adds a colorbar to indicate the scale of concentration
view(2); %view from top
Error message:
Error using surf
Data dimensions must agree.
Error in Computing2024 (line 172)
surf(X,Y,Ccp) % Create a surface plot

0 Comments
Accepted Answer
Voss
on 18 Apr 2024
"'Concentration at Final Time Step'" would seem to indicate that you want to plot a surface from the last page of the 3D array Ccp, so:
surf(X,Y,Ccp(:,:,end))
8 Comments
Voss
on 19 Apr 2024
In your for loops where A and B are calculated, B depends on m (the time loop index) but A does not. So you're calculating the same A for each m. Actually, you're calculating the same B for each m too, because, although B is calculated from Cc(i,j,m), Cc is all zeros, so you're getting the same B for each m. If that's what's supposed to be happening, then you might as well just calculate A and B once instead of Nt times and do away with the for m = 1:Nt.
Then, after A and B are calculated, C is calculated from A and B in the while loop.
Then, Cc is calculated, but only the m+1th page of Cc (i.e., the Nt+1th page, because m has value Nt after the for m loop):
Cc(ix,iy,m+1)=C(ik)
% ^^^ no other page of Cc besides m+1 is updated
This makes size(Cc,3) equal to Nt+1, and the transpose of that last page of Cc is what's shown in the surface plot. No other page of Cc has anything but zeros because rest of Cc was initialized to zeros and remains zeros.
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
