File tree Expand file tree Collapse file tree 3 files changed +94
-0
lines changed
algorithms/arithmetic_analysis Expand file tree Collapse file tree 3 files changed +94
-0
lines changed Original file line number Diff line number Diff line change 1+ % The basic idea behind the false position method is similar to the bisection
2+ % method in that we continuously shrink the interval the root lies on until
3+ % the algorithm converges on the root. Unlike the bisection method, the false
4+ % position method does not halve the interval with each iteration. Instead of
5+ % using the midpoint of a and b to create the new interval, the false position
6+ % method uses the x-intercept of the line connecting f(a) and f(b). This
7+ % algorithm converges faster than the bisection method.
8+
9+ % INPUTS:
10+ % Function handle f
11+ % endpoint a
12+ % endpoint b
13+ % maximum tolerated error
14+
15+ % OUTPUTS:
16+ % An approximated value for the root of f within the defined interval.
17+
18+ % Written by MatteoRaso
19+
20+ function y = false_position(f , a , b , error )
21+ if ~(f(a ) < 0 )
22+ disp(" f(a) must be less than 0" )
23+ elseif ~(f(b ) > 0 )
24+ disp(" f(b) must be greater than zero" )
25+ else
26+ c = 100000 ;
27+ while abs(f(c )) > error
28+ % Formula for the x-intercept
29+ c = - f(b ) * (b - a ) / (f(b ) - f(a )) + b ;
30+ if f(c ) < 0
31+ a = c ;
32+ else
33+ b = c ;
34+ endif
35+ disp(f(c ))
36+ endwhile
37+ x = [" The root is approximately located at " , num2str(c )];
38+ disp(x )
39+ y = c ;
40+ endif
41+ endfunction
Original file line number Diff line number Diff line change 1+ % Newton's method is one of the fastest algorithms to converge on a root.
2+ % It does not require you to provide any endpoints, but it does require for
3+ % you to provide the derivative of the function. It is faster than the secant
4+ % method, but is also not guaranteed to converge.
5+
6+ % INPUTS:
7+ % function handle f
8+ % function handle df for the derivative of f
9+ % initial x-value
10+ % maximum tolerated error
11+
12+ % OUTPUTS:
13+ % An approximated value for the root of f.
14+
15+ % Written by MatteoRaso
16+
17+ function y = newton(f , df , x , error )
18+ while abs(f(x )) > error
19+ x = x - f(x ) / df(x );
20+ disp(f(x ))
21+ endwhile
22+ A = [" The root is approximately located at " , num2str(x )];
23+ disp(A )
24+ y = x ;
25+ endfunction
Original file line number Diff line number Diff line change 1+ % Extremely similar to the false position method. The main difference is
2+ % that the secant method does not actually have a defined interval where
3+ % the root lies on. It converges faster than the false position method,
4+ % but it is not always guaranteed to converge.
5+
6+ % INPUTS:
7+ % Function handle f
8+ % x1 = a
9+ % x2 = b
10+ % maximum tolerated error
11+
12+ % OUTPUTS:
13+ % An approximated value for the root of f.
14+
15+ % Written by MatteoRaso
16+
17+ function y = secant(f , a , b , error )
18+ x = [a , b ];
19+ n = 2 ;
20+ while abs(f(x(n ))) > error
21+ x(n + 1 ) = - f(x(n )) * (x(n ) - x(n - 1 )) / (f(x(n )) - f(x(n - 1 ))) + x(n );
22+ n = n + 1 ;
23+ disp(f(x(n )))
24+ endwhile
25+ A = [" The root is approximately " , num2str(x(n ))];
26+ disp(A )
27+ y = x(n );
28+ endfunction
You can’t perform that action at this time.
0 commit comments