% Unit 3 (MATLAB Course @ UAM)
% (c) Alexander Knebe

%========================================================
% root of effective gravitational potential by bisection
% (using an anonymous function and then fzero())
%========================================================
clear all
close all
clc
format long

% the potential
Phi = @(r)(2./r.^2-5./r);

% start with simply plotting the potential
r   = linspace(0.2,2,1000);

plot(r,Phi(r),r,0), xlabel('r'), ylabel('Phi'), title('effective gravitational potential')

%-----------------------------
% root finding via bi-section
%-----------------------------
% initial guess for the root
a = 0.2;
b = 2.0;
n = 0;    % we also count the number of iterations

% and find the root using fzero()
r0 = fzero(Phi, [a b])

% mark root
hold on
plot([r0 r0],[min(Phi(r)) 0],'r')
