Matrix must be square 出错求助
function f=p1fun(x,w1,w2)t=1000*1540;
l=0.0045;
k=2*pi/0.0015;
a=5;
n=length(x);
m=n/3;
i=1:m;
xx(i)=-0.0105+0.00525*rem((i-1),5)+x(i+m);
w=(i-1)/5;
yy(i)=0.0105-0.00525*floor(w)+x(i+2*m);
R1(i)=(0.01+(w1-xx(i)).^2+(w2-yy(i)).^2)^(1/2);
R2(i)=(0.01+xx(i).^2+yy(i).^2)^(1/2);
t1(i)=(R2(i)-0.1)*k;
q1(i)=(k*(w1-xx(i))*l)/2*R1(i)+eps;
s1(i)=(k*(w2-yy(i))*l)/2*R1(i)+eps;
A1(i)=sin(q1(i))/q1(i);
B1(i)=sin(s1(i))/s1(i);
p1(i)=abs(j*x(i)*t*l.^2*exp(j*t1(i))*exp(-j*k*R1(i))*A1(i)*B1(i)/(0.001*R1(i)));
f=sum(p1);
该函数以前是用for循环实现的,我想尽量用矢量化,来节省时间.但是出现不少问题,请版主指点下,是否能避免使用for循环.
错误描述:
??? Error using ==> ^
Matrix must be square.
Error in ==> E:\matlab\work\P1fun.m
On line 15==> R1(i)=(0.01+(w1-xx(i)).^2+(w2-yy(i)).^2)^(1/2);
输入变量x为75维,即n=75,m=25;
[ 本帖最后由 eight 于 2007-11-29 10:20 编辑 ] 原帖由 lyx_812 于 2007-11-29 00:35 发表 http://www.chinavib.com/forum/images/common/back.gif
function f=p1fun(x,w1,w2)
t=1000*1540;
l=0.0045;
k=2*pi/0.0015;
a=5;
n=length(x);
m=n/3;
i=1:m;
xx(i)=-0.0105+0.00525*rem((i-1),5)+x(i+m);
w=(i-1)/5;
yy(i)=0.0105-0.00525*floor(w) ...
出错问题和矢量化问题都可以在置顶帖中找到,尤其是前者,最后那个 ^ 应该是点运算。另外,请学会看错误提示,这个在置顶帖中也能找到 谢谢院长,我再看看! function f=p1fun(x,w1,w2)
t=1000*1540;
l=0.0045;
k=2*pi/0.0015;
a=5;
n=length(x);
m=n/3;
i=1:m;
xx(i)=-0.0105+0.00525*rem((i-1),5)+x(i+m);
w=(i-1)/5;
yy(i)=0.0105-0.00525*floor(w)+x(i+2*m);
R1(i)=(0.01+(w1-xx(i)).^2+(w2-yy(i)).^2).^(1/2);
R2(i)=(0.01+xx(i).^2+yy(i).^2).^(1/2);
t1(i)=(R2(i)-0.1)*k;
q1(i)=(k.*(w1-xx(i)).*l)/2.*R1(i)+eps;
s1(i)=(k.*(w2-yy(i)).*l)/2.*R1(i)+eps;
A1(i)=sin(q1(i))/q1(i);
B1(i)=sin(s1(i))/s1(i);
p1(i)=abs(j.*x(i).*t*l.^2.*exp(j*t1(i)).*exp(-j*k.*R1(i)).*A1(i).*B1(i)/(0.001*R1(i)));
f=sum(p1);
改过之后运行正常,之前用for循环时好象对"点"的要求不是很严格.多谢院长.
页:
[1]