Search code examples
phptimeexecutiontrigonometry

PHP "Maximum execution time"


I'm trying to program my own Sine function implementation for fun but I keep getting :

Fatal error: Maximum execution time of 30 seconds exceeded

I have a small HTML form where you can enter the "x" value of Sin(x) your looking for and the number of "iterations" you want to calculate (precision of your value), the rest is PhP. The maths are based of the "Series definition" of Sine on Wikipedia : --> http://en.wikipedia.org/wiki/Sine#Series_definition Here's my code :

<?php

    function factorial($int) {
        if($int<2)return 1;
        for($f=2;$int-1>1;$f*=$int--);
        return $f;
    };

    if(isset($_POST["x"]) && isset($_POST["iterations"])) {
        $x = $_POST["x"];
        $iterations = $_POST["iterations"];
    }
    else {
        $error = "You forgot to enter the 'x' or the number of iterations you want.";
        global $error;
    }

    if(isset($x) && is_numeric($x) && isset($iterations) && is_numeric($iterations)) {

        $x = floatval($x);
        $iterations = floatval($iterations);

        for($i = 0; $i <= ($iterations-1); $i++) {
            if($i%2 == 0) {
                $operator = 1;
                global $operator;
            }
            else {
                $operator = -1;
                global $operator;
            }
        }

        for($k = 1; $k <= (($iterations-(1/2))*2); $k+2) {
            $k = $k;
            global $k;
        }

        function sinus($x, $iterations) {
            if($x == 0 OR ($x%180) == 0) {
                return 0;
            }
            else {
                while($iterations != 0) {
                    $result = $result+(((pow($x, $k))/(factorial($k)))*$operator);
                    $iterations = $iterations-1;
                    return $result;
                }
            }
        }

        $result = sinus($x, $iterations);
        global $result;
    }
    else if(!isset($x) OR !isset($iterations)) {
        $error = "You forgot to enter the 'x' or the number of iterations you want.";
        global $error;
    }
    else if(isset($x) && !is_numeric($x)&& isset($iterations) && is_numeric($iterations)) {
        $error = "Not a valid number.";
        global $error;
    }

?>

My mistake probably comes from an infinite loop at this line :

$result = $result+(((pow($x, $k))/(factorial($k)))*$operator);

but I don't know how to solve the problem. What I'm tring to do at this line is to calculate :

((pow($x, $k)) / (factorial($k)) + (((pow($x, $k))/(factorial($k)) * ($operator)

iterating :

+ (((pow($x, $k))/(factorial($k)) * $operator)

an "$iterations" amount of times with "$i"'s and "$k"'s values changing accordingly.

I'm really stuck here ! A bit of help would be needed. Thank you in advance !

Btw : The factorial function is not mine. I found it in a PhP.net comment and apparently it's the optimal factorial function.


Solution

  • Why are you computing the 'operator' and power 'k' out side the sinus function.

    sin expansion looks like = x - x^2/2! + x^3/3! ....

    something like this.

    Also remember iteration is integer so apply intval on it and not floatval. Also study in net how to use global. Anyway you do not need global because your 'operator' and power 'k' computation will be within sinus function.

    Best of luck.