-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecantMethod.m
66 lines (58 loc) · 1.54 KB
/
SecantMethod.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
63
64
65
66
clc,clear all,close all;
format longG;
maxiter = 100; % max number of iteration before get the answer
f = @(x) x.^3+2*x.^2+10*x-20
low = [0,0,0,0,0,0];
high = [2.0,2.0,2.0,10.0,10.0,10.0];
tolerance = [1e-6,1e-8,1e-10,1e-6,1e-8,1e-10];
plot_it = 1;
for n= 1:3
SecantMet(f,tolerance(n),low(n),high(n),plot_it);
plot_it = plot_it + 2;
end
function SecantMet(f,tol,lower,higher,plot_it)
figure(1)
fplot(f,[-100,100]);
hAxis = gca;
hAxis.XAxisLocation = 'origin';
hAxis.YAxisLocation = 'origin';
x_0_vec = [];
fx_0_vec = [];
maxiter=1000;
xn = (lower*f(higher) - higher*f(lower))/(f(higher) - f(lower));
disp('xn-2 f(xn-2) xn-1 f(xn-1) xn f(xn)');
disp(num2str([lower f(lower) higher f(higher) xn f(xn)],'%20.7f'));
flag = 1;
x_0_vec(end + 1) = xn;
fx_0_vec(end + 1) = f(xn);
while abs(f(xn)) > tol
lower = higher;
higher = xn;
xn = (lower*f(higher) - higher*f(lower))/(f(higher) - f(lower));
x_0_vec(end + 1) = xn;
disp(num2str([lower f(lower) higher f(higher) xn f(xn)],'%20.7f'));
fx_0_vec(end + 1) = f(xn);
flag = flag + 1;
if(flag == maxiter)
break;
end
end
iterations = [1:flag];
figure(2)
subplot(3,2,plot_it);
plot(iterations,x_0_vec);
title('Iteration vs X0');
xlabel('Iterations')
ylabel('x0')
subplot(3,2,plot_it+1);
plot(iterations,fx_0_vec);
title('Iteration vs f(x)');
xlabel('Iterations')
ylabel('f(x)')
if flag < maxiter
disp(['Root is x = ' num2str(xn)]);
Root = xn
else
disp('Root does not exist');
end
end