Error using / Matrix dimensions must agree.

3 次查看(过去 30 天)
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?

回答(2 个)

KSSV
KSSV 2016-12-15
Check sizes of mass and v2tot, they might not be compatible for matrix multiplication.
  2 个评论
dpb
dpb 2016-12-15
Excepting it's the '/' in the error message, not the '*'...

请先登录,再进行评论。


dpb
dpb 2016-12-15
编辑:dpb 2016-12-15
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.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by