Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- format long;
- es = (.5 * (10 ^ -3));
- f = @(z) z ^ 3 - 4 * z + 1;
- x0 = 0;
- x1 = 1;
- x(1) = x0;
- for i = 2:1:10
- %ans0 = f(x0);
- ans0 = (x0 ^ 3) - (4 * x0) + 1;
- ans1 = (x1 ^ 3) - (4 * x1) + 1;
- x(i) = x0 - ((ans0 * (x1 - x0)) / (ans1 - ans0));
- ans2 = (x(i) ^ 3) - (4 * x(i)) + 1;
- if(ans0 > 0 && ans2 < 0) || (ans0 < 0 && ans2 > 0)
- x1 = x(i);
- end
- if(ans1 > 0 && ans2 < 0) || (ans1 < 0 && ans2 > 0)
- x0 = x(i);
- end
- ea = abs(((x(i) - x(i - 1)) * 100 )/ x(i));
- %disp(ea);
- arr(i - 1) = ea;
- fprintf('%.5f %.5f %.5f --> %.5f\n', ans0, ans1, ans2, x(i));
- if(abs(ea) < abs(es))
- break;
- end
- %plot(x);
- plot(arr);
- end
- --------------------------------------------------------------------------
- format long;
- es = .5 * (10^-1);
- current = 1;
- fprintf('%d %d\n', 1, 1);
- for i = 1:1:100
- prev = current;
- current = current + ((.5 ^ i) / factorial(i));
- ea = ((current - prev) * 100) / current;
- fprintf('%d %f %.4f\n', i + 1, current, ea);
- %if(abs(ea) < abs(es))
- if(ea < es)
- break;
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement