Getting Complex number from a function

1 vue (au cours des 30 derniers jours)
Eyan Hudson
Eyan Hudson le 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);

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Linear Algebra dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by