2

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
Bahareh LV
  • 53
  • 2
  • 8
  • Where exactly do you get the error? Furthermore you may want to run the code after using `dbstop if error` to isolate/solve the problem. – Dennis Jaheruddin Jan 02 '13 at 10:59
  • after I used {roots(f)} instead of {solve(f)} and run the program in work space with {betaDistribution_2(1,2)} I got this error ??? Undefined function or method 'isfinite' for input arguments of type 'sym'. – Bahareh LV Jan 02 '13 at 12:04

1 Answers1

0

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
slitvinov
  • 5,693
  • 20
  • 31
  • No . the two results are both real . – Bahareh LV Jan 02 '13 at 11:28
  • It is possible that your system has several solutions and only one of them has a physical meaning. What is the range for `a` and `b`? – slitvinov Jan 02 '13 at 11:37
  • when I tried a=1 , b=2 I got 2 results . when I tried a=3 , b=4 I got 4 results !!!!!!!!! – Bahareh LV Jan 02 '13 at 11:39
  • the range of a and b could be different and it depends on user's opinion . – Bahareh LV Jan 02 '13 at 11:41
  • I see you redefine `i`. It messes up imagery numbers. Better change it and do `clean`. – slitvinov Jan 02 '13 at 11:45
  • Even I chaned the code like this `function betaDistribution_2(a,b)` `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));` `% 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);` `fxi=0.001;` `f=p-fxi;` `xi=solve(f);` `result=eval(xi);` `disp(result)' but the result was : `'1.4370 ` ` 0.0379 ` ` -0.0189 - 0.0310i` `1.0814 - 0.4712i` ` 1.0814 + 0.4712i` ` -0.0189 + 0.0310i ` – Bahareh LV Jan 02 '13 at 12:17
  • I don't know how to thank you . my problem finally solved . I was trying so hard but It didn't have any effect . thanks again . – Bahareh LV Jan 02 '13 at 12:43