Here is a program that asks the user for a number (variable r) to find the positive root of, and then asks for a starting interval [a,b]. This is done in some HTML code. The javascript below it has the code for linear interpolation inside a while loop.
function everything() {
r= document.getElementById('ri').value*1;
a= document.getElementById('ai').value*1;
b= document.getElementById('bi').value*1;
bisect(function(x){return x*x-r;},a,b);
}
function bisect(f,a,b) {
var avg,fa,fb;
avg = NaN;
while (Math.abs(a-b)>1e-10) {
fa=f(a);
fb=f(b);
if(fa*fb<0) {
grad=(fb-fa)/(b-a);
avg=a-(fa/grad);
favg=f(avg);
} else {
alert('There has been an error. Redifine the interval A to B');
break;
}
if (fa*favg<0) {
b=avg;
} else {
a=avg;
}
}
alert(avg);
}
The problem with this code is it returns the error text, and the final value for avg at the end. This is a problem.