Error using / Matrix dimensions must agree.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi everyone! I'm new, and I've got this problem:
_Error using /
Matrix dimensions must agree.
Error in viniz (line 58)
Teff=11603*mass*v2tot/(3*nat);_
I write my simple code:
function [vx0 vy0 vz0] = viniz
global nat tempiniz mass
vx0=zeros(nat,1);
vy0=zeros(nat,1);
vz0=zeros(nat,1);
v0=zeros(nat,1);
cost=sqrt(3*tempiniz/(11603*mass));
%Creo velocità
for i=1:nat
vx0(i)=cost*(2*rand-1);
vy0(i)=cost*(2*rand-1);
vz0(i)=cost*(2*rand-1);
end
%Calcolo velocità media
vxmed=0;
vymed=0;
vzmed=0;
for i=1:nat
vxmed=vxmed+(vx0(i)/nat);
vymed=vymed+(vy0(i)/nat);
vzmed=vzmed+(vz0(i)/nat);
end
%Riscalo velocità
for i=1:nat
vx0(i)=vx0(i)-(vxmed);
vy0(i)=vy0(i)-(vymed);
vz0(i)=vz0(i)-(vzmed);
v0(i)=((vx0(i)*vx0(i))+(vy0(i)*vy0(i))+(vz0(i)*vz0(i)))^(1/2);
end
% Calcolo T efficace
v2tot=0;
for i=1:nat
v2tot=v2tot+(v0(i)*v0(i));
end
Teff=11603*mass*v2tot/(3*nat);
%Riscalo con Teff sotto radice così velocità delle mia particella
%corrisponde a tempiniz
for i=1:nat
vx0(i)=vx0(i)*((tempiniz/Teff)^(1/2));
vy0(i)=vy0(i)*((tempiniz/Teff)^(1/2));
vz0(i)=vz0(i)*((tempiniz/Teff)^(1/2));
end
end
Can anyone help me?
0 Kommentare
Antworten (2)
KSSV
am 15 Dez. 2016
Check sizes of mass and v2tot, they might not be compatible for matrix multiplication.
2 Kommentare
dpb
am 15 Dez. 2016
Bearbeitet: dpb
am 15 Dez. 2016
global nat tempiniz mass
is very bad inside the function; you can screw up what they are too easily. Make them arguments to the function.
IF nat is a scalar constant as it appears your code assumes, then the line above will not error on the divide operation; anything can be operated on by a constant. Hence, something broke other than what you've shown us; we can't see the data in the workspace at the time of the evocation.
Also, the function could be written without looping it appears...
vx0=zeros(nat,1);
...
for i=1:nat
vx0(i)=cost*(2*rand-1);
...
is simply written in Matlab as
vx0=cost*(2*rand(nat,1)-1);
and similarly for the other terms and loops.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!