var newton = function(f, df, a, eps, eps2, maxIter) { var x = a, cnt = 0; do { v
ID: 3800202 • Letter: V
Question
var newton = function(f, df, a, eps, eps2, maxIter) { var x = a, cnt = 0; do { var fx = f(x), dfx = df(x), x2 = x-fx/dfx; if (Math.abs(x2 - x) < eps) { return [x2,++cnt,fx,x2-x,'eps']; } else if (Math.abs(fx) < eps2) { return [x,++cnt,fx,x2-x,'eps2']; } else { x = x2; } } while (++cnt < maxIter); return [x, ++cnt,fx, x2-x, 'cnt']; } var f = function(x){return -1;}; var df = function(x) {return (-1/4)}; With no java calculator: (1) What is the result of newton(f,df,1,Math.pow(10,-15),Math.pow(10,-15),3)) ? (2) What is the result of newton(f,df,Math.pow(10,-15),Math.pow(10,-15), Math.pow(10,-15),3)) ?
Explanation / Answer
var newton = function(f, df, a, eps, eps2, maxIter) {
var x = a, cnt = 0;
do {
var fx = f(x), dfx = df(x), x2 = x-fx/dfx;
if (Math.abs(x2 - x) < eps) {
return [x2,++cnt,fx,x2-x,'eps'];
} else if (Math.abs(fx) < eps2) {
return [x,++cnt,fx,x2-x,'eps2'];
} else {
x = x2;
}
} while (++cnt < maxIter);
return [x, ++cnt,fx, x2-x, 'cnt'];
}
var f = function(x){return -1;};
var df = function(x) {return (-1/4)};
Solution:
[-11, 4, -1, 0, "cnt"]
[-12, 4, -1, 0, "cnt"]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.