Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I'm not sure how to title this problem actually, but I have a clumsy PHP code that I've used to get approximate fraction numbers for imaginary numbers like pi, phi, square root of 2, 3 and so on. I'd like to get a formula that I can use on MatLab and get both data table and draw a plot based on approximate fraction numbers. Maybe someone already can grab from this but I'll provide PHP code to complement the case:

$n = phi(); # imaginary number (complex number?)
$x = 500; # how many numbers to check
$max = 50; # how many instances to show
$precision = 0.0001;

# check every i against every j and make a comparison how near their values are to each other
for ($i=1; $i<$x; $i++) {
    for ($j=1; $j<$x; $j++) {
        # compared value is stored on array. very distant numbers needs to be discarded ($precision) or array gets easily too big, limit 64k
        if (($d = abs(($n - ($i/$j)))) && $d > $precision) continue;
        $c[] = array($i, $j, $d);
    }
}

# sort comparison chart by third index (2)
array_qsort($c, 2);

# print max best values from the sorted comparison chart
$count = count($c);
echo "closest fraction numbers for $n from $count calculated values are:<br />\n<br />\n";
$r = 0;
foreach ($c as $abc) {
    $r++;
    $d = $abc[0]/$abc[1];
    echo $abc[0] . '/' . $abc[1] . ' = ' . $d . ' (' . round($abc[2]*(1/$precision), 10) . ')' . "<br />\n";
    if ($r > $max) break;
}
share|improve this question
    
1. You mean irrational numbers, not imaginary. 2. If you're asking how to convert your PHP code to Matlab, you would do better to ask at StackOverflow. –  Rahul Mar 26 '12 at 3:05
    
Thanks for clarification, I wasn't sure of the term imaginary, complex, irrational. What comes to converting code, thats not necessary if I just can get a math formula to find fraction numbers. –  PHPGAE Mar 26 '12 at 3:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.