# Getting Complex number from a function

1 Ansicht (letzte 30 Tage)
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);
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

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 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Sulaymon Eshkabilov am 25 Nov. 2022
Most welcome!

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Loops and Conditional Statements 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