MATLAB: Index in position 1 exceeds array bounds (must not exceed 1)

for loopMATLAB

i am tring to run this piece of code but i keep getting this error.
i am sure that indexing is right
clc
clear all
close all
N=10;
q2=[linspace(-180,180,N)];
q2=q2*pi/180;
q3=[linspace(-140,140,N)];
q3=q3*pi/180;
size=N^2;
x=zeros(size,1);
y=zeros(size,1);
z=zeros(size,1);
i=1;
for i1=1:1:N
for i2=1:1:N
c2=cos(q2(i1,1));
s2=sin(q2(i1,1));
c3=cos(q3(i2,1));
s3=sin(q3(i2,1));
det11=-(0.41)^2*c3*(c2*(0.41+0.41*c3)-0.41*s2*s3);
x(i,1)=q2(i1,1);
y(i,1)=q3(i2,1);
z(i,1)=det11;
i=i+1
end
end

Best Answer

Your matrices have a single row, however, you are indexing them like a column matrix. Try the following code now.
clc
clear all
close all
N=10;
q2=[linspace(-180,180,N)];
q2=q2*pi/180;
q3=[linspace(-140,140,N)];
q3=q3*pi/180;
size=N^2;
x=zeros(size,1);
y=zeros(size,1);
z=zeros(size,1);
i=1;
for i1=1:1:N
for i2=1:1:N
c2=cos(q2(1,i1)); % <-- changed order of subscripts
s2=sin(q2(1,i1));
c3=cos(q3(1,i2));
s3=sin(q3(1,i2));
det11=-(0.41)^2*c3*(c2*(0.41+0.41*c3)-0.41*s2*s3);
x(i,1)=q2(1,i1);
y(i,1)=q3(1,i2);
z(i,1)=det11;
i=i+1
end
end
Related Question