Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- </head>
- <body>
- <script type="text/javascript">
- function f (x) {
- return x*x - 2;
- }
- function algoritm(a, b, nr_iteratii) {
- let c = (a + b) / 2;
- for (var i = 1; i <= nr_iteratii; i ++) {
- if (f(a) * f(c) < 0) {
- b = c;
- } else {
- a = c;
- }
- c = (a+b)/2;
- console.log("a = " + a + ", b = " + b + ", c = " + c + ", f(a) = " + f(a) + ", f(b) = " + f(b) + ", f(c) = " + f(c));
- }
- }
- function eroare(a, b) {
- let c = new Array();
- // c.push((a + b) / 2);
- let c1 = (a+b)/2;
- let c2 = (a+b)/2;
- while(true) {
- if (f(a) * f(c2) < 0) {
- b = c2;
- } else {
- a = c2;
- }
- c1=c2;
- c2 = (a+b)/2;
- if (Math.abs(c2-c1) < 0.001) {
- console.log(c2 + " " + c1);
- return;
- }
- }
- }
- algoritm(1, 2, 10);
- eroare(1, 2);
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement