strange behavior of function leading to complex numbers
Show older comments
i have defined a function named f_gamma which is as follows:
function F = f_gamma(x,xdata)
if xdata <= x(1)
F = 0;
return;
end
F = x(2).*((xdata-x(1)).^x(3)).*exp(-(xdata-x(1))/x(4));
end
in my understanding, for every xdata<=x(1) the function should return zero. but it doesnt. let me give you an example:
lets set x (the constants) = [8, 5, 3, 0.5]; therefore, for all xdata <= 8 the function should return F = 0.
if i call the function now like this
f_gamma(x,2)
ans = 0
everything is correct.
however, if i call it like this, e.g. for plotting
f_gamma(x,1:50)
ans(2) = 229898430,869035 - 250127664,068870i
where does this complex number come from? am i missing something? your help is much appreciated. :)
Accepted Answer
More Answers (1)
Adam
on 20 Aug 2014
If you call
xData <= x(1)
when xData is a vector you will get a vector of results of the same length.
You need to use
all( xData <= x(1) )
if you want a single value representing that all the data in your array meet the condition.
any( xData <= x(1) )
would be the equivalent if you just want to check that at least one value meets the condition.
Categories
Find more on Time Series Events in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!