I am trying to solve a function in Matlab: digamma(x) - log(x/5) = 0
I tried sol = solve(psi(x)-log(x/5),x)
, but the digamma function psi
is not compatible with the solve
function.
Is there any way to solve this problem?
The sym/psi
function was added to the Symbolic Math toolbox in Matlab version R2011b so it's not a surprise that you can't get it to work in R2011a.
If you can't upgrade, here are two things to try. First, although you can't call psi
for symbolic inputs, it's possible that the symbolic engine in R2011a still knows about this function. I don't guarantee that it will work, but you can try this:
x = evalin(symengine,'float(solve(psi(x)-log(x/5),x))')
By the way, you may wish to consider setting assumptions
– the x
returned is different if you specify syms x real
as the equation has multiple roots. If the above works, then you can call a vectorized version of the symbolic psi
with:
psisym = @(x)evalin(symengine,['map(' char(sym(x)) ',psi)'])
Otherwise, you'll have to solve this equation numerically (solve
gives an answer in terms of a number anyways). This is not a bad option as long as you're assuming that x
is real and positive. Using fzero
and the numeric version of psi
:
f = @(x)psi(max(x,realmin))-log(max(x,realmin)/5)
x = fzero(f,1)
returns
x =
0.407621610906987
I used max(x,realmin)
to ensure that x
never gets too small (as long as the the initial guess for fzero
is large enough, it may not be needed). An x
close to zero is psi(0)-log(0)
, which is NaN
and would result in an error from fzero
.