Matrix dimensions must agree

2 Ansichten (letzte 30 Tage)
Tito
Tito am 31 Mär. 2020
Beantwortet: David Hill am 31 Mär. 2020
Hi, I'm trying to plot a field, but it always shows me the next error:
Matrix dimensions must agree.
Error in codeH (line 24)
Hx = (((1j .* k .* a.^2 .* I0 .* cos(theta)) ./ (x.^2+(z-b).^2)) .* (1 + 1 ./ (1j .*
k .* sqrt(x.^2+(z-b).^2))) .* exp(-1j .* k .* sqrt(x.^2+(z-b).^2))) .*
sin(theta).*cos(phi);
The code that I'm using is this:
clear all
close all
clc
a = 0.4;
f = 10^6;
w = 2 .* pi .* f;
I0 = 1;
b = (0.4/2);
mu0 = 4 * pi * 1e-7;
e0 = 8.85e-12;
k = w .* sqrt( mu0 .* e0 );
theta = (-pi/2:pi/3:pi/2);
phi = (0:pi/2:2*pi);
xx = linspace(-1,0.1,1);
zz = xx;
[x,z] = meshgrid(xx,zz);
Hx = (((1j .* k .* a.^2 .* I0 .* cos(theta)) ./ (x.^2+(z-b).^2)) .* (1 + 1 ./ (1j .* k .* sqrt(x.^2+(z-b).^2))) .* exp(-1j .* k .* sqrt(x.^2+(z-b).^2))) .* sin(theta).*cos(phi);
Hz = (((1j .* k .* a.^2 .* I0 .* cos(theta)) ./ (x.^2+(z-b).^2)) .* (1 + 1 ./ (1j .* k .* sqrt(x.^2+(z-b).^2))) .* exp(-1j .* k .* sqrt(x.^2+(z-b).^2))) .* cos(theta);
quiver(x,z,abs(Hx),abs(Hz))
I'm kind of desperate right now

Akzeptierte Antwort

David Hill
David Hill am 31 Mär. 2020
Several problems I have noticed:
a = 0.4;
f = 10^6;
w = 2*pi*f;%scalar no need for .
I0 = 1;
b = (0.4/2);
mu0 = 4 * pi * 1e-7;
e0 = 8.85e-12;
k = w*sqrt(mu0*e0);%scalar no need for .
theta = linspace(-pi/2,pi/2,100);%this must be a consistent size based on your equation, use linspace
phi = linspace(0:2*pi,100);%consistent size
xx = linspace(-1,1,100);%not sure what you are doing here but size is only 1
zz = xx;
[x,z] = meshgrid(xx,zz);%this is now a 100x100 matrix
for k=1:100%you have to keep the size consistent
Hx(k,:) = (((1j .* k .* a.^2 .* I0 .* cos(theta)) ./ (x.^2+(z(k,:)-b(k,:)).^2)) .* (1 + 1 ./ (1j .* k .* sqrt(x.^2+(z(k,:)-b(k,:)).^2))) .* exp(-1j .* k .* sqrt(x.^2+(z(k,:)-b(k,:)).^2))) .* sin(theta).*cos(phi);
Hz(k,:) = (((1j .* k .* a.^2 .* I0 .* cos(theta)) ./ (x.^2+(z(k,:)-b(k,:)).^2)) .* (1 + 1 ./ (1j .* k .* sqrt(x.^2+(z(k,:)-b(k,:)).^2))) .* exp(-1j .* k .* sqrt(x.^2+(z(k,:)-b(k,:)).^2))) .* cos(theta);
end
quiver(x,z,abs(Hx),abs(Hz))
I'm not sure if everyting is now correct, but you get the idea.

Weitere Antworten (0)

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