-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolySolve.m
56 lines (40 loc) · 924 Bytes
/
PolySolve.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
52
53
54
55
56
function ret = PolySolve(p)
global zero_app
n = length(p) - 1;
ret = [];
if n < 1 || p(1) == 0
disp('Invalid input polynomial!');
return;
end
if n == 1
ret = - p(2) / p(1);
return;
end
diff = PolyDiff(p);
crit = PolySolve(diff);
if isempty(crit)
crit = [0];
end
count = 1;
v1 = polyval(p, crit(1));
if ~(abs(v1) < zero_app) && polyval(p, crit(1) - 1) / v1 < 1
ret(count) = PolyNR(p, crit(1) - 1);
count = count + 1;
end
for i = 1:(length(crit) - 1)
v2 = polyval(p, crit(i + 1));
if abs(v1) < zero_app
ret(count) = crit(i);
count = count + 1;
elseif ~(abs(v2) < zero_app) && v1 / v2 < 0;
ret(count) = PolyNR(p, (crit(i) + crit(i+1)) / 2);
count = count + 1;
end
v1 = v2;
end
if abs(v1) < zero_app
ret(count) = crit(end);
elseif polyval(p, crit(end) + 1) / v1 < 1
ret(count) = PolyNR(p, crit(end) + 1);
end
end