Either I am misunderstanding the question, or it shouldn't. There is no root between 1 and 4.y = x^3 - 9x + 0.1, that script should give me
M = [-2 1;1 4]
The sign function won't be able to help you very much, I'm afraid, since it is possible to have a root without a change of sign if the graph just touches the x-axis.
Are you sure you are not allowed to take advantage of the function? Cleaning up the constants produces . You probably know something about the roots of the cos and exponential functions that could allow you to calculate the roots from the values of p and q. I don't think that what the script is supposed to do is possible if it must work for every function.