Error using / Matrix dimensions must agree.

2 views (last 30 days)
Emanuele Balduzzi
Emanuele Balduzzi on 15 Dec 2016
Edited: dpb on 15 Dec 2016
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?

Answers (2)

KSSV
KSSV on 15 Dec 2016
Check sizes of mass and v2tot, they might not be compatible for matrix multiplication.
  2 Comments
Emanuele Balduzzi
Emanuele Balduzzi on 15 Dec 2016
They are just number, they aren't matrix
dpb
dpb on 15 Dec 2016
Excepting it's the '/' in the error message, not the '*'...

Sign in to comment.


dpb
dpb on 15 Dec 2016
Edited: dpb on 15 Dec 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.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!