Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var eps = 0.001;
- function f(x) { return x*x + x - 6; }
- function df(x) { return 2*x + 1; }
- function sgn(x) {
- if (x > 0) return 1;
- if (x < 0) return -1;
- return 0;
- }
- function newtons(fx, dfx, x0) {
- var x0, x1 = x0 - fx(x0)/dfx(x0);
- var steps = 0;
- while (true) {
- x0 = x1;
- x1 = x1 - fx(x1)/dfx(x1);
- if (0 == fx(x1))
- break;
- if (fx(x1) * fx(x1 + sgn(x0 - x1)*eps) < 0)
- break;
- steps++;
- }
- console.log("X=" + x1 + " steps=" + steps);
- }
- function lab4Main() {
- newtons(f, df, -7.5);
- //newtons(f, df, 7.5);
- }
Advertisement
Add Comment
Please, Sign In to add comment