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

4 views (last 30 days)
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;

Accepted Answer

Geert
Geert on 6 Sep 2013
Edited: Geert on 6 Sep 2013
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.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!