Getting Complex number from a function

5 ビュー (過去 30 日間)
Eyan Hudson
Eyan Hudson 2022 年 11 月 25 日
コメント済み: Sulaymon Eshkabilov 2022 年 11 月 25 日
function fxy = fxy(x,y)
square = sqrt(x*x + y*y);
t1 = (sin(20*square))/(20*square);
t2 = (1/5)*cos(10*square);
t3 = (y/2) - .3;
fxy = t1 + t2 + t3;
return
end
I am using the values below to run through the function above as a part of a machine learning project, but I keep recieving the error "Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 2.368008e-33" when I click run. The numbers I recieve for fxy are complex numbers, but if you plug the values in individually, there is no way for them to come out as complex. I am not sure how to get the correct results. (The function code is saved as fxy.m and run in the same folder).
dim = 100;
%create 100x100 array
a = linspace(-1, 1, dim);
b = linspace(-1, 1, dim);
[ax, by] = meshgrid(a,b);
%plug values into f(x,y)
fxy = fxy(ax, by);

採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022 年 11 月 25 日
Here is the corrected code:
dim = 100;
%create 100x100 array
a = linspace(-1, 1, dim);
b = linspace(-1, 1, dim);
[ax, by] = meshgrid(a,b);
%plug values into f(x,y)
fxy = FXY_FUN(ax, by);
meshc(fxy)
function fxy = FXY_FUN(x,y) % Be careful with the function name and variable name
square = sqrt(x.^2 + y.^2); % Elementwise operation is necessary
t1 = (sin(20*square))./(20*square); % Another elementwise operation
t2 = (1/5)*cos(10*square);
t3 = (y/2) - .3;
fxy = t1 + t2 + t3;
return
end
  2 件のコメント
Eyan Hudson
Eyan Hudson 2022 年 11 月 25 日
Thank you! I completely forgot about the elementwise operator, that fixed it!
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022 年 11 月 25 日
Most welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by