if (!array_key_exists('rad',$_REQUEST))
{
$_REQUEST['rad'] = '';
}
if (!array_key_exists('data',$_REQUEST))
{
$_REQUEST['data'] = '';
}
if (!array_key_exists('num',$_REQUEST))
{
$_REQUEST['data'] = 0;
}
if (!array_key_exists('from',$_REQUEST))
{
$_REQUEST['from'] = 0;
}
if (!array_key_exists('to',$_REQUEST))
{
$_REQUEST['to'] = 0;
}
if (!array_key_exists('num',$_REQUEST))
{
$_REQUEST['num'] = 0;
}
function calculate_value()
{
if ($_REQUEST['rad'] == '')
{
echo "Choose Integration Type";
return;
}
if ($_REQUEST['from'] === '' || $_REQUEST['to'] === '')
{
echo "Enter Interval";
return;
}
if ($_REQUEST['from'] >= $_REQUEST['to'])
{
echo "Interval out of Order";
return;
}
if ($_REQUEST['num'] == 0)
{
echo "Enter Number of points at N:";
return;
}
$n = $_REQUEST['num'];
if ($n > 20)
{
echo "Maximum 20 points";
return;
}
if ($n < 2)
{
echo "Minimum 2 points";
return;
}
for($i=0;$i<$n;$i++)
{
if ($_REQUEST['data'][$i] === '')
{
echo "Missing Data Item ";
echo $i+1;
return;
}
}
switch($_REQUEST['rad'])
{
case 'l': calculate_left(); break;
case 'r': calculate_right(); break;
case 't': calculate_trap(); break;
case 's': calculate_simp(); break;
default: echo("Choose Type");
}
}
function calculate_left()
{
$data = $_REQUEST['data'];
$n = $_REQUEST['num'];
$from = $_REQUEST['from'];
$to = $_REQUEST['to'];
$delta = ($to-$from)/($n-1);
$amount = 0;
for($i=0;$i<$n-1;$i++)
{
$amount += ($data[$i] * $delta);
}
echo "Value: " . $amount . "
\n";
}
function calculate_right()
{
$data = $_REQUEST['data'];
$n = $_REQUEST['num'];
$from = $_REQUEST['from'];
$to = $_REQUEST['to'];
$delta = ($to-$from)/($n-1);
$amount = 0;
for($i=1;$i<$n;$i++)
{
$amount += ($data[$i] * $delta);
}
echo "Value: " . $amount . "
\n";
}
function calculate_trap()
{
$data = $_REQUEST['data'];
$n = $_REQUEST['num'];
$from = $_REQUEST['from'];
$to = $_REQUEST['to'];
$delta = ($to-$from)/($n-1);
$amount = 0;
echo "FROM: " . $from . " TO:" . $to . " N:" . $n . " DELTA:" . $delta . "
";
for($i=0;$i<($n-1);$i++)
{
$amount += (($data[$i]+$data[$i+1])/2 * $delta);
}
echo "Value: " . $amount . "
\n";
}
function calculate_simp()
{
$data = $_REQUEST['data'];
$n = $_REQUEST['num'];
$from = $_REQUEST['from'];
$to = $_REQUEST['to'];
$delta = ($to-$from)/($n-1);
$amount = 0;
if ($n < 3)
{
echo "Simpson Rule requires at least 3 data points
\n";
return;
}
if (($n & 1) == 0)
{
echo "Simpson Rule requires an odd number of data points
\n";
return;
}
for($i=0;$i<$n-1;$i+=2)
{
$amount += (($data[$i]+$data[$i+1]*4+$data[$i+2])/3 * $delta);
}
echo "Value: " . $amount . "
\n";
}
?>