Error using / Matrix dimensions must agree.

조회 수: 3 (최근 30일)
Emanuele Balduzzi
Emanuele Balduzzi 2016년 12월 15일
편집: dpb 2016년 12월 15일
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
Emanuele Balduzzi
Emanuele Balduzzi 2016년 12월 15일
They are just number, they aren't matrix
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