Why does the SQRT function or ^0.5 function return negative values in MATLAB?

11 visualizaciones (últimos 30 días)
I am taking square roots and MATLAB is returning, negative values, why is this?
Let me show you the maths and the script that is doing this.
I was implementing the following compositional function (which in the end takes a sqrt of two POSITIVE or zero numbers):
in matlab and generating a lot of values using that function. It happens that when I generate a lot of values of that function, some are (strangely) negative. Why is that? It should never happen since its suppose to be the sqrt of two functions that are always positive because of the sum of two squares is always positive or zero.
The script is:
restoredefaultpath;clear;clc;clear;clc;
%%target function
f_target = struct('h', cell(2,2), 'f', cell(2,2));
h11 = @(A) (1/20)*(1*A(1) + 2*A(2))^4; % ( x1 + x2)
h12 = @(A) (1/10)*(3*A(1) + 4*A(2))^3;
h21 = @(A) (1/100)*(5*A(1) + 6*A(2))^2;
f_target(1,1).h = h11;
f_target(1,2).h = h12;
f_target(2,1).h = h21;
h13 = @(A) (1/20)*(1*A(1) + 2*A(2))^4; % ( x1 + x2)
h14 = @(A) (1/10)*(3*A(1) + 4*A(2))^3;
h22 = @(A) (1/100)*(5*A(1) + 6*A(2))^2;
f_target(1,3).h = h13;
f_target(1,4).h = h14;
f_target(2,2).h = h22;
%h31 = @(A) (1/1)*(A(1) + (1/100)*A(2) + 1)^0.5;
h31 = @(A) (1/500)*sqrt(A(1) + (1/100)*A(2) + 1);
f_target(3,1).h = h31;
% f_target(1,1).f_4D = @f_4D;
% f_target(1,1).f_8D = @f_8D;
% f_target(1,1).f_8D_hard_code = @f_8D_hard_code;
f_target(1,1).f = @f_8D_hard_code;
%%make data set
sigpower = 'measured';
powertype = 'linear';
snr = 8;
low_x = -2;
high_x = 2;
nb_samples = 100000; %100,000
D = 8;
[X,Y] = generate_data_from_function( f_target, snr, low_x,high_x, nb_samples, sigpower, powertype, D);
D = size(X,2);
D_out = size(Y,2);
sum(Y < 0)
save('f8D_all_data_set')
beep;
and the coded function is:
function [ f_val ] = f_8D_hard_code( x, f_target )
%compute left
h(1,1).val = f_target(1,1).h(x(1:2));
h(1,2).val = f_target(1,2).h(x(3:4));
h(2,1).val = f_target(2,1).h( [h(1,1).val, h(1,2).val] );
%compute right
h(1,3).val = f_target(1,3).h(x(5:6));
h(1,4).val = f_target(1,4).h(x(7:8));
h(2,2).val = f_target(2,2).h( [h(1,3).val, h(1,4).val] );
%compute all
h(3,1).val = f_target(3,1).h( [h(2,1).val, h(2,2).val] );
f_val = h(3,1).val;
end
and the generation data function is:
function [ X, Y ] = generate_data_from_function( f_target, snr, low_x,high_x, nb_samples, sigpower, powertype, D)
% sigpower = usually 'measured', powertype = usually 'linear'
X = low_x + (high_x - low_x) * rand(nb_samples,D);
Y = zeros(nb_samples,1); % (N x 1)
for n = 1:nb_samples
xn = X(n,:);
fx = f_target(1,1).f( xn, f_target );
yn = awgn(fx,snr,sigpower, powertype);
Y(n,:) = yn;
end
end
  1 comentario
Image Analyst
Image Analyst el 22 de Mayo de 2016
Editada: Image Analyst el 22 de Mayo de 2016
Note: code above requires the Communications System Toolbox. (So I can't run it.)

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 22 de Mayo de 2016
?? What does this have to do with sqrt() ??
You are calling awgn . awgn is Gaussian, which is normally distributed, so it has indefinite tails in both directions. So for any given mean and sigma, there is a finite probability that it will generate a negative value.

Más respuestas (0)

Categorías

Más información sobre Communications Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by