matlab中这是个什么问题 Subscript indices must either be real positive integers or logicals.程序如下na=1.926;nb=3.23;n1=1;n2=1;nc=1.585;nd=3.55;for d=400:1600;c3=0;c1=asin(n1*sin(c3)/na);c2=asin(na*sin(c1)/nb);c4=asin(nb*sin(c2)/n2);cc=asi

来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/08 09:13:15
matlab中这是个什么问题Subscriptindicesmusteitherberealpositiveintegersorlogicals.程序如下na=1.926;nb=3.23;n1=1;n

matlab中这是个什么问题 Subscript indices must either be real positive integers or logicals.程序如下na=1.926;nb=3.23;n1=1;n2=1;nc=1.585;nd=3.55;for d=400:1600;c3=0;c1=asin(n1*sin(c3)/na);c2=asin(na*sin(c1)/nb);c4=asin(nb*sin(c2)/n2);cc=asi
matlab中这是个什么问题 Subscript indices must either be real positive integers or logicals.
程序如下
na=1.926;nb=3.23;n1=1;n2=1;nc=1.585;nd=3.55;
for d=400:1600;
c3=0;c1=asin(n1*sin(c3)/na);c2=asin(na*sin(c1)/nb);c4=asin(nb*sin(c2)/n2);cc=asin(nc*sin(c3)/na);cd=asin(nd*sin(c3)/na);
d1=1064;
a=d1/(4*na);b=d1/(4*nb);c=d1/(4*nc);d=d1/(4*nd);
Ba=2*pi*na*a*cos(c1)/d;
Bb=2*pi*nb*b*cos(c2)/d;Bc=2*pi*nc*c*cos(cc)/d;
Bd=2*pi*nd*d*cos(cd)/d;
f=4*pi*1e-7;
e=1e-9/(36*pi);
m=sqrt(e/f);
za=m*cos(c1)*na;zb=m*cos(c2)*nb;z1=f*cos(c3)*n1;z2=f*cos(c4)*n2;zc=m*cos(cc)*nc;zd=m*cos(cd)*nd;
p1=cos(Bb);p2=-i*sin(Bb)/zb;p3=-i*zb*sin(Bb);p4=cos(Bb);
P=[p1 p2;p3 p4];
q1=cos(Ba);q2=-i*sin(Ba)/za;q3=-i*za*sin(Ba);q4=cos(Ba);
Q=[q1 q2;q3 q4];
c1=cos(Bc);c2=-i*sin(Bc)/zc;c3=-i*zc*sin(Bc);c4=cos(Bc);
C=[c1 c2;c3 c4];
d1=cos(Bd);d2=-i*sin(Bd)/zd;d3=-i*zb*sin(Bd);d4=cos(Bd);
D=[d1 d2;d3 d4];
O1=Q*P*Q*P*C*P*Q*D*Q*D *C* D*Q*D*Q*P*C*P*Q*P*Q;
O11=O1(1,1);O12=O1(1,2);O13=O1(2,1);O14=O1(2,2);
z1=sqrt(e/f)*n1*cos(c3);z2=sqrt(e/f)*n2*cos(c4);
t=2*z1/(z1*(O11+z2*O12)+O13+z2*O14);
r=(z1*(O11+z2*O12)-O13-z2*O14)/(z1*(O11+z2*O12)+O13+z2*O14);
t1=abs(t);
r1=abs(r);
s=d-399;
k(1,s)=t1;
end
d=400:1600;
plot(d,k)
错误:On line 29 ==> k(1,s)=t1;
而以下程序却是对的,能运行出来,没有错误,这是为什么?
na=2.10;nb=1.46;n1=1;n2=1;
for d=400:1600;
c3=0;c1=asin(n1*sin(c3)/na);c2=asin(na*sin(c1)/nb);c4=asin(nb*sin(c2)/n2);
d1=1064;
a=d1/(4*na);b=d1/(4*nb);
Ba=2*pi*na*a*cos(c1)/d;
Bb=2*pi*nb*b*cos(c2)/d;
f=4*pi*1e-7;
e=1e-9/(36*pi);
m=sqrt(e/f);
za=m*cos(c1)*na;zb=m*cos(c2)*nb;z1=f*cos(c3)*n1;z2=f*cos(c4)*n2;
p1=cos(Bb);p2=-i*sin(Bb)/zb;p3=-i*zb*sin(Bb);p4=cos(Bb);
P=[p1 p2;p3 p4];
q1=cos(Ba);q2=-i*sin(Ba)/za;q3=-i*za*sin(Ba);q4=cos(Ba);
Q=[q1 q2;q3 q4];
O=Q*P;O1=O^27;
O11=O1(1,1);O12=O1(1,2);O13=O1(2,1);O14=O1(2,2);
z1=sqrt(e/f)*n1*cos(c3);z2=sqrt(e/f)*n2*cos(c4);
t=2*z1/(z1*(O11+z2*O12)+O13+z2*O14);
r=(z1*(O11+z2*O12)-O13-z2*O14)/(z1*(O11+z2*O12)+O13+z2*O14);
t1=abs(t);
r1=abs(r);
s=d-399;
k(1,s)=t1
end
d=400:1600;
plot(d,k)
可以怎样改进,

matlab中这是个什么问题 Subscript indices must either be real positive integers or logicals.程序如下na=1.926;nb=3.23;n1=1;n2=1;nc=1.585;nd=3.55;for d=400:1600;c3=0;c1=asin(n1*sin(c3)/na);c2=asin(na*sin(c1)/nb);c4=asin(nb*sin(c2)/n2);cc=asi
你第一个程序中第5行“a=d1/(4*na);b=d1/(4*nb);c=d1/(4*nc);d=d1/(4*nd);   ”这里你把d的值改了变成了74.93(而你for循环的递增变量是d),后面s=d-399的时候s就不是整数了.把你的d=d1/(4*nd);   这个变量换个名字,比如dd=di/(4*nd),后面的Bd=2*pi*nd*d*cos(cd)/d;也换(这句中用到两个d,而你的意思显然两个d不是指的同一个值),把他改成Bd=2*pi*nd*dd*cos(cd)/d;
这样之后运行结果如下图.
    变量命名的时候尽量使其能反映该变量的含义或者用途,能少掉很多麻烦.