-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterval.m
More file actions
40 lines (40 loc) · 813 Bytes
/
Interval.m
File metadata and controls
40 lines (40 loc) · 813 Bytes
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
f=inline(input('Enter a function f(x) for minimization case','s'));
a=input('Enter the starting point of search point');
b=input('Enter the ending point of search point');
eps=input('Enter desired accuracy of the interval');
xm=(a+b)/2;
x1=(a+xm)/2;
x2=(b+xm)/2;
L=b-a;
iter=0;fprintf('%d\t%6.4f\t%6.4f\n',iter,a,b);
while L>eps
if f(xm)>=f(x1)
b=xm;
xm=(a+b)/2;
x1=(a+xm)/2;
x2=(b+xm)/2;
L=b-a;
iter=iter+1;
fprintf('%d\t%6.4f\t%6.4f\n',iter,a,b);
continue
elseif f(xm)>=f(x2)
a=xm;
xm=(a+b)/2;
x1=(a+xm)/2;
x2=(b+xm)/2;
L=b-a;
iter=iter+1;
fprintf('%d\t%6.4f\t%6.4f\n',iter,a,b);
continue
else
a=x1;b=x2;
xm=(a+b)/2;
x1=(a+xm)/2;
x2=(b+xm)/2;
L=b-a;
iter=iter+1;
fprintf('%d\t%6.4f\t%6.4f\n',iter,a,b);
continue
end
end
fprintf('Optimal lies between %6.3f and %6.3f',a,b);