-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex13.m
62 lines (56 loc) · 1.7 KB
/
ex13.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
57
58
59
60
61
62
clear variables;
strF = input('Insira a equação da função f em x: ', 's');
a = input('Insira o valor de a: ');
b = input('Insira o valor de b: ');
nprobe = input('Insira o valor de nprobe: ');
tol = input('Insira o valor da tolerância tol: ');
strF = strcat('@(x)', strF);
f = str2func(strF);
intervalSize = (b - a)/nprobe;
roots = [];
for startInt = a : intervalSize : b - intervalSize
endInt = startInt + intervalSize;
res = f(startInt) * f(endInt);
foundRoot = 0;
if res < 0
strt = startInt;
[output(1), output(2)] = secant(f, strt, endInt, tol);
%se a raiz não foi encontrada, aplico 3 bisecções
%e aciono o método da secante novamente
while output(2) == 0 && foundRoot == 0
%Método da bisecção
for i = 1:3
m = (strt + endInt)/2;
if f(strt)*f(m) < 0
endInt = m;
else
if f(strt)*f(m) == 0
roots = [roots, m];
foundRoot = 1;
break;
else
strt = m;
end
end
end
if foundRoot == 0
[output(1), output(2)] = secant(f, strt, endInt, tol);
end
end
if foundRoot == 0
roots = [roots, output(1)];
end
else
if res == 0
if f(startInt) == 0
roots = [roots, startInt];
end
if f(endInt) == 0
roots = [roots, endInt];
end
end
end
end
for i=1:length(roots)
disp(roots(i));
end