Real valued function for positive x,y returning complex values?

조회 수: 4 (최근 30일)
Rob
Rob 2013년 9월 6일
I am getting this error when evaluating my function ff: "Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.467910e-279." The function isn't complex, so I am unsure why it is returning a matrix of 0+0i values. How do I fix this error?
x=[0:0.1:5];
y=[0:0.1:5];
[xx,yy]=meshgrid(x,y);
gg=10*xx*0.5+0.1*xx-10-2*yy;
figure
surf(xx,yy,gg)
xlabel('s1')
ylabel('s2')
zlabel('g(x,y)');
hold on;
q=zeros(51,51);
mesh(xx,yy,q);
ff=10*xx^0.5-3*yy^1.2 +2*yy;

채택된 답변

Geert
Geert 2013년 9월 6일
편집: Geert 2013년 9월 6일
Rob,
I think the problem is that on the last line of your code, i.e.,
ff=10*xx^0.5-3*yy^1.2 +2*yy;
you're taking the matrix square root of xx and not of each matrix element individually. What you probably want to do is the following:
ff=10*xx.^0.5-3*yy.^1.2 +2*yy;
( Notice the point in xx.^0.5. This is an elementwise operation, it basically will take the square root of each individual element, rather than from the entire matrix).
I wrote a little example to explain the difference between elementwise and full matrix operations:
% generate random matrix
A = rand(2,2)
% calculate 2th power of A
A^2
% observe that the same result is produced by A*A
A*A
% observe that the result is different from A.^2
A.^2
% when taking the (1/2)th power (= square root), complex numbers arise
B = A^0.5
% observe that B*B = A
B*B
The warning you get, relates to the matrix xx, which is close to singular. Remember that a matrix is singular iff it has a zero eigenvalue. The fact that xx is close to singular can be observed by having a look at its eigen values:
lambda = eig(xx);
disp(sprintf('The last and smallest eigenvalue is %d', lambda(end)))
You can observe the last (and smallest) eigenvalue is 1.6421e-142. This last eigenvalue is so small, that it renders the square root calculation of xx inaccurate.
  댓글 수: 1
Rob
Rob 2013년 9월 6일
Thanks Geert! I forgot all about element-wise operations.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by