Help in Javascript with getElementById

2

Very good to all, I've been trying for a while and nothing ... could someone correct me and tell me what my mistake is? all the code works well, the error is in wanting to take the values of the form, that's what does not work for me.

<!DOCTYPE html>
<html>
<head>
<title>Metodo de bisecion</title>
</head>

<body>
<h2>La funcion a iterar es: 6x^3-2x^2-x-1 = 0 en [0.5, 1]</h2><br><br>

<script type="text/javascript">

var n, an, fan, bn, fbn, pn, fpn;
n = 1;
an = parseFloat(document.getElementById("an").value);
bn = parseFloat(document.getElementById("bn").value);



//Comienza la iteracion
function biseccion(){

    do {

        pn = (an+bn)/2;


        fpn =Math.abs((6*(Math.pow(pn, 3))) - (2*(Math.pow(pn, 2))) - pn - 1);

        document.write(" | " + n + " | " + an + " | " + bn + " | " + pn + " | " + fpn + " | " + "<br><br>");

        fpn =(6*(Math.pow(pn, 3))) - (2*(Math.pow(pn, 2))) - pn - 1;

        fan =(6*(Math.pow(an, 3))) - (2*(Math.pow(an, 2))) - an - 1;

        if (fpn*fan > 0) {
            an = pn;
            bn = bn;
        }
        else if (fpn*fan < 0) {
            bn = pn;
            an = an;
        }

        else{

            alert("No se pudo determinar");
        }

        fpn =Math.abs((6*(Math.pow(pn, 3))) - (2*(Math.pow(pn, 2))) - pn - 1);

        n++;

    } while (fpn > 0.0001);

};

</script>

<!-- Formulario para ingresar intervalo de valores -->

<form>

a:<input type="text" id="an"><br><br>
b:<input type="text" id="bn"><br><br>
<input type="button" value="Comenzar iteraciones" onclick="biseccion()">

</form>

</body>

</html>
    
asked by Power_HR 26.03.2016 в 22:32
source

2 answers

2

You take the values of an and bn at the beginning, but it is not updated, if you put the two lines with the getElementById inside the function, so that they use the value at the moment of pressing the key, it should work.

    
answered by 26.03.2016 в 22:41
2

The way you get the values is correct:

valor = document.getElementById("an").value;

but you could define them inside, when you execute your function I would take the values, with that it would be enough:

<!DOCTYPE html>
<html>
<head>
<title>Metodo de bisecion</title>
<script>




var n, an, fan, bn, fbn, pn, fpn;
n = 1;


//Comienza la iteracion
function biseccion(){

an = parseFloat(document.getElementById("an").value);
bn = parseFloat(document.getElementById("bn").value);

alert(an);
alert(bn);

    do {

        pn = (an+bn)/2;


        fpn =Math.abs((6*(Math.pow(pn, 3))) - (2*(Math.pow(pn, 2))) - pn - 1);

        document.write(" | " + n + " | " + an + " | " + bn + " | " + pn + " | " + fpn + " | " + "<br><br>");

        fpn =(6*(Math.pow(pn, 3))) - (2*(Math.pow(pn, 2))) - pn - 1;

        fan =(6*(Math.pow(an, 3))) - (2*(Math.pow(an, 2))) - an - 1;

        if (fpn*fan > 0) {
            an = pn;
            bn = bn;
        }
        else if (fpn*fan < 0) {
            bn = pn;
            an = an;
        }

        else{

            alert("No se pudo determinar");
        }

        fpn =Math.abs((6*(Math.pow(pn, 3))) - (2*(Math.pow(pn, 2))) - pn - 1);

        n++;

    } while (fpn > 0.0001);

};

</script>
</head>

<body>
<h2>La funcion a iterar es: 6x^3-2x^2-x-1 = 0 en [0.5, 1]</h2><br><br>

<form>

a:<input type="text" id="an"><br><br>
b:<input type="text" id="bn"><br><br>
<input type="button" value="Comenzar iteraciones" onclick="biseccion()">

</form>

</body>

</html>
    
answered by 26.03.2016 в 22:50