Re: Double-checking a Program
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: Double-checking a Program         

Group: comp.lang.fortran · Group Profile
Author: Steven Lord
Date: Sep 5, 2007 20:58

"widmar" sdynamix.com> wrote in message
news:46DF4957.F6D18C79@sdynamix.com...
> mecej4 wrote:
>>
>> I get with Watcom 1.5:
>> s:\>rpoly
>> 3,1,0,-2,-5
>> 2.095E+00 0.000E+00 -1.047E+00 1.136E+00 -1.047E+00-1.136E+00
>>
>> which is in agreement with what Matlab zeros() gives.
>> polynomial = [1 0 -2 -5];
>> theroots = roots(polynomial)
theroots =
2.09455148154233
-1.04727574077116 + 1.13593988908893i
-1.04727574077116 - 1.13593988908893i
>> values = polyval(polynomial, theroots)
values =
1.95399252334028e-014
-1.77635683940025e-015 - 1.77635683940025e-015i
-1.77635683940025e-015 + 1.77635683940025e-015i

Looks pretty reasonable to me.
> Actually this doesn't mean much considering the origins of Matlab code -
> for all you know they made the same mistake (translating a mixture of
> sp/dp machine constants) as you did. It could be worse, awhile back an
> academic cited some theory in trying to explain Matlab's faulty roots...
> try his case,

The ROOTS function is an M-file, you can edit it and read it if you want, or
you can look at the Algorithm section of the reference page for the function
for a description of basically what it's doing:

http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/roots...

By the way, is the "academic" you're referring to perhaps Cleve Moler, who
is the original author of MATLAB and one of the authors of LINPACK and
EISPACK?

http://www.mathworks.com/company/aboutus/founders/clevemoler.html

The reason I ask is because ...
> p(x) = x^7 - 7*x^6 + 21*x^5 - 35*x^4 + 35*x^3 - 21*x^2 + 7*x - 1

Cleve used this example in one of his Cleve's Corner articles:

http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf
> to see if that theory still holds!? Note, that validation should start
> with solving a known case - only then can you assume you're not
> comparing the *same* algorithm renamed by different commercial brands.
> "cpoly" (predecessor to "rpoly") does just that - provides a number of
> nontrivial cases to verify if any particular *port* passes the grade.

You can also try substituting the roots generated by the program back into
the original expression and see if you get essentially 0 as a result. While
that's not guaranteed to always work (see the last column of Cleve's article
above, but think about a case with larger roots, higher order polynomials,
and more cancellation) it's still a pretty useful check.

--
Steve Lord
slord@mathworks.com
no comments
diggit! del.icio.us! reddit!