clear, clc, close all
%%
%Bisection Method
%b
x = linspace(1, 4, 100);

function_sinh = sinh_function(x);
figure;
plot(x, function_sinh)
hold on
function_sinh = sinh_function(1)
function_sinh = sinh_function(4)

%% d 
tol = 1e-6;
imax = 50;
a = 1;
b = 4;
[root,counter] = bisect2( @sinh_function, a, b, imax, tol);

plot(x,function_sinh,root,0,'o')
hold off


title({['Root: ', num2str(root),'  Number of interations: ', num2str(counter),'  Function : sin(x).*sinh(x)']})
    subtitle(['Upper Limit: ', num2str(b),' lower Limit: ', num2str(a) ' Tol: ', num2str(tol)]); 


%% c
x = linspace(4, 4.7, 100);
function_R = Function_R(x);


%% d
a = 4;
b = 4.7;

[root,counter] = bisect2( @Function_R, a, b, imax, tol);

figure;

plot(x,function_R,root,0,'o')
title({['Root: ', num2str(root),'  Number of interations: ', num2str(counter),'  Function : (sqrt(R.^2 - x.^2)) - x.*tan(x)']})
    subtitle(['Upper Limit: ', num2str(b),' lower Limit: ', num2str(a) ' Tol: ', num2str(tol)]); 


%% Newtons Method 
% first function 
syms x 
Derv1_sym = diff(exp(x) - (1 + x + ((x^2)/2)));
x_guess = 5;
[x_next,counter] = Newton(@Function1,@Derv1, imax, tol, x_guess );
figure;
x1 = linspace(-1, 1, 100);
function1 = Function1(x1);
% plot graph
plot(x1,function1,x_next,0,'o')
title({['Root: ', num2str(x_next),'  Number of interations: ', num2str(counter),'  Function : (exp(x) - (1 + x + ((x^2)/2)))']})
    subtitle([ num2str(a) ' Tol: ', num2str(tol)]); 

% Second Function 
syms x 
Derv2_syms = diff(10*log(x) - x) ;
x_guess = 1;
[x_next,counter] = Newton(@function2,@derv2, imax, tol, x_guess );

% plot graph
figure;
x1 = linspace(-1.5, 1.5, 100);
function2 = function2(x1);
plot(x1,function2,x_next,0,'o')
title({['Root: ', num2str(x_next),'  Number of interations: ', num2str(counter),'  Function : (10*log(x) - x)']})
    subtitle([ num2str(a) ' Tol: ', num2str(tol)]); 
%% Newtons Method- Intersection of two functions 

syms x 
derv4_syms = diff(100*exp(-x) -5 * sin((pi*x)/2));

 x_guess = 4;
 imax = 20;
 tol = 1e-6;

 [x_next,counter] = Newton(@Function_4,@derv4, imax, tol, x_guess );

 % plot graph
figure;
x1 = linspace(3, 10, 100);
function4 = Function_4(x1);
plot(x1,function4,x_next,0,'o')
title({['Root: ', num2str(x_next),'  Number of interations: ', num2str(counter)]})
    subtitle([ num2str(a) ' Tol: ', num2str(tol),'  Function : (100.*exp(-x_guess) - 5.*sin((pi.*x_guess)/2))']); 

%% Newtons Method - Intersection of two functions at 1e-10

 tol = 1e-10;

 [x_next,counter] = Newton(@Function_4,@derv4, imax, tol, x_guess );

 % plot graph
figure;
x1 = linspace(3, 10, 100);
function4 = Function_4(x1);

syms x 
function5 = Function_5(x1);
function6 = Function_6(x1);

plot(x1,function5)
hold on 
plot(x1,function6)
plot(x1,function4,x_next,0,'o')
hold off
title({['Root: ', num2str(x_next),'  Number of interations: ', num2str(counter)]})
    subtitle([ num2str(a) ' Tol: ', num2str(tol),'  Function : (100.*exp(-x_guess) - 5.*sin((pi.*x_guess)/2))']); 
%% 4.	Bracketing (Multiple Roots)  


%% B Bisection method 
f = @(x) 100 * exp(-x) - 5 * sin((pi * x) / 2); % Define the function
a = 4; % Start of interval
b = 10; % End of interval
tol = 1e-6; % Tolerance for root finding

% Find all roots in the interval
roots = bisectionAllRoots(f, a, b, tol)

% Display the results
disp('Roots found bisection:');
disp(roots);

%% Newtons Method 
f = @(x) 100*exp(-x) - 5*sin((pi*x)/2);
roots = newtonsAllRoots(f, tol, imax)

% Display the results
disp('Roots found for newtons:');
disp(roots);



%% 4.	Bracketing (Multiple Roots)  
f = @(x) 100 * exp(-x) - 5 * sin((pi * x) / 2); % Define the function
d = @(x) -100 * exp(-x) - (5 * pi / 2) * cos((pi * x) / 2); % Define the derivative
imax = 100; % Maximum iterations
tol = 1e-10; % Tolerance
interval = [4, 10]; % Search interval
num_subintervals = 100; % Number of subintervals for bracketing
find_all_roots(f, d, a, b, imax, tol);
disp('Roots found for bracketing:');

plot(x1,function5)
hold on 
plot(x1,function6)
plot(x1,function4,root1,0,'o',root2,0,'o',root3,0,'o',root4,0,'o')
hold off
title({['Root: ', num2str(x_next),'  Number of interations: ', num2str(counter)]})
    subtitle([ num2str(a) ' Tol: ', num2str(tol),'  Function : (100.*exp(-x_guess) - 5.*sin((pi.*x_guess)/2))']); 



