-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixedPoint.m
52 lines (39 loc) · 936 Bytes
/
fixedPoint.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function [i,root,data,timeElapsed] = fixedPoint(f, g, xi, epsilon, maxNumberOfIterations)
tic;
data = 0;
root = 0;
i = 1;
if f(xi) == 0
root = xi;
data(1,1) = xi;
data(1,2) = xi;
data(1,3) = 0;
timeElapsed = toc;
return;
end
syms x
differentiation = eval(['@(x)' char(diff(g(x)))]);
result = abs(differentiation(xi));
if result >= 1
i = 0;
timeElapsed = toc;
errordlg('Functions diverges, Change g(x).');
return;
end
while true
root = g(xi);
approximateError = abs((root - xi)/root) * 100;
% data(i,1) = i;
data(i,1) = xi;
data(i,2) = root;
data(i,3) = approximateError;
%fprintf('%2i %f %f %f \n', i, xi, root, approximateError);
[done] = checkConditions(i, maxNumberOfIterations, approximateError, epsilon, f, root);
if (done == true)
break;
end
i = i + 1;
xi = root;
end
timeElapsed = toc;
end