Error message when trying to plot 3D surface plot with colour bar

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

 Accepted Answer

"'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

thank you that has produced a figure like this but as you can see it is blank
this is the gaussian-siedel solver but it is not producing sigma or error values so its not functioning properly but I cant seem to see where it is going wrong. for the purpose of the code to run I've put in values for A and B from the original workspace photo and displayed sigma and error as L2 and L5 respectively which is displaying as zero
A=(2121*2121);
B= (1*2121);
tolerance = 1e-6;% gaussian-siedel solver
maxIterations = 100;
[n, ~] = size(A);
C = zeros(n, 1);
error = Inf;
iteration = 0;
while error > tolerance && iteration < maxIterations
C_old = C;
for i = 1:n
sigma = 0;
for j = 1:n
if j ~= i
sigma = sigma + A(i, j) * C(j);
end
end
C(i) = (B(i) - sigma) / A(i, i);
end
error = norm(C - C_old, inf);
iteration = iteration + 1;
end
L2=sigma
L2 = 0
L5= error
L5 = 0
That your plot is blank indicates that your Ccp(:,:,end) is all NaNs.
When I ran the code in the question (including my suggested modification), using the variable values in the screen shot (except for C since I only know its size), I got a non-blank plot, as you can see:
Lx = 50;
Ly = 1;
T = 100;
dx = 0.5;
dy = 0.05;
dt = 0.5;
C = rand(2121,1);
m = 201;
%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(:,:,end)) % 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
If you would like help debugging your implementation of the Gauss-Seidel method as it applies to your A and B, please save your A and B to a mat file and upload it using the paperclip button.
The A and B are coming up as zeros i dont think theres anything wrong with the boundaries that are set. I've attached the m file and im going to compare the different codes that we've written to see if i can see why im not getting any values
You have:
for j=Ny
on line 48 and line 92. I suspect that should be:
for j=1:Ny
And on line 103 there are a couple of what look like typos (tau should be Tau and jj should be j).
Making those changes produces a non-blank surface plot:
Computing2024_modified
Thank you I was going through and noticied some of these inconsistencies. Hopefully now i can run through the rest of my code without any issues.
You're welcome! If my answer solved the original problem, please "Accept" it. Thanks!
When I look through the code it only produces non-zero values at T=100 why are any values for the iterations between 0-100. Part of my next step is to show the concentration at T=1, 5, 10 ... 100 but im only producing T= 100. Are the values not being stored in the iterations before the last?
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.

Sign in to comment.

More Answers (0)

Products

Release

R2023a

Asked:

on 18 Apr 2024

Commented:

on 19 Apr 2024

Community Treasure Hunt

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

Start Hunting!