0
var a = document.forms[0].elements[0].value;
var b = document.forms[0].elements[1].value;
var c = document.forms[0].elements[2].value;
var d = document.forms[0].elements[3].value;
var Xp = document.forms[0].elements[4].value;
var Yp = document.forms[0].elements[5].value;

document.forms[0].elements[7].value = a/c;

document.forms[0].elements[8].value = (b*c-a*d)/(c*c);

document.forms[0].elements[9].value = c;

document.forms[0].elements[10].value = d;

document.forms[0].elements[11].value = Yp-(a/c*Xp)-((b*c-a*d)/(c*c)*(Math.log(Math.abs(c*Xp+d))));` 

element 11 should be -1 but instead i get 6.20. I understood that the error is from Math.log to ; is Math.log the natural logarithm? cause i have to do that

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
user2580684
  • 27
  • 1
  • 6

2 Answers2

0

See the documentation of Math.log : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log

For further help you should ask your math teacher or someone who knows about algorithms ;-P

maybe you should see the part: ((b*c-a*d)/(c*c)*(Math.log(Math.abs(c*Xp+d)))) Maye you want to have the braces done like (x/(z*y)) or ((x*y)/z).

jko
  • 2,068
  • 13
  • 25
0

Edit : Using forms to input data Working code

<!DOCTYPE html>
<html>
<body>

<script>

function foo (form)
{


var a = form.elements[0].value
var b = form.elements[1].value;
var c = form.elements[2].value;
var d = form.elements[3].value;
var Xp = form.elements[4].value;
var Yp = form.elements[5].value;
var z = ((c*1)*(Xp)+(d*1));

var K = Yp-(a/c*Xp)-((b*c-a*d)/(c*c)*(Math.log(Math.abs(z))));
alert(K);

}

</script>

<form id="frm1" action="" method = "Get">
  a: <input type="number" name="a" value="1"><br>
  b: <input type="number" name="b" value="-3"><br>
  c: <input type="number" name="c" value="1"><br>
  d: <input type="number" name="d" value="4"><br>
  Xp: <input type="number" name="Xp" value="1"><br>
  Yp: <input type="number" name="Yp" value="-11.2661"><br>
  <input type="button" id = "Submit" value="Read" onClick="foo(this.form)"/>
</form> 


</body>

</html>

The problem lied with the (c*Xp + d). You see instead of evaluating (1+4) = 5 the code saw c d and Xp as strings and came out with (1+4) = 14 as adding one string to another. using z = ((c*1))*(Xp) + (d*1)) must somehow cast the values to double so they evaluate to the right digit of 5.

Conor Linehan
  • 448
  • 3
  • 11
  • the fact is that i took the values from text boxes in my html page and instead of writing 'Yp' as -7ln(5) i wrote -11.2661 and when i calculate the result it ends up being 6.20... – user2580684 Nov 24 '13 at 16:41
  • Right after doing that I still get -1 (well -1.000058 to be exact) can you post up the exact code your using? – Conor Linehan Nov 24 '13 at 17:01
  • Alright I dont really know much about html/javascript but from what we've gathered so far we know the problems lies in the document.forms section (because the formula works when the values are hardcoded in) so we have to solve the input problem – Conor Linehan Nov 24 '13 at 17:29
  • I've found a good link that explains how to retrive data from a form aswell. http://stackoverflow.com/questions/2435525/best-practice-access-form-elements-by-html-id-or-name-attribute – Conor Linehan Nov 24 '13 at 17:30
  • Id ask other people is there a better way to cast values though and if you plan on changing any of the other values you'll have to cast them all to numerical digits i'd imagine – Conor Linehan Nov 24 '13 at 19:40
  • No bothers :) if it answers your question can you mark it as chossen answer please ?? :) – Conor Linehan Nov 24 '13 at 20:54
  • Heres a link that explains the whole problems of types in javascript aswell http://www.quirksmode.org/js/strings.html – Conor Linehan Nov 24 '13 at 20:58