﻿function [p2,i] = DannyMullers(f,p0,p1,p2,TOL,N)
    i=1;
    p0 = 0;
    p1 = 1;
    p2 = 2;
    TOL=0.0001;
    N=50;
    f=@(x) exp(x) - 2
    
    
    while (i<=N)
        
        x1=p0
        x2=p1
        x3=p2
        y1=f(x1)
        y2=f(x2)
        y3=f(x3)
        
        % I could not for the life of me get the equations to work, did some searching on the internet and fuond another method to fit a porabola to 3 points using matrices
        A = [x1^2, x1, 1; x2^2, x2, 1; x3^2, x3, 1];
    
        % Solve the least squares problem
        coeffs = A \ [y1; y2; y3];
    
        % Grab them coefficients
        a = coeffs(1)
        b = coeffs(2)
        c = coeffs(3)
        
        %same idea as secants but with super fancier equation faturing 3 initial vars!

        %DDp1p2=(f(p1)-f(p2))./(p1-p2);
        %DDp1p0=(f(p1)-f(p0))./(p1-p0);
        %DDp2p0=(f(p2)-f(p0))./(p2-p0);
        
        %a = (DDp1p2-DDp2p0)./(p0-p1)
        %b = DDp1p2 - (p1-p2).*a
        %c = f(p2)
        
        %a = (((p1 - p2) * (f(p0) - f(p2)) - (p0 - p2) * (f(p1) - f(p2))) / ((p0 - p2) * (p1 - p2) * (p0 - p1)))
        %b = ((p0 - p2)^2 * ((f(p1) - f(p2)) - ((p1 - p2)^2) * (f(p0) - f(p2)))) / ((p0 - p2) * (p1 - p2) * (p0 - p2))
        %c=f(p2)
        
        
        %b = ((p0 - p2).^2 * ((f(p1) - f(p2)) - ((p1 - p2).^2) * (f(p0) - f(p2))) / ((p0 - p2) * (p1 - p2) * (p0 - p2))
        %a = (((p1 - p2) * (f(p0) - f(p2)) - (p0 - p2) * (f(p1) - f(p2))) / ((p0 - p2) * (p1 - p2) * (p0 - p1));
        
        
        if (b >= 0)
            r1 = (-b - sqrt(b*b - 4*a*c))/(2*a)
            r2 = (2*c)/(-b - sqrt(b*b - 4*a*c))
        
            if (abs(-b - sqrt(b*b - 4*a*c))>abs(-b + sqrt(b*b - 4*a*c)))
                p3=r2;
            else
                p3=r1;
            end
                
        else
            r1 = (-b + sqrt(b*b - 4*a*c))/(2*a)
            r2 = (2*c)/(-b + sqrt(b*b - 4*a*c))
        
            if (abs(-b + sqrt(b*b - 4*a*c))>abs(-b - sqrt(b*b - 4*a*c)))
                p3=r2;
            else
                p3=r1;
            end
        
        end
    
        p0=p1
        p1=p2
        p2=p3
        
        if (abs(p2 - p1)<TOL)
            disp (p3);
            return
        end%if
        %disp ("x iteration")
        %disp (i + 2)
        %disp ("x value")
        %disp(p2)
        i=i+1;
    end%while
    
    
    %x="Method failed---maximum number of iterations reached";
end%function
