Clear Filters
Clear Filters

Solve more complex problems in matlab

7 views (last 30 days)
Online
Online on 28 Aug 2024
Answered: Paul on 31 Aug 2024

Hello, I have the following problem: Determine m so that the equation (m-2)x²-3mx+(m+2)=0 has one positive root and one negative root.

I have written the following code and I am getting Empty sym: 0-by-1

syms x m
assume(m~=0);
eq = (m-2)*x^2-3*m*x+(m+2) == 0;
eq_roots = solve(eq, x);
x1 = eq_roots(1);
x2 = eq_roots(2);
cond1 = x1 < 0;
cond2 = 0 < x2;
s=solve([cond1, cond2], m, 'Real', true,'ReturnConditions', true)
s.conditions

Is there any way to solve problems like this in matlab?

I am a beginner in matlab and I am using problems that I know how to solve mathematically to train the use of matlab

  1 Comment
David Goodmanson
David Goodmanson on 30 Aug 2024
Hi OF,
I have modified my answer below to better address the question you are asking.

Sign in to comment.

Answers (3)

John D'Errico
John D'Errico on 28 Aug 2024
Edited: John D'Errico on 28 Aug 2024
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.
  1. When m = -2, note that one of the roots will be zero.
  2. 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.
  1. The quadratic coefficient will be positive when m > 2, and negative when m < 2.
  2. The linear coefficient will have the opposite sign as m, so positive when m < 0, and negative when m > 0.
  3. The constant term will be positive when m > -2, and negative when m < -2.
Now we know where to look.
  1. -inf < m < -2 ==> (coefficients are - + -) 2 sign changes ==> ZERO or TWO positive roots
  2. -2 < m < 0 ==> (coefficients are - + +} 1 sign change ==> ONE positve root (ergo one negative root)
  3. 0 < m < 2 ==> (coefficients are - - +} 1 sign change ==> ONE positve root (ergo one negative root)
  4. 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 = 2x1
-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 = 2x1
1.9196 0.2233
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
roots(P(-1))
ans = 2x1
1.2638 -0.2638
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
roots(P(1))
ans = 2x1
-3.7913 0.7913
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
roots(P(5))
ans = 2x1
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.
syms m x
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 = struct with fields:
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')
hold on
fplot(xsol(2),[-3,3],'b')
hold off
xlabel m
legend('Root #1','Root #2')
grid on
xline([-2,0,2],'g')
ylim([-10,10])
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])
grid on
xlabel m
ylim([-10,10])
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 = struct with fields:
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.

David Goodmanson
David Goodmanson on 29 Aug 2024
Edited: David Goodmanson on 30 Aug 2024
HI OF,
y(x) = (m-2)x^2 -3mx + (m+2)
The domain of m where the two roots have opposite sign is a numerical question and I think people too often automaically go to symbolic variables when they are not actually needed. Here is one way to do some exploration to find the answer. The yesno array = 1 when roots are opposite sign, 0 otherwise and from the plot you can see that the roots have opposite sign for -2 < m < 2.
m = -8:.01:8; % create a row vector of likely m values
n = size(m,2); % number of elements of m
yesno = zeros(1,n); % array for the 'opposite sign' result, pass = 1
for k = 1:n
mk = m(k);
p = [(mk-2) -3*mk (mk+2)]; % poly coeffcients, largest power of x first
r1r2 = (roots(p));
if min(r1r2) < 0 & max(r1r2) > 0
yesno(k) = 1;
end
end
figure(1)
plot(m,yesno)
grid on
ylim([-2 2])
******************************
There are several ways to solve this problem algebraically, One is to use the standard result
if ax^2 + bx + c = 0
then the product of the roots is
r1r2 = c/a.
The product of the roots in this case is
r1r2 = (m+2)/(m-2).
For one positive and one negative root their product must be negative so the requirement is
(m+2)/(m-2) is negative.
For very large positive m or very large negative m this quotient is positive and it is easy to see that for
-2 < m < 2
the quotient is negative, so that's the answer. Except that argument only works when both roots are real, as opposed to being complex. When the parabola
y = ax^2 +bx + c
stays on one side or the other of y = 0 for all x, then there are complex roots. When the parabola crosses y = 0 at least once then it crosses twice and there are two real roots. If you don't want to mess with the discriminant you can use
y(x) = (m-2)x^2 -3mx + (m+2)
y(1) = (m-2) -3m + (m+2) = -m
y(-1) = (m-2) +3mx + (m+2) = 5m
so whatever the value of m we have demonstrated a positive real and a negative real value of y. This means that the parabola always crosses y = 0 and the roots are both real. The argument doesn't quite work for m = 0 but then the equation is
-2x^2 + 2 = 0
which has roots +-1. Also there are some divergence problems when m = 2 exactly.

Paul
Paul on 31 Aug 2024
Modifying the approach from the question, using solve seems to work fine.
syms x m real
eq = (m-2)*x^2-3*m*x+(m+2) == 0;
eq_roots = solve(eq, x);
x1 = eq_roots(1)
x1 = 
x2 = eq_roots(2)
x2 = 
cond = x1*x2 < 0 % one positive and one negative
cond = 
s = solve(cond, m, 'Real',true,'ReturnConditions',true)
s = struct with fields:
m: x parameters: x conditions: -2 < x & x < 2

Tags

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!