First of all, check here and herehere. This is a common pitfall, and questions related to this are posted literally weekly, so I am going to let you review those articles.
In short, if you try evaluating medianLEfromQx[x qx1]
with x
having no value, you'll see that it returns a number. This expression evaluates inside FindRoot
even before FindRoot
gets a chance to substitute a value for x
. So you would have to make medianLEfromQx
not evaluate except for truly numerical vector arguments.
You can do this by changing its definition to look like:
Clear[medianLEfromQx]
medianLEfromQx[qx_ /; (VectorQ[qx, NumericQ])] := ...
Now medianLEfromQx[x qx1]
won't evaluate unless x
has a numerical value.
Next, Solve
and NSolve
won't work on numerical blackboxes, only FindRoot
will. Solve
only works with symbolic equations with exact coefficients. NSolve
is designed for solving polynomial equations (or equations that can be reduced to a polynomial equation) numerically, thus it also needs to see the structure of an equation and won't work with a numerical black box.
So the only candidate here is FindRoot
.
However FindRoot
isn't very appropriate here either. The methods it can use all assume that the function they're working with is a "nice and smooth one". Your function always returns integers, so it has a "step structure". The default FindRoot
method would try to approximate the derivative of the function and would of course fail: the derivative is zero everywhere.
You can use Brent's method, but this isn't ideal either: FindRoot[medianLEfromQx[x*qx1] == 8, {x, 0, 2}, Method -> "Brent"]
Instead I would just plot the function and visually check the range of x
which satisfies this equation.
Plot[medianLEfromQx[x qx1] - 8, {x, 0, 10}]