Can you just throw it at solve, and hope solve can figure out what is your question? Probably not. A tool like solve is not that terribly intelligent. Sorry.
As is so often the case, the trick is to use mathematics, to solve a problem of mathematics. Here, you might want to look at something called Descartes' rule of signs.
Importantly, read this abstracted portion:
"The rule states that if the nonzero terms of a single-variable polynomial with real coefficients are ordered by descending variable exponent, then the number of positive roots of the polynomial is either equal to the number of sign changes between consecutive (nonzero) coefficients, or is less than it by an even number. A root of multiplicity k is counted as k roots. In particular, if the number of sign changes is zero or one, the number of positive roots equals the number of sign changes."
You can also use a varition of this rule to count the number of negative roots, but if we can count the number of positive roots accurately, then the number of negative roots is trivial.
Of course, this is just a quadratic polynomial, pretty simple, really. So there are surely many ways you might solve the problem. I'll employ Descartes' rule. The nice thing is, you don't need no steenkin' computer to do it either.
The coefficients of the polynomial are
{m-2, -3*m, m+2}
I might first consider special cases.
- When m = -2, note that one of the roots will be zero.
- Whem m = 2, the quadratic reduces to a linear polynomial, so there is only one root. (It is positive, if you care.) But since you want to have two roots, this entire problem becomes moot and irrelevant then.
For any value of m, how many sign changes are there? We can learn this by breaking the problem down into 4 intervals.
- The quadratic coefficient will be positive when m > 2, and negative when m < 2.
- The linear coefficient will have the opposite sign as m, so positive when m < 0, and negative when m > 0.
- The constant term will be positive when m > -2, and negative when m < -2.
Now we know where to look.
- -inf < m < -2 ==> (coefficients are - + -) 2 sign changes ==> ZERO or TWO positive roots
- -2 < m < 0 ==> (coefficients are - + +} 1 sign change ==> ONE positve root (ergo one negative root)
- 0 < m < 2 ==> (coefficients are - - +} 1 sign change ==> ONE positve root (ergo one negative root)
- 2 < m < inf ==> (coefficients are + - +} 2 sign changes ==> ZERO or TWO positve roots
And we can check the special case when m==0, and see the two roots have opposite signs.
P = @(m) [m-2,-3*m, m+2];
roots(P(0))
ans =
-1
1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Indeed they do. Therefore, we know that there are two roots of opposite signs when -2 < m < 2. And ONLY then.
Of course, nothing stops us from checking that conclusion. What is the old saying, to trust, but verify?
roots(P(-5))
ans =
1.9196
0.2233
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
roots(P(-1))
ans =
1.2638
-0.2638
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
roots(P(1))
ans =
-3.7913
0.7913
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
roots(P(5))
ans =
4.4791
0.5209
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
As predicted, at each of those choices, I was correct. I'm not using this as a proof of correctness of course, as I already knew that to be true. But it never hurts to validate your logic.
By the way, I'll not be at all surprised if the Descartes rule comes as a complete surprise to many people reading this site. This is the sort of thing that seems to have been lost along the way, like our skills at using a slide rule. ;-)
Ok, suppose you are one of those who really does want to use a steenkin' computer to solve this? Can you do something? Well, I would imagine so.
xsol = solve((m-2)*x^2-3*m*x+(m+2),x)
xsol =
First, are the roots EVER complex? This is easy enough to see. For that we need to look only at the part inside the square root, the discriminant. You should recognize that for any real value of m, 5*m^2 + 16 will ALWAYS be positive. Therefore we will always have exactly two real roots, except for the case when m==2. Notice that when m==2, xsol has a problem, due to the divide by zero. So we should strenuously avoid that case.
Can we get solve to give us a hint here?
solve(xsol(1) > 0,returnconditions = true)
Warning: Unable to find explicit solution. For options, see
help.
ans =
m: [0x1 sym]
parameters: [1x0 sym]
conditions: [0x1 sym]
Sadly, solve seems to be a bit recalcitrant. Hey solve, go back and read Descartes! Stupid compiuters.
One thing we might do here, is to plot the actual roots. We can learn a great deal from a plot, as long as we think about what we see.
fplot(xsol(1),[-3,3],'r')
fplot(xsol(2),[-3,3],'b')
legend('Root #1','Root #2')
So root #1 (in red) seems to be always positive in the region of interest. Root number 2 changes sign at m==-2. (Easy enough to check.) And root #2 undergoes a singularity at m==2, where it changes sign. Do you see it? There are roots of opposite signs ONLY when m lies in the open interval (-2,2). That Descartes guy was right after all! Who woulda known?
Ok. Surely we can do a little better yet. But how many different ways do I need to do your homework? Sigh.
What if we look at the ratio xsol(2)/xsol(1)? If both roots have the same signs, then the ratio will be positive. If they have different signs, then the ratio will be negative. Again, a plot may be sufficient.
fplot(xsol(2)/xsol(1),[-5,5])
Where is the blue line less than zero? You can convince yourself pretty easily that happens only when -2<m<2. Wow. that Descartes guy really knew what he was talking about! Lets try it again, using solve now.
xratio = xsol(2)/xsol(1)
xratio =
solve(xratio < 0,m,returnconditions = true)
Warning: Unable to find explicit solution. For options, see
help.
ans =
m: [0x1 sym]
parameters: [1x0 sym]
conditions: [0x1 sym]
Blast. Stupid computers. But we can still do more. First, what is the asymptotic behavior of that ratio at each end of the real line?
limit(xratio,m,-inf)
ans =
limit(xratio,m,inf)
ans =
You should see the limits are POSITIVE at both ends of the real line. Next, we could look at the behavior of the ratio for m>2, and m<-2. With a little effort, we could make avaluable inference. Again, I've done enough by now.
If the real gist of your question is how do you solve somewhat difficult problems using MATLAB, the answer is always to use mathematics. Use everything you know. Be creative. In the end, that is really the only answer.