I have an equation in Matlab according to X parameter . I want to find the amount of X for the random amounts of F(x) . and I tried the code below . but It gives me two different results while my equation should have just one result .
even I tried the roots(f)
instead of solve(f)
but it gave me an error :
??? Undefined function or method 'isfinite' for input arguments of type 'sym'.
anybody can help me in this ? what should I do ? even if I have a wrong idea about solving this problem please tell me . Thank you
function betaDistribution_2(a,b)
syms x ; y=inline((x^(a-1))*((1-x)^(b-1))); beta=quad(y,0,1); g=(1/beta)*(x^(a-1))*((1-x)^(b-1)); % I have this equation and I want to find the amount of x for the random %amounts of p p=int(g,x,0,x); for i=0:50 fxi=rand(1); f=p-fxi; xi=solve(f); result=eval(xi); disp(result) end end
Try to filter your solutions.
a=1;
b=2;
% a threshold for imagery part
SMALL=1e-9;
syms x real;
y=inline((x^(a-1))*((1-x)^(b-1)));
beta=quad(y,0,1);
g=(1/beta)*(x^(a-1))*((1-x)^(b-1));
p=int(g,x,0,x);
% return true for physically meaningfull results
filter = @(xc) abs(imag(xc))<SMALL && real(xc)>0 && subs(g, x, xc) > 0 && subs(p, x, xc)>0;
for m=0:50
fxi=rand(1);
f=p-fxi;
xi=solve(f, x);
result=eval(xi);
idx = arrayfun (filter, result);
result = result(idx);
% make sure it is OK
assert(length(result)==1);
disp(result)
end