Z must be a matrix, not a scalar or vector

clc
clear all
R=10;
f=0.5;
s=50;
N=36;
a=f/(s*N);
Ax=0.002;
Ay=0.002;
lx=2.5;
ly=2.5;
n=((R*s*N)/f)+1;
for i=1:n
r(i)=(R-((i-1)*a));
theta(i)= ((i-1)*(2*pi/N));
x(i)=(r(i)*cos(theta(i)));
y(i)=(r(i)*sin(theta(i)));
[xx(i),yy(i)]=meshgrid(x(i),y(i));
Z(i)=((Ax*cos(2*pi*xx(i)/lx))+(Ay*cos(2*pi*yy(i)/ly)));
end
surf(xx,yy,Z);

2 Comments

You forgot to ask a question.
I want 3d plot and every time i am getting an error that Z must be a matrix, not a scalar or vector

Sign in to comment.

 Accepted Answer

R=10;
f=0.5;
s=50;
N=36;
a=f/(s*N);
Ax=0.002;
Ay=0.002;
lx=2.5;
ly=2.5;
n=((R*s*N)/f)+1;
for i=1:n
r(i)=(R-((i-1)*a));
theta(i)= ((i-1)*(2*pi/N));
x(i)=(r(i)*cos(theta(i)));
y(i)=(r(i)*sin(theta(i)));
% [xx(i),yy(i)]=meshgrid(x(i),y(i));
Z(i)=((Ax*cos(2*pi*x(i)/lx))+(Ay*cos(2*pi*y(i)/ly)));
end
T=delaunayTriangulation(x(:),y(:));
trisurf(T.ConnectivityList,x(:),y(:),Z(:),'EdgeColor','none','FaceAlpha',0.6);
view(-80,75)

4 Comments

Thank you but can we change Triangulation to other form so that smooth profile can be obtained
To get a smoother profile, increase the number of samples (e.g., with N=360):
R=10;
f=0.5;
s=50;
N=360;
....
T=delaunayTriangulation(x(:),y(:));
trisurf(T.ConnectivityList,x(:),y(:),Z(:),'EdgeColor','none','FaceAlpha',0.6);
view(-80,75)
thank you it worked
You're welcome, but please Accept-click the answer to indicate that it worked.

Sign in to comment.

More Answers (1)

R=10;
f=0.5;
s=50;
N=100;
a=f/(s*N);
Ax=2;
Ay=2;
lx=2.5;
ly=2.5;
n=((R*s*N)/f)+1;
r(0)=0;
Z(0)=0;
t=0.5;
for i=1:n
r(i)=(R-((i-1)*a));
theta(i)= ((i-1)*(2*pi/N));
x(i)=(r(i)*cos(theta(i)));
y(i)=(r(i)*sin(theta(i)));
Z(i)=((Ax*cos(2*pi*x(i)/lx))+(Ay*cos(2*pi*y(i)/ly)));
m(i) = ((Z(i-1) - Z(i))./(r(i-1)-r(i)));
newth(i) = atan(-m(i));
G(i)=r(i)-(t*sin(newth(i)));
H(i)=Z(i)+(t*cos(newth(i)))-t; %% THE compensate value of r1 and Z1
% needed to calculate from the previous value of r(i) and Z(i)
end
T=delaunayTriangulation(x(:),y(:));
trisurf(T.ConnectivityList,x(:),y(:),Z(:),'EdgeColor','none','FaceAlpha',0.6);
view(-80,75)
I am unable to calculate the value of G and H from the previous value of r(i) and Z(i). Can you please check the code. Thank you

10 Comments

