-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbisection.m
More file actions
55 lines (52 loc) · 1.2 KB
/
Copy pathbisection.m
File metadata and controls
55 lines (52 loc) · 1.2 KB
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
function [errorFlag,root, eps] = bisection(expression, upper, lower, epsilon,imax)
%
% root = zeros(imax,1);
% eps = zeros(imax,1);
try
errorFlag = '';
root = [0];
eps = [0];
for i=2:(imax+1)
eval('z=0;');
z = (upper +lower)/2; %lowerisectioiterNo method
eval('f1 = 0;');
eval('f2 = 0;');
eval('y=[];');
syms x;
try
y = subs(expression, x, z);
f1 = eval(y);
y = subs(expression, x, upper);
f2 = eval(y);
catch
errorFlag = 'Invalid Func or Div by 0';
return;
end
if(f1 * f2 < 0)
lower = z;
else
upper = z;
end
if(f1 == 0)
root(i) = z;
eps(i) = 0;
break;
end
root(i) = z;
eval('myeps=0.0;');
myeps = abs((z-root(i-1))/z);
eps(i) = abs(myeps);
if(z==0)
eps(i) = -1;
end
if(abs(myeps)<epsilon)
break;
end
end
catch
errorFlag = 'Un Idetified Error Occurred';
end
end
% display(root)
% display(iterNo)
% display(precision)