function xarr = newtonraphson(f,xarr1,n)
%% Newton-Raphson Solver Example
% Estimates solutions to algebraic equations
% Please predefine x as symbolic by typing
% syms x
% INPUTS:
% ===========
% f symbolic function
% xarr1 initial guess of x (a number)
% n number of iterations
% OUTPUTS:
% ===========
% xarr solutions to the algebraic equation

format long
%% Solve Symbolic Equation
syms x
df = diff(f,x); % derivative

xarr = zeros(n,1);
xarr(1) = xarr1; % initial guess
for i=1:n,
    term1 = subs(f,x,xarr(i));
    term2 = subs(df,x,xarr(i));
    xarr(i+1) = xarr(i) - term1./term2;
end

%% Example 
% % Solve square root of 2
% % f(x) = x^2 - 2
% % fdot(x) = 2*x
% n = 25; % number of iterations
% x = zeros(n,1);
% x(1) = 0.5; % initial guess
% for i=1:n
%     x(i+1) = x(i) - (x(i).^2 - 2)./(2.*x(i));
% end
% x
