T
transkawa
I wrote the script below. I know it runs well but it does not run on
javascript or is there something about javascript i am missing out?
safari reports slow script and does not give me any error report aside
from that. Firefox tells me that there is error on the formula function
which is not true or is that where the script starts to get slow?
need help. just want to know what about javascript and its use on html
environments i don't know about.
xnt
/*
* this program demonstrates the half-interval method of looking for
* the roots of functions i.e where f(x)=0. it takes two points of
different
* signs and then by repeated averaging of their intervals comes to a
very
* close point where x is or very close to 0 by 0.01, i.e the tolerance
for
* deviation from 0.
*/
//global variables
var positive =0;
var negative =0;
var result =0;
function halfInterval(ap, bp){
var temp1 = formula(ap);
var mebug = isZero(temp1);
if(mebug == 'true'){
result = temp1;
return result;
}
var temp2 = formula(bp);
mebug = isZero(temp2);
if(mebug == 'true'){
result = temp2;
return result;
}
var apositive = isPositive(temp1);
var anegative = isNegative(temp1);
var bpositive = isPositive(temp2);
var bnegative = isNegative(temp2);
if ((apositive == true) && (bpositive == 'true')){
if ((anegative == 'true') && (bnegative == 'true')){
return 'both values are of same sign; error.';
}
}
if(apositive == 'true'){
positive = ap;
negative = bp;
}
else{
positive = bp;
negative = ap;
}
result = search(positive, negative);
return result;
}
function search(positive, negative){
var got = 'false';
var midpointX, midpointY;
while(got == 'false'){
midpointX = (positive + negative) / 2;
midpointY = formula(midpointX);
//check for zero tolerance
var zeroIn = isZero(midpointY);
if(zeroIn == 'true'){
got = 'true'; //out of the loop
}
//check for the sign of midpoint
zeroIn = isPositive(midpointY);
//now change loop variables
if (zeroIn == 'true'){
positive = midpointX;
}else {
negative = midpointX;
}
}
return midpointX;
}
function isPositive(x){
return (x > 0);
}
function isNegative(x){
return (x < 0);
}
function isZero(x){
var res = (!(Math.abs(x) > 0.01));
return res;
}
function formula(x){
return (3*x*x - 27); //firefox 3.6 tells me the error is here
}
function checkFeat(){
try{
//function to check now is 3x(squared) - 27. zero is 9
var apoint = parseInt(prompt('input the first x value'));
var bpoint = parseInt(prompt('input the second x value'));
//call half interval function
var ops = halfInterval(apoint, bpoint);
//return result to three decimal places
document.getElementById('res').value = ops;
}
catch(err){
alert('error report: '+ err.toString());
}
}
the html:
<body>
<form action="http://www.example.com/" id="form1">
<input type="button" id="but1" name="but1" value="click this
button!"
onclick="checkFeat();"><br />
<label for="res">Result:</label>
<input type="text" size="20" id="res" >
</form>
</body>
thanks for all your reponses. TFAYR.
xnt
javascript or is there something about javascript i am missing out?
safari reports slow script and does not give me any error report aside
from that. Firefox tells me that there is error on the formula function
which is not true or is that where the script starts to get slow?
need help. just want to know what about javascript and its use on html
environments i don't know about.
xnt
/*
* this program demonstrates the half-interval method of looking for
* the roots of functions i.e where f(x)=0. it takes two points of
different
* signs and then by repeated averaging of their intervals comes to a
very
* close point where x is or very close to 0 by 0.01, i.e the tolerance
for
* deviation from 0.
*/
//global variables
var positive =0;
var negative =0;
var result =0;
function halfInterval(ap, bp){
var temp1 = formula(ap);
var mebug = isZero(temp1);
if(mebug == 'true'){
result = temp1;
return result;
}
var temp2 = formula(bp);
mebug = isZero(temp2);
if(mebug == 'true'){
result = temp2;
return result;
}
var apositive = isPositive(temp1);
var anegative = isNegative(temp1);
var bpositive = isPositive(temp2);
var bnegative = isNegative(temp2);
if ((apositive == true) && (bpositive == 'true')){
if ((anegative == 'true') && (bnegative == 'true')){
return 'both values are of same sign; error.';
}
}
if(apositive == 'true'){
positive = ap;
negative = bp;
}
else{
positive = bp;
negative = ap;
}
result = search(positive, negative);
return result;
}
function search(positive, negative){
var got = 'false';
var midpointX, midpointY;
while(got == 'false'){
midpointX = (positive + negative) / 2;
midpointY = formula(midpointX);
//check for zero tolerance
var zeroIn = isZero(midpointY);
if(zeroIn == 'true'){
got = 'true'; //out of the loop
}
//check for the sign of midpoint
zeroIn = isPositive(midpointY);
//now change loop variables
if (zeroIn == 'true'){
positive = midpointX;
}else {
negative = midpointX;
}
}
return midpointX;
}
function isPositive(x){
return (x > 0);
}
function isNegative(x){
return (x < 0);
}
function isZero(x){
var res = (!(Math.abs(x) > 0.01));
return res;
}
function formula(x){
return (3*x*x - 27); //firefox 3.6 tells me the error is here
}
function checkFeat(){
try{
//function to check now is 3x(squared) - 27. zero is 9
var apoint = parseInt(prompt('input the first x value'));
var bpoint = parseInt(prompt('input the second x value'));
//call half interval function
var ops = halfInterval(apoint, bpoint);
//return result to three decimal places
document.getElementById('res').value = ops;
}
catch(err){
alert('error report: '+ err.toString());
}
}
the html:
<body>
<form action="http://www.example.com/" id="form1">
<input type="button" id="but1" name="but1" value="click this
button!"
onclick="checkFeat();"><br />
<label for="res">Result:</label>
<input type="text" size="20" id="res" >
</form>
</body>
thanks for all your reponses. TFAYR.
xnt