matlab光強(qiáng)編程,提示矩陣維度 必須一致,不知是哪里出現(xiàn)的問題,代碼在下面
代碼如下:
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;
出現(xiàn)錯誤如下:
矩陣維度必須一致。
出錯 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;
返回小木蟲查看更多
京公網(wǎng)安備 11010802022153號
問題出在,quadl(@(theta)(1+cos(theta))./(1-cos(theta)).*sin(theta).*Besselk(2,k.*r.*sin(theta)),2.23,pi),這個數(shù)值積分命令上,該函數(shù)要求點對點,即已知值為單一數(shù)值(不接受數(shù)組數(shù)據(jù))。所以你的問題,應(yīng)用for循環(huán)語句來完成。

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' 類型的輸入?yún)?shù)相對應(yīng)的函數(shù) '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);
>>
,
剛我又調(diào)試了下,出現(xiàn)如下錯誤,您再幫我看下:
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是函數(shù),應(yīng)該不存在定義變量的問題。可能是軟件的問題,重啟試一試看。
我試了試其他筆記本的電腦上的版本matlab,還是提示未定義變量‘real ’或類‘real’,請您運行下我的程序,看看是什么問題呢?
定義變量的問題解決了,但是又遇到了一個索引函數(shù)編制的問題,問題在代碼后面:
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);%直角坐標(biāo)轉(zhuǎn)化為極坐標(biāo)
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
錯誤: 不能使用 {} 或 . 索引為函數(shù)編制索引。
出錯 para (line 25)
mesh(x1,y1,real(I).C);