---
# System prepended metadata

title: 'Lab 1: Secant method'

---

# Lab 1: Secant method

In numerical analysis, the secant method is a root-finding algorithm that uses a succession of roots of secant lines to better approximate a root of a function f. The secant method can be thought of as a finite-difference approximation of Newton's method. However, the method was developed independently of Newton's method and predates it by over 3000 years.

## The method

![](https://i.imgur.com/EoRGGHz.png)

As can be seen from the recurrence relation, the secant method requires two initial values, $x_0$ and $x_1$, which should ideally be chosen to lie close to the root.

## Derivation of the method

![](https://i.imgur.com/zZDnCcx.png)

![](https://i.imgur.com/H1Zao1f.png)

![](https://i.imgur.com/XddBsSs.png)

## Visualization

![](https://i.imgur.com/iAyw5i3.png)

The first two iterations of the secant method. The red curve shows the function f, and the blue lines are the secants. For this particular case, the secant method will not converge to the visible root.

## Code

```scilab
function [result]=y_true()
    result = [-3.0771032, 2.8952]
endfunction

function [result]=y_true_builded
    x=poly([-98 2 11], 'x', "coeff")
    result = roots(x)
endfunction

function [result]=QE(x)
    result = 11 * x ^ 2 + 2 * x - 98;
endfunction

function [result]=QE_der2(x)
    result = 22
end

function [result]=secant(x0, x1, eps, max_iter)
    if QE(x0) * QE(x1) < 0 then
        if QE(x0) * QE_der2(x0) > 0 then
            x_stopped = x0;
            x_runner = x1;
        end
    if QE(x1) * QE_der2(x1) > 0 then
        x_stopped = x1;
        x_runner = x0;
    end
    for i = 1:max_iter
        old = x_runner;
        x_runner = x_runner - (QE(x_runner) / (QE(x_runner) - QE(x_stopped))) * (x_runner - x_stopped);
        if abs(old - x_runner) < eps then
            break;
        end
    end
        result = x_runner
    else
        result = "Root is not in this range";
    end
endfunction

function [result]=secant_iter(epses)
    for i = 1:length(epses)
        result(i) = secant(-10, 1, epses(i), 200);
    end
endfunction

```



