Since the MATLAB quantity pi that approximates $\pi$ is rational, we can set tol to zero without fear of an infinite loop. I have another function, ratp, that is rat with round replaced by floor so that it produces proper, but longer, continued fractions with positive terms. If we're careful with the curly braces, Latex and MathJax can typeset that last continued fraction and produce a graphic for this post. Somewhat higher accuracy produces a negative term. The default tolerance produces a less familiar, but elegant approximation. Requesting low accuracy produces a familiar approximation of $\pi$. The accuracy of rat is determined by an optional tolerance, rat(x,tol). With two output arguments, rat unwinds the continued fraction to produce two integers whose ratio has the same value. Let's generate increasingly accurate rational approximations to $\pi$. That means it may take fewer terms to obtain a specified accuracy, but the continued fractions are, shall I say, unorthodox. But 40 years ago, I got clever and used round instead of floor. That always leaves a positive fraction to reciprocate. To generate proper continued fractions, integer part should be floor. The controversary comes from what is meant by integer part. If the input is irrational, the process does not terminate. If there is nothing left to reciprocate, the process terminates because the input is a rational number. The core of rat generates continued fractions by repeatedly subtracting off the integer part and taking the reciprocal of what is left. So, we don't have much use for rat and format rat today, but they're still there and, as I said, they're moderately controversial. Besides, most important MATLAB calculations involve larger matrices, and eigenvalues, differential equations and signal processing. Don't try to hide itīut I have learned that it is better to explain floating point arithmetic than it is to try to hide it. For this example, almost everybody would vote for the rational format. Or the second, printed with a rational format that seeks to disguise the roundoff. Which version of X do you prefer? The first, printed with a floating point format so the roundoff errors are readily apparent. Let's try that second example with modern MATLAB and format rat. B = RAT(X) Some examples: longĭ = 1:8, e = ones(d), A = abs(d'*e - e'*d) + 1/dk)) with k = RAT(X) produces integer matrices A and B so that A. RAT(X) approximates each element of X by a continuedįraction of the form a/b = d1 + 1/(d2 + 1/(d3 +. Roundoff error from results that should be "simple" Help rat RAT An experimental function which attempts to remove the Here is the help entry from 40 years ago. It was one of the 73 functions in my original Fortran MATLAB. The function rat is older than MathWorks.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |