I am trying to solve a function in Matlab: digamma(x) =C

1k views Asked by At

For example, C=0,

solve('psi(x)=0')

ans =
-226.83295306016122662496413158295

psi(ans)

???Error using ==> psi
Input must be single or double.

I cannot get the right answer

1

There are 1 answers

4
Rody Oldenhuis On

Interesting...this seems like a bug in solve to me...Whatever value I try to solve for, I always get a weird value of around -227. Even when I try to trick MATLAB by giving an approximation of the digamma, I get the same result or worse:

>> solve('(gamma(x+0.01)-gamma(x))/0.01/gamma(x)=0')
ans = 
    matrix([[-226.83790783643886637282996154237]])

>> solve('(gammaln(x+0.01)-gammaln(x-0.01))/0.02 = 0')
??? Error using ==> mupadmex
Error in MuPAD command: cannot differentiate equation [numeric::fsolve]

The following numerical approach works:

%// value of the digamma to solve for
Y = -10; 

%// Solve using numerical scheme 
X = fsolve(@(x)psi(max(0,x)) - Y, exp(Y))

%// Check solution: psi(X) ≈ Y
psi(X)