r(0)=0;
Array indices must be positive integers or logical values.
Z(0)=0;
Exactly i am also getting the same statement. Actual from r(i) and Z(i), I have to calculate the G and H. r(0)=0 and Z(0)=0, I have randomly initilize. For calculation of slope i.e. m(1)= Z(2)-Z(1)/r(2)-r(1), I need to call the value of r(2) and r(1) from r(i) and same in case of Z(i). I am unable to call this value.
The only part of MATLAB that is able to use indices starting from zero, is Simulink (and maybe Symbolic Toolbox if I dug deeply enough.)
In any case where you have an array being indexed starting from integer S and S < 1 then you need to add (1-S) to each array index expression. For example if your array indices started from -2 then you would have to add (1-(-2)) = 3 to each index expression.
roff = 1;
Zoff = 1;
r(0+roff)=0;
Z(0+Zoff)=0;
t=0.5;
for i=1:n
r(i+roff)=(R-((i-1)*a));
theta(i)= ((i-1)*(2*pi/N));
x(i)=(r(i+roff)*cos(theta(i)));
y(i)=(r(i+roff)*sin(theta(i)));
Z(i)=((Ax*cos(2*pi*x(i)/lx))+(Ay*cos(2*pi*y(i)/ly)));
m(i) = ((Z(i-1+Zoff) - Z(i))./(r(i-1)-r(i+roff)));
newth(i) = atan(-m(i));
G(i)=r(i+roff)-(t*sin(newth(i)));
H(i)=Z(i+Zoff)+(t*cos(newth(i)))-t; %% THE compensate value of r1 and Z1
% needed to calculate from the previous value of r(i) and Z(i)
end
Still same error is showing
Array indices must be positive integers or logical values.
Error in thert1 (line 22)
m(i) = ((Z(i-1+Zoff) - Z(i))./(r(i-1)-r(i+roff)));
R=10;
f=0.5;
s=50;
N=100;
a=f/(s*N);
Ax=2;
Ay=2;
lx=2.5;
ly=2.5;
n=((R*s*N)/f)+1;
roff = 1;
Zoff = 1;
r(0+roff)=0;
Z(0+Zoff)=0;
t=0.5;
for i=1:n
r(i+roff)=(R-((i-1)*a));
theta(i)= ((i-1)*(2*pi/N));
x(i)=(r(i+roff)*cos(theta(i)));
y(i)=(r(i+roff)*sin(theta(i)));
Z(i+Zoff)=((Ax*cos(2*pi*x(i)/lx))+(Ay*cos(2*pi*y(i)/ly)));
m(i) = ((Z(i-1+Zoff) - Z(i))./(r(i-1+roff)-r(i+roff)));
newth(i) = atan(-m(i));
G(i)=r(i+roff)-(t*sin(newth(i)));
H(i)=Z(i+Zoff)+(t*cos(newth(i)))-t; %% THE compensate value of r1 and Z1
% needed to calculate from the previous value of r(i) and Z(i)
end
size(H)
ans = 1×2
1 100001
R=10;
f=0.5;
s=50;
N=100;
a=f/(s*N);
Ax=2;
Ay=2;
lx=2.5;
ly=2.5;
n=((R*s*N)/f)+1;
roff = 1;
Zoff = 1;
r(0+roff)=0;
Z(0+Zoff)=0;
t=0.1;
for i=1:n
r(i+roff)=(R-((i-1)*a));
theta(i)= ((i-1)*(2*pi/N));
x(i)=(r(i+roff)*cos(theta(i)));
y(i)=(r(i+roff)*sin(theta(i)));
Z(i+Zoff)=((Ax*cos(2*pi*x(i)/lx))+(Ay*cos(2*pi*y(i)/ly)));
m(i) = ((Z(i-1+Zoff) - Z(i))./(r(i-1+roff)-r(i+roff)));
newth(i) = atan(-m(i));
G(i)=r(i+roff)-(t*sin(newth(i)));
H(i)=Z(i+Zoff)+(t*cos(newth(i)))-t; %% THE compensate value of G and H
% needed to calculate from the previous value of r(i) and Z(i)
end
size(H)
T=delaunayTriangulation(x(:),y(:));
trisurf(T.ConnectivityList,x(:),y(:),H(:),'EdgeColor','none','FaceAlpha',0.6);
view(-80,75)
It is working but i am not getting the desired value. The value of r(i) and G are same and same case for Z(i) and H. For example if the value of r(1)=1 and Z(1)=4, then after giving the compensation value t=0.1, the new value i.e. G(1) and H(1) should be different from the r(1) and Z(1). Can you please enlighted how to solve this issue. Thank you
No, r(1) needs to be used to store the initial value, r(0+roff) and Z(1) needs to be used to store the initial value Z(0+Zoff) . The first output value is at r(1+roff) and Z(1+zoff)
Still same value for both i.e. r(i) and G is showing
R=10;
f=0.5;
s=50;
N=100;
a=f/(s*N);
Ax=2;
Ay=2;
lx=2.5;
ly=2.5;
n=((R*s*N)/f)+1;
roff = 1;
Zoff = 1;
r(0+roff)=0;
Z(0+Zoff)=0;
t=0.1;
Pi = (pi);
for i=1:n
r(i+roff)=(R-((i-1)*a));
theta(i)= ((i-1)*(2*Pi/N));
x(i)=(r(i+roff)*cos(theta(i)));
y(i)=(r(i+roff)*sin(theta(i)));
Z(i+Zoff)=((Ax*cos(2*Pi*x(i)/lx))+(Ay*cos(2*Pi*y(i)/ly)));
m(i) = ((Z(i-1+Zoff) - Z(i+Zoff))./(r(i-1+roff)-r(i+roff)));
newth(i) = atan(-m(i));
G(i)=r(i+roff)-(t*sin(newth(i)));
H(i)=Z(i+Zoff)+(t*cos(newth(i)))-t; %% THE compensate value of G and H
% needed to calculate from the previous value of r(i) and Z(i)
end
T=delaunayTriangulation(x(:),y(:));
trisurf(T.ConnectivityList,x(:),y(:),H(:),'EdgeColor','none','FaceAlpha',0.6);
view(-80,75)
Thank you, now its working

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!