File Exchange Pick of the Week
August 26th, 2008
Puzzler: Working with polynomials
This puzzler is very straightforward, so I hope to hear from some of the newer MATLAB users. I think the solution is very linear, most people would come up with the same solution. As a hint, be sure to look at our help for POLYDER and ROOTS.
The challenge is this: Given the coefficients of two second order polynomials, find the local extrema of the first one. Knowing the X where this point happens, put a marker on both curves at that X value. Plot both curves for three units to the left and right of the inflection point of the first curve. [Edited Aug 27th in response to J. Paul R’s observation that I misused terminology, switching inflection point for local extrema. MATLAB t-shirt for the catch. Thank you!]

Here is the code to create the coefficients:
coef1 = rand(1,3)-0.5;
coef2 = rand(1,3)-0.5;
<pre> <code>
all the code so someone can just copy and paste it from the comments.
</code> </pre>
08:36 UTC |
Posted in MATLAB Basics, Puzzler |
Permalink |
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.
Leave a Reply
|
Here’s my approach using matrices for the calculation
The real puzzler is: “what is this puzzler?” Your text ask for an “inflection point” while your example shows an extrema. Additional, 3 coefficients specify a 2nd degree polynomial which has no inflection points. So are you looking for extrema or inflection points? Or perhaps the extrema of the first derivative, which is, generally, an inflection point of the original polynomial?
BTW, here’s my code for inflection points. I don’t think there are any edge cases to worry about with 3rd order polynomials. (Like x=0 for x^4)
OK, here is my totally AR* comment: extrema is plural; for a quadratic, there can be only one local extremum (the singular version of extrema).
Yours in good grammar, Mike Briggs (-:
* AR: anal-retentive
A 2nr order polynomial is just the old parabola from high school. It extremum is called the vertex and has analytically known coordinates. Its x coordinate is just
My solution is the following:
perhaps some error checking is in order. After all, it is possible that our randomly generated quadratic happens to be linear ( coef1(1) == 0 ). In this case, the first three codes above codes throw a cryptic error “Inner matrix dimensions must agree.” from which it is not obvious what has gone wrong (roots(polyder) returns an empty matrix). The last code runs without error, but produces nothing as extremum has value +/-Inf
Since this error is fairly unlikely, simply running the code a bunch of times with random input probably won’t notice it. What caught my attention was the last code, which happily sticks a random number from an interval containing zero at the bottom of a fraction.
A quick check to make we have a quadratic before diving in to extreme point finding may be in order:
Regards,
David
While we’re at it…
Checking for a number to be zero is tricky in itself.
We’re better off using
revised code:
by the way, infinity is the correct answer in the limit of the 2nd degree coefficient going to zero, as long as the first degree coefficient is not zero: in that case infinity is not the solution either.
coef1 = rand(1,3)-0.5;
coef2 = rand(1,3)-0.5;
lex1=roots(polyder(coef1))
lex2=roots(polyder(coef2))
hold all
plot(lex1-3:0.1:lex1+3,polyval(coef1,lex1-3:0.1:lex1+3),’-');
plot(lex1-3:0.1:lex1+3,polyval(coef2,lex1-3:0.1:lex1+3),’-');
plot(lex1,polyval(coef1,lex1),’o');
plot(lex1,polyval(coef2,lex1),’d');
xlim([lex1-3 lex1+3])
This is my solution:
Wow!
I have been out for a week, so I have not been able to respond to all the great comments here. I find it interesting how the challenge evolved.
First many people solve the problem and then people start to see edge cases where the square term is zero. Then people find that the test for this edge case can be found more effectively in a different way.
This kind of hardening of the code is exactly why we have frequent “code reviews” at The MathWorks to make sure many eyes see code before it enters the testing stage and finally the product itself.
Thanks everyone!
Doug