Error using mesh (line 71): Z must be a matrix, not a scalar or vector.
4 views (last 30 days)
Show older comments
Hello,
So I'm trying to make a 3-D plot using the mesh function of a metallic prism to understand the behavior of an electric potential around it and on the surface of the object and I'm using a numerical method called the Jacobi relaxation method for Laplace's equation and I keep getting the same error message:
"Z must be a matrix, not a scalar or vector."
and it follows by:
Error in <filename> (line 28) mesh(V_new, 'FaceColor', 'interp');
and I'm struggling to figure out why it's giving me this message? Here's my code:
8- %Load array into V
9- [V] = Initialize_prism;
11- %Run update routine and estimate convergence
13- [V_new, delta_V_new] = Update_prism(V);
15- %Initialize loop counter
16- loops = 0;
18- % Loops = 0 while we have not met the convergence criterion and the number of loops <10 so that we give the relaxation algorithm time to converge while (delta_V_new > & loops <30);
22- while (delta_V_new>4e-5||loops<20);
23- loops = loops+1
24- [V_new, delta_V_new] = Update_prism(V_new);
26- %Draw the surface using the mesh function
28- mesh(V_new,'FaceColor','interp');
29- title('Potential Surface');
30- axis([0, 20, 0, 20, 0, 1]);
31- drawnow;
33- %Insert a pause here so we can see the evolution of the potential surface
35- pause(0.5);
end
38- %This function creates the initial voltage array V
39- function[V] = Initialize_prism;
40- %Clear Variables
41- clear;
43- V = [00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000001111110000000
00000001111110000000
00000001111110000000
00000001111110000000
00000001111110000000
00000001111110000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000
00000000000000000000];
end
66- function[V_new, delta_V_new] = Update_prism(V);
73- row_size = size (V,1);
74- column_size = size(V,2);
76- %Preallocate memory for speed
78- V_new = V;
79- delta_V_new = 0;
84- for j=2:column_size-1;
for i=2:row_size-1;
if V(i,j)~=1;
V_new(i,j) = (V(i-1,j)+V(i+1,j)+V(i,j-1)+V(i,j+1))/4;
delta_V_new = delta_V_new+abs(V_new(i,j)-V(i,j));
else
V_new(i,j) = V(i,j);
end
end
end
end
0 Comments
Answers (1)
Charan Jadigam
on 11 Mar 2020
Hi, I could understand from the question that you are trying to plot electric potential around and on the metallic prism, but the input potential ‘V’ is given as column vector instead of matrix. Mesh considers the row and column indices as the x and y co-ordinates. But ‘V_new’ is a column vector. So, I would suggest reshaping the electric potential to a matrix with the size of metallic prism and try again. You can reshape a matrix using the following function,
A = 1:10;
B = reshape(A, [5,2])
You can know more about reshape and rearranging the arrays here
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!