root() applies only to symbolic expressions, so your p1 would have to be symbolic for root(p1,alpha) to work, and alpha would have to evaluate to a symbolic variable name.
You define p1 in terms of Q and gradient(expression) . gradient() exists for symbolic expressions, but it would typically be called in symbolic form as gradient(expression, variable_list) . If your expression happens to be symbolic, then gradient(expression) would be equivalent to gradient(expression, symvar(expression)) . Not impossible, but the call you are using hints that your expression is more likely to be numeric than symbolic.
In the part you posted, we cannot tell what data type Q is likely to be.
The second parameter to root(p1, alpha) would have to evaluate to a symbolic variable list. But look two lines further on: you have while sum(alpha) >= 1 . If alpha is a variable list, then sum(alpha) does exist and would be a symbolic expression involving the variables, but since the variables would be unresolved, it would not be possible to compare the result to >= 1 for the purposes of a while statement. That hints that your alpha is probably not a list of variable names.
Perhaps what you wanted was
r1 = roots([p1, alpha])
In the case where p1 is a scalar, then that would be trying to solve p1*x + alpha = 0, and the solution to that for non-zero p1 is always going to be x = -alpha/p1 in which case there is no point calling roots()
In the case where p1 is not a scalar, roots([p1, alpha]) would only be valid if p1 and alpha were both row vectors (including the case where alpha is a scalar -- which is unlikely considering the sum(alpha) you have later.). But if so then you need to watch out for complex roots...
Is it likely that p1 is a numeric scalar? Not really -- not considering you built it with a gradient() call, since you would be unlikely to call gradient() to return a scalar.
The point we have reached is this:
You are either using your own root() function that has nothing to do with MATLAB's root() or roots() -- or else you likely have significant bugs in your logic.

