hbar2 = 0.1;
m = 1;
V = 10;
a = 0.3;

alpha = @(E) sqrt(2*m*E/hbar2);
beta  = @(E) sqrt(2*m*(V-E)/hbar2);

alpha_p = @(E) sqrt(2*m/hbar2)*(1/2*1./sqrt(E));
beta_p  = @(E) sqrt(2*m/hbar2)*(1/2*1./sqrt(V-E));

f_even = @(E) beta(E).*cos(alpha(E)*a) - ...
    alpha(E).*sin(alpha(E)*a);
g_even  = @(E) alpha(E).*tan(alpha(E)*a) - beta(E);

f_odd = @(E) alpha(E).*cos(alpha(E)*a) + ...
    beta(E).*sin(alpha(E)*a);
g_odd  = @(E) alpha(E).*cot(alpha(E)*a) + beta(E);

f_even_p =@(E) beta_p(E).*cos(alpha(E)*a) ...
    -beta(E).*sin(alpha(E)*a).*alpha_p(E) ...
    -alpha_p(E).*sin(alpha(E)*a) ...
    -alpha(E).*cos(alpha(E)*a).*alpha_p(E);

f_odd_p =@(E) alpha_p(E).*cos(alpha(E)*a) ...
    -alpha(E).*sin(alpha(E)*a).*alpha_p(E) ...
    +beta_p(E).*sin(alpha(E)*a) ...
    +beta(E).*cos(alpha(E)*a).*alpha_p(E);

x_array = linspace(-1,10,100); % demonstrates the pecular behaviour of f_odd near 0
f_even_array = f_even(x_array);
f_odd_array = f_odd(x_array);
g_even_array = g_even(x_array);
g_odd_array = g_odd(x_array);

%
% Plotting
%

figure;
sub1=subplot(2,1,1);          % add first plot in 2 x 1 grid
hold on;                      % tell matlab to draw over previous graphs 
plot(x_array,f_even_array);
plot(x_array,f_odd_array);
hold off;                     % done plotting things in this subplot
legend('f_{even}','f_{odd}'); % add a legend
title('f_{even}, f_{odd}');

sub2=subplot(2,1,2);          % add second plot in 2 x 1 grid
hold on;
plot(x_array,g_even_array);  
plot(x_array,g_odd_array);
hold off;
ylim([-20 20]);                % set y-limits
legend('g_{even}','g_{odd}');
title('g_{even}, g_{odd}');


%
% Finding Roots
%

first  = newton(f_even, f_even_p, 1);
second = newton(f_odd, f_odd_p, 3);
third  = newton(f_even, f_even_p, 6);

%
% finding roots numerically
%

first_num  = newton_num(f_even, 1);
second_num = newton_num(f_odd, 3);
third_num  = newton_num(f_even, 6);

%
% difference in performace between analytic and numeric solvers
%

(first - first_num)/first,
(second - second_num)/second,
(third - third_num)/third,
