1
PHP / Método numerico de Bisección
« en: 18 de Septiembre de 2012, 18:05:01 pm »
Hola,
El siguiente código permite realizar la busqueda de raices por el método de bisección, se pueden corregir algunas cosas dentro del código, pero funciona correctamente:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post">
<p>an</p>
<input type="text" name="an" />
<p>bn</p>
<input type="text" name="bn" />
<p>Función</p>
<input type="text" name="fun" />
<p>Iteraciones</p>
<input type="text" name="ite" />
<br><br>
<input type="submit" name="calcular" value="calcular">
</form>
<?php
if (isset($_POST["calcular"])) {
$an = $_POST["an"];
$bn = $_POST["bn"];
$it = $_POST["ite"];
?>
<table border="1">
<tr>
<td>n</td>
<td>an</td>
<td>bn</td>
<td>pn</td>
<td>F(Pn)</td>
<td>Error</td>
</tr>
<tr>
<?php
$err="";
$pn = array();
for ($n=1; $n<=$it; $n++) {
$eva = ($an + $bn)/2;
$fun = str_replace("x",$eva,$_POST["fun"]);
eval("\$foo = $fun;");
$pn[]=$eva;
if ($n>1) {
$err = abs(($eva - $pn[$n-2])/$eva);
}
echo "<td>".$n."</td><td>".$an."</td><td>".$bn."</td><td>".$eva."</td><td>".$foo."</td><td>".$err."</tr></tr>";
if ($foo<0 AND $err<0 ) {
$bn = $eva;
} elseif ($foo>0 AND $err>0 ) {
$bn = $eva;
} else {
$an = $eva;
}
}
echo "</table>";
}
?>
</body>
</html>
El siguiente código permite realizar la busqueda de raices por el método de bisección, se pueden corregir algunas cosas dentro del código, pero funciona correctamente:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form method="post">
<p>an</p>
<input type="text" name="an" />
<p>bn</p>
<input type="text" name="bn" />
<p>Función</p>
<input type="text" name="fun" />
<p>Iteraciones</p>
<input type="text" name="ite" />
<br><br>
<input type="submit" name="calcular" value="calcular">
</form>
<?php
if (isset($_POST["calcular"])) {
$an = $_POST["an"];
$bn = $_POST["bn"];
$it = $_POST["ite"];
?>
<table border="1">
<tr>
<td>n</td>
<td>an</td>
<td>bn</td>
<td>pn</td>
<td>F(Pn)</td>
<td>Error</td>
</tr>
<tr>
<?php
$err="";
$pn = array();
for ($n=1; $n<=$it; $n++) {
$eva = ($an + $bn)/2;
$fun = str_replace("x",$eva,$_POST["fun"]);
eval("\$foo = $fun;");
$pn[]=$eva;
if ($n>1) {
$err = abs(($eva - $pn[$n-2])/$eva);
}
echo "<td>".$n."</td><td>".$an."</td><td>".$bn."</td><td>".$eva."</td><td>".$foo."</td><td>".$err."</tr></tr>";
if ($foo<0 AND $err<0 ) {
$bn = $eva;
} elseif ($foo>0 AND $err>0 ) {
$bn = $eva;
} else {
$an = $eva;
}
}
echo "</table>";
}
?>
</body>
</html>