forked from vsellemi/computational-physics-tools-MATLAB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecant.m
More file actions
22 lines (18 loc) · 610 Bytes
/
secant.m
File metadata and controls
22 lines (18 loc) · 610 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function [y,iterations] = secant(f,a,b,e)
%this function attempts to find the root using the secant method
%write the function in f = 0 form
%provide initial interval in the form [a,b]
%specify absolute error tolerance e
y = (f(a)*b - f(b)*a)/(f(a)-f(b)); %implement secant method iterations
error = abs(f(y)); %calculate absolute error
count = 0; %initialize count of iterations
while error > e
%check if error is below set tolerance
count = count + 1;
a = b;
b = y;
y = (f(a)*b - f(b)*a)/(f(a)-f(b)); %output estimate for root
error = abs(f(y));
iterations = count;
end
end