In A2_trail (line 16) Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN
조회 수: 1 (최근 30일)
이전 댓글 표시
I'm Trying to solve matrix multiplications involing 500 by 4 size...but I'm getting that warning and final ans as : a= NaN + iNaN
NaN + iNaN
NaN + iNaN
NaN + iNaN
Could someone please tell me where its going wrong and why..!
load HW2_Prob2_data
z=log(z_train); %z_train is 500 by 1 matrix,similarly x_train and y_train
y=log(y_train); % train data is already available
x=x_train;
a=[1; 1; 1; 1];
r=rand(100,1);
for k=1:50
J=rand(500,4);
for i=1:500
J(i,4)=double(1/a(1));
J(i,3)=double(a(2)*(log(x(i)+a(3))));
J(i,2)=double(a(2)/(log(x(i)+a(3))));
J(i,1)=double(y(i));
r(i)=double(z(i)-(log(a(1))+a(2)*log(x(i)+a(3))+a(4)*y(i)));
end
a_new=double(inv(J'*J)*J'*r);
if norm(a_new)<0.0001
break
end
a=a+a_new;
end
disp(a)
댓글 수: 1
David Goodmanson
2019년 9월 3일
편집: David Goodmanson
2019년 9월 4일
Hello ND,
This code works if x,y and z are random positive numbers, so something is going on with your data. The most likely possibility is that x contains at least one entry xi that equals 0, so that log(xi) = inf.
Even if the data were OK, it is bad practice to solve the equation
J*a_new = r
by the old fashioned method
a_new = inv(J'*J)*J'*r .
(The 'double' isn't required since J is already a double).
Instead, take advantage of Matlab's capabilities and use
a_new = J\r .
p.s. it woud be better and faster if the entire r vector were preallocated with r = rand(500,1). That way Matlab doesn't have to resize the r vector for every value of i between 101 and 500.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear Algebra에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!