Matlab error "Matrix dimensions must agree"

2 Ansichten (letzte 30 Tage)
Andrés Olvera
Andrés Olvera am 28 Mai 2020
Beantwortet: Clemens Dierks am 5 Nov. 2020
I'm trying to get the integral() of a two functions but I keep getting this error and I don't know why
This is the code for the functions:
R = 0.5; %m
I = 2; %A
u0 = 4*pi*10^-7;
BiotSavart = (u0*R*I)/(4*pi);
max = 50;
px = -R:R/max:R;
py = -R:R/max:R;
pz = 0:R/max:2*R;
fy = @(t) sin(t)./((px-R*cos(t)).^2+(py-R*sin(t)).^2+pz.^2).^(3/2);
fz = @(t) (R-py.*sin(t)-px.*cos(t))./((px-R*cos(t)).^2+(py-R*sin(t)).^2+pz.^2).^(3/2);
By = pz * BiotSavart * integral(fy,0,2*pi);
Bz = BiotSavart * integral(fz,0,2*pi);
I get the error in this line:
fy = @(t) sin(t)./((px-R*cos(t)).^2+(py-R*sin(t)).^2+pz.^2).^(3/2);

Akzeptierte Antwort

Star Strider
Star Strider am 28 Mai 2020
Integrating an array, set 'ArrayValued' to 'true':
By = pz .* BiotSavart .* integral(fy,0,2*pi, 'ArrayValued',1);
Bz = BiotSavart * integral(fz,0,2*pi, 'ArrayValued',1);
That runs without error.
  2 Kommentare
Andrés Olvera
Andrés Olvera am 28 Mai 2020
That worked, thanks! :D
Star Strider
Star Strider am 28 Mai 2020
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Clemens Dierks
Clemens Dierks am 5 Nov. 2020
Hello everybody,
I have the same error in a different situation.
I wrote the function to save two variables (matrices) in a xlsx file within two sheets.
Because xlswrite seems to be deprecated I used 'writematrix'.
A is 9000x2 matrix.
B is 1x1 vector.
The error occurs in when calling the method "writematrix".
Thank you in advance! :)
This is my code:
function [] = save_data(filename, A, B)
save(filename,'solution','DBC10');
writematrix(A, strcat(filename+'.xlsx'), 'sheet', 1)
writecell(B, strcat(filename+'.xlsx'),'sheet', 2)
end

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by