From: transkawa on
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
--
Never question, keep asking
234 702 866 8957
From: Dr J R Stockton on
In comp.lang.javascript message <MPG.267393adb13a1f259896a2(a)nntp.aioe.or
g>, Tue, 8 Jun 2010 18:51:24, transkawa <transkawa(a)yahoo.fr> posted:

>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.

Your code seems a lot longer than the code for
<URL:http://www.merlyn.demon.co.uk/js-demos.htm#FZ>.


> * close point where x is or very close to 0 by 0.01, i.e the tolerance

Perhaps better to use a variable limit, or execute until there is no
improvement


> if(mebug == 'true'){

Don't use 'true' as a Boolean; use true, and don't use ==true as there
is no need.


> if ((apositive == true) && (bpositive == 'true')){
> if ((anegative == 'true') && (bnegative == 'true')){
> return 'both values are of same sign; error.';

More easily done with an XOR operation, or by multiplying temp1 & temp2
and checking the sign.


> var apoint = parseInt(prompt('input the first x value'));

Function parseInt should for safety be given an explicit second
argument; but it is better to use a unary + - read the newsgroup FAQ.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE8 FF3 Op10 Sf4 Cr4
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
From: transkawa on
In article <iWCpDOP4D$DMFwHY(a)invalid.uk.co.demon.merlyn.invalid>,
reply1023(a)merlyn.demon.co.uk says...
>
> In comp.lang.javascript message <MPG.267393adb13a1f259896a2(a)nntp.aioe.or
> g>, Tue, 8 Jun 2010 18:51:24, transkawa <transkawa(a)yahoo.fr> posted:
>
> >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.
>
> Your code seems a lot longer than the code for
> <URL:http://www.merlyn.demon.co.uk/js-demos.htm#FZ>.
>
>
> > * close point where x is or very close to 0 by 0.01, i.e the tolerance
>
> Perhaps better to use a variable limit, or execute until there is no
> improvement
>
>
> > if(mebug == 'true'){
>
> Don't use 'true' as a Boolean; use true, and don't use ==true as there
> is no need.
>
>
> > if ((apositive == true) && (bpositive == 'true')){
> > if ((anegative == 'true') && (bnegative == 'true')){
> > return 'both values are of same sign; error.';
>
> More easily done with an XOR operation, or by multiplying temp1 & temp2
> and checking the sign.
>
>
> > var apoint = parseInt(prompt('input the first x value'));
>
> Function parseInt should for safety be given an explicit second
> argument; but it is better to use a unary + - read the newsgroup FAQ.

thanks for your input Dr. Stockton. noted.

--
happy are those who have endured
for they shall reap bountifully
--the gospel according to an avatar