How to fix this code
Mostra commenti meno recenti
function [Centriod, M_Inertia,cpmo]=Rect(x,y)
if ~isequal(size(x),size(y))
error('X and Y must be the same size');
end
xm=mean(x);
ym=mean(y);
x=x-xm;
y=y-ym;
xp=x([2:end 1]);
yp=y([2:end 1]);
a=x.*yp-xp.*y;
A=sum(a)/2;
xc=sum((x+xp).*a)/6/A;
yc=sum((y+yp).*a)/6/A;
Ixx=sum((y.*y+y.*yp+yp.*yp).*a)/12;
Iyy=sum((x.*x+x.*xp+xp.*xp).*a)/12;
Ixy=sum((x.*yp+2*x.*y+2*xp.*yp+xp.*y).*a)/24;
dx=xp-x;
dy=yp-y;
P=sum(sqrt(dx.*dx+dy.*dy));
if A<0
A=-A;
Ixx=-Ixx;
Iyy=-Iyy;
Ixy=-Ixy;
end
Iuu=Ixx-A*yc*yc;
Ivv=Iyy-A*xc*xc;
Iuv=Ixy-A*xc*yc;
J=Iuu+Ivv;
x_cen=xc+xm;
y_cen=yc+ym;
Ixx=Iuu+A*y_cen*y_cen;
Iyy=Ivv+A*x_cen*x_cen;
Ixy=Iuv+A*x_cen*y_cen;
I=[Iuu -Ivv;
-Iuv Ivv];
[eig_vec, eig_val]=eig(I);
I1=eig_val(1,1);
I2=eig_val(2,2);
ang1=atan2(eig_vec(2,1),eig_vec(1,1));
ang2=atan2(eig_vec(2,2),eig_vec(1,2));
Centriod=[A x_cen y_cen P];
M_Inertia=[Ixx Iyy Ixy];
cpmo=[I1 ang1 I2 ang2 J];
end
Error using eig
Input to EIG must not contain NaN or Inf.
Error in Rect (line 46)
[eig_vec, eig_val]=eig(I);
Risposte (1)
In these lines;
xc=sum((x+xp).*a)/6/A
yc=sum((y+yp).*a)/6/A
when A is 0, these values become NaN since division by 0 is not defined. Since they are NaN, any other variable that uses them become NaN as well. eig() function requires numeric values since there cannot be eigenvalues of NaN.
If in your application A can be 0, these equations do not seem to be right. Otherwise, if A can be 0 and the equations are right, you can use an if statement to force xc and yc to be some number when A is equal to 0.
4 Commenti
karimat bello
il 20 Lug 2018
Aquatris
il 20 Lug 2018
The issue is A takes the value 0. If A is not supposed to be 0 at all, then the inputs you supply to the function should be carefully chosen to not let A equal to 0 or the equation that determines A is just wrong. I cannot help you further unless I go into details of what this function does, where the equations come from, what the input should be etc. which would take a lot of time since I am not familiar with what it is.
I recommend you check the equation if they are implemented correctly and then check what the inputs to the function should be and make sure you do not supply inputs that make the A equal to 0.
karimat bello
il 20 Lug 2018
Aquatris
il 20 Lug 2018
Correct but the underlying reason is the variable A. Since A is NaN, the other variables that are calculated using the A variable becomes NaN as well. Then you feed a matrix with NaN elements to eig() function, which is where you see the error since eig() function cannot work with NaN.
To test this, you can replace the equation that is "A = sum(a)/2" withs "A = 5" or any other numeric value, and you will see your code runs fine.
Categorie
Scopri di più su Creating and Concatenating Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!