Getting Complex number from a function

1 Ansicht (letzte 30 Tage)
Eyan Hudson
Eyan Hudson am 25 Nov. 2022
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);

Akzeptierte Antwort

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 25 Nov. 2022
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 Kommentare
Eyan Hudson
Eyan Hudson am 25 Nov. 2022
Thank you! I completely forgot about the elementwise operator, that fixed it!
Sulaymon Eshkabilov
Sulaymon Eshkabilov am 25 Nov. 2022
Most welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Linear Algebra finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by