Filter löschen
Filter löschen

error >> Matrix dimensions must agree. How to fix

15 Ansichten (letzte 30 Tage)
Ofer Aluf
Ofer Aluf am 23 Apr. 2024
Bearbeitet: Torsten am 23 Apr. 2024
Hello,
I have script:
d=0.025:0.0025:1; % d=0.125*Lambda to 12.5*Lambda
Lambda=1;
% Ga function
Lambda=0.2;l1= Lambda/4;zi=l1;si=1;k=(2*pi)/Lambda;
z=0:Lambda/400:Lambda/4;
y= sqrt(d.^2+(z-zi));
Arrays have incompatible sizes for this operation.
Gfun=@(z)(1./y).*exp(-1i.*k.*y).*exp(-1i.*k.*si.*z);
% R=sqrt(d^2+(z-zi));
% evaluate the integral from z=0 to l1
Ga=integral(Gfun,0,Lambda/4);
Question? Why i got message "Matrix dimensions must agree"? and how to fix it??

Antworten (3)

Anjaneyulu Bairi
Anjaneyulu Bairi am 23 Apr. 2024
Bearbeitet: Anjaneyulu Bairi am 23 Apr. 2024
Hi,
Below line causing the error and it says "z" and "zi" are incompatible sizes for this operation.
y= sqrt(d.^2+(z-zi));
The "d.^2" variable has size of 1*391 double and "(z-zi)" has size of 1*101 double. Make sure you have declared these variables correctly to avoid these kind of errors.
You can refer below documentation links to know more information on arrays
I hope above information is helpful to resolve your query

Lokesh
Lokesh am 23 Apr. 2024
Hi Ofer,
It appears that you are encountering the error "Matrix dimensions must agree" while running your MATLAB script.
This issue arises because the dimensions of "d.^2" (1x391) and "z-zi" (1x101) in the equation "y= sqrt(d.^2+(z-zi));" are not compatible, leading to the error.
Please refer to the following MATLAB documentation for more information about compatible array sizes for basic operations:
Hope this helps!

Torsten
Torsten am 23 Apr. 2024
Bearbeitet: Torsten am 23 Apr. 2024
z doesn't need to be prescribed in array form because you integrate with respect to z. Here I assume that the integration variable and the z in the expression for y are the same.
If you mean to evaluate your integral for the d-values that you defined, you can use
d=0.025:0.0025:1; % d=0.125*Lambda to 12.5*Lambda
% Ga function
Lambda=0.2;l1=Lambda/4;zi=l1;si=1;k=(2*pi)/Lambda;
y= @(d,z)sqrt(d^2+(z-zi));
Gfun =@(d,z)(1./y(d,z)).*exp(-1i.*k.*y(d,z)).*exp(-1i.*k.*si.*z);
Ga=arrayfun(@(d)integral(@(z)Gfun(d,z),0,Lambda/4),d);
plot(d,[real(Ga);imag(Ga)])

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by