Numerické integrovanie

f(x), PHP - vložiť dolár
ε > 0
<a, b>, a < b
m >= 2
platných miest

Implementácia

JavaScript

$("#integral_nahrad").on('submit', '#integral', function(e) {
        e.preventDefault();
        $(this).append("<input type='hidden' name='ok' value='vypočítať'>");
 	$.post("php/ulohy/numericke_integrovanie.php", $(this).serialize(), function(vystup) {
		$("#integral_nahrad").html(vystup)		
	});
});

PHP

<?php
//default hodnoty
if (!isset($_POST['fx'])) $_POST['fx'] = 'exp($x)';
if (!isset($_POST['e'])) $_POST['e'] = 0.001;
if (!isset($_POST['a'])) $_POST['a'] = -1;
if (!isset($_POST['b'])) $_POST['b'] = 1;
if (!isset($_POST['m'])) $_POST['m'] = 4;

//odoslanie formulara
if ($_POST['ok'])
{
	//overenie podmienok
	if (!$_POST['fx']) die ('fcia je zle zadaná');
	if (!$_POST['e']) die ('ε > 0');
	if ($_POST['a'] >= $_POST['b']) die ('a < b');
	if ($_POST['m'] < 2) die ('m >= 2');
	
	$e = $_POST['e'];
	$a = $_POST['a'];
	$b = $_POST['b'];
	$m = $_POST['m'];
	
	//vypocet
	$f = create_function('$x', 'return '.$_POST['fx'].';'); //registracia fcie

	//obdlznikova metoda
	$out .= "<p><strong>Obdĺžniková metóda</strong></p>";
	$I = 0;
	for($krok = 1; $krok <= 1000; $krok++)
	{
		$h = ($b-$a)/$m; //sirka dielika
		
		$x = $a;
		$suma = 0;
		for ($i = 1; $i <= $m; ++$i)
		{
			$xi = $a+$i*$h;
			$suma += $f(($x+$xi)/2);
			$x = $xi;		
		}
		
		$Ii = $h*$suma; //priebezny vysledok
		$out .= "I<sub>$krok</sub> = $Ii<br />";
		
		if (abs($Ii-$I) <= $e) break;
		$I = $Ii;
		$m *= 2;		
	}
	
	$m = $_POST['m']; //reset poctu dielikov
	
	//lichobeznikova metoda
	$out .= "<p><strong>Lichobežníková metóda</strong></p>";
	$I = 0;
	for($krok = 1; $krok <= 1000; $krok++)
	{
		$h = ($b-$a)/$m; //sirka dielika
		unset($suma);
		
		$x = $a;
		for ($i = 0; $i <= $m; ++$i)
		{
			$xi = $a+$i*$h;
			$suma[] = $f($xi);
			$x = $xi;		
		}
		
		$Ii = $h/2*(array_sum(array_slice($suma,0,$m))+array_sum(array_slice($suma,1))); //priebezny vysledok
		$out .= "I<sub>$krok</sub> = $Ii<br />";
		
		if (abs($Ii-$I) <= $e) break;
		$I = $Ii;
		$m *= 2;		
	}
}

echo "
<form id='integral' method='post'>
<input name='fx' type='text' placeholder='f(x)' required value='$_POST[fx]'> f(x), PHP - vložiť dolár<br>
<input name='e' type='text' placeholder='ε' required value='$_POST[e]'> ε &gt; 0<br>
<input name='a' type='text' placeholder='a' value='$_POST[a]' size='3' required>
<input name='b' type='text' placeholder='b' value='$_POST[b]' size='3' required>
 &lt;a, b&gt;, a &lt; b<br />
<input name='m' type='text' placeholder='dielikov' value='$_POST[m]' required> m &gt;= 2<br>
<input type='submit' name='ok' value='vypočítať'>
<p>$out<p>
</form>";
?>