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 CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by