matlab光强编程,提示矩阵维度 必须一致,不知是哪里出现的问题,代码在下面
代码如下:
clear all;
k=10;
f=1004.8;
x=-1.5:0.01:1.5;
y=-1.5:0.01:1.5;
[x1,y1]=meshgrid(x,y);
r=sqrt(x1.^2+y1.^2);
Phi=atan(y1./x1);
I=(i*k*exp(i*(pi/2+k*f)).*sin(2.*Phi).*quadl(@(theta)(1+cos(theta))./(1-cos(theta)).*sin(theta).*Besselk(2,k.*r.*sin(theta)),2.23,pi)).^2;
subplot(2,1,1);
mesh(x1,y1,I);
axis([-1.5 1.5 -1.5 1.5 0 1]);
subplot(2,1,2);
subimage(I*255);
axis off;
出现错误如下:
矩阵维度必须一致。
出错 formula>@(theta)(1+cos(theta))./(1-cos(theta)).*sin(theta).*Besselk(2,k.*r.*sin(theta))
出错 quadl (line 62)
y = feval(f,x,varargin{:}); y = y(.';
出错 formula (line 9)
I=(i*k*exp(i*(pi/2+k*f)).*sin(2.*Phi).*quadl(@(theta)(1+cos(theta))./(1-cos(theta)).*sin(theta).*Besselk(2,k.*r.*sin(theta)),2.23,pi)).^2;
返回小木虫查看更多
京公网安备 11010802022153号
问题出在,quadl(@(theta)(1+cos(theta))./(1-cos(theta)).*sin(theta).*Besselk(2,k.*r.*sin(theta)),2.23,pi),这个数值积分命令上,该函数要求点对点,即已知值为单一数值(不接受数组数据)。所以你的问题,应用for循环语句来完成。

123.jpg
你好,我按照你的编程输入了下,还是有提示错误,请你在帮我解答下,
clear all;
k=10;
f=1004.8;
x=-0.5:0.01:0.5;
y=-0.5:0.01;0.5
[x1,y1]=meshgrid(x,y);
r=sqrt(x1.^2+y1.^2);
[K,L]=size(r);
for m=1:k
for n=1:L
Phi=atan(y1(n)./x1(m));
r0=r(m,n);
eq=quadl(@(theta)(1+cos(theta))./(1-cos(theta)).*sin(theta).*Besselk(2,k.*r0.*sin(theta)),2.23,pi);
I0=(i*k*exp(i*(pi/2+k*f))*sin(2*Phi)*eq).^2;
I(m,n)=I0;
end
end
Re=real(I);Im=imag(I);
s.FaceColor='flat';
%subplot(2,1,1);
C=rand(K,L);
mesh(x1,y1,real(I).C);hold on
提示错误如下:
formula
ans =
0.5000
未定义与 'double' 类型的输入参数相对应的函数 'Besselk'。
出错 formula>@(theta)(1+cos(theta))./(1-cos(theta)).*sin(theta).*Besselk(2,k.*r0.*sin(theta))
出错 quadl (line 62)
y = feval(f,x,varargin{:}); y = y(.';
出错 formula (line 13)
eq=quadl(@(theta)(1+cos(theta))./(1-cos(theta)).*sin(theta).*Besselk(2,k.*r0.*sin(theta)),2.23,pi);
>>
,
刚我又调试了下,出现如下错误,您再帮我看下:
k=10;
f=1004.8;
x=-0.5:0.01:0.5;
y=-0.5:0.01:0.5;
[x1,y1]=meshgrid(x,y);
r=sqrt(x1.^2+y1.^2);
[K,L]=size(r);
for m=1:k
for n=1:L
Phi=atan(y1(n)./x1(m));
r0=r(m,n);
eq=quadl(@(theta)(1+cos(theta))./(1-cos(theta)).*sin(theta).*besselk(2,k.*r0.*sin(theta)),2.23,pi);
I0=(i*k*exp(i*(pi/2+k*f))*sin(2*Phi)*eq)^2;
I(m,n)=I0;
end
end
Re=real(I);Im=imag(I);
s.FaceColor='flat';
%subplot(2,1,1);
C=rand(K,L);
mesh(x1,y1,real(I).C);hold on
axis([-1.5 1.5 -1.5 1.5 0 1]);
subplot(2,1,2);
subimage(I*255);
axis off
提示错误:
定义变量 "real" 或类 "real"。
出错 para (line 22)
mesh(x1,y1,real(I).C);hold on
real是函数,应该不存在定义变量的问题。可能是软件的问题,重启试一试看。
我试了试其他笔记本的电脑上的版本matlab,还是提示未定义变量‘real ’或类‘real’,请您运行下我的程序,看看是什么问题呢?
定义变量的问题解决了,但是又遇到了一个索引函数编制的问题,问题在代码后面:
clear all;
close all;
clc;
k=10;
f=1004.8;
x=-0.5:0.01:0.5;
y=-0.5:0.01:0.5;
[x1,y1]=meshgrid(x,y);%直角坐标转化为极坐标
r=sqrt(x1.^2+y1.^2);
[K,L]=size(r);
for m=1:k
for n=1:L
Phi=atan(y1(n)./x1(m));
r0=r(m,n);
eq=quadl(@(theta)(1+cos(theta))./(1-cos(theta)).*sin(theta).*besselk(2,k.*r0.*sin(theta)),2.23,pi);
I0=(i*k*exp(i*(pi/2+k*f))*sin(2.*Phi)*eq).^2;
I(m,n)=I0;
end
end
Re=real(I);
Im=imag(I);
s.FaceColor='flat';
%subplot(2,1,1);
C=rand(K,L);
mesh(x1,y1,real(I).C);
hold on ;
axis([-1.5 1.5 -1.5 1.5 0 1]);
subplot(2,1,2);
subimage(I*255);
axis off
错误: 不能使用 {} 或 . 索引为函数编制索引。
出错 para (line 25)
mesh(x1,y1,real(I).C);