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 Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by