% Class 11
% Coded by Nigel Forest Reuel on 2.19.2018
% This class covers OPEN methods
clc
close all
%
% Warm up problem
F1 = @(x) sin(x).*cos(x)./(x.^2);
% Graphically
fplot(F1,[1.2 2])
% I zoomed in and found Root at x = 1.5708
% Incremental Search
Root_IS = incsearch(F1,0.1,2,10000)
% Bisection
[Root_B,~,~,iter]=bisect(F1,0.1,2,0.0000001)
disp('Bisect is the best method here.')
disp('It is automated and took only 31 function iterations to converge.')
%
% Example of Fixed Point Iteration
% f(x) = sin(sqrt(x))-x;
hfunc = @(x) sin(sqrt(x)); % We have added x
[root, ~, ea] = FPI(hfunc,0.5,0.01)
% Example of Newton Raphson Method (6.9 book)
F1 = @(x) -2 + 6*x - 4*x^2 +0.5*x^3;
D1 = @(x) 6 - 8*x + 1.5*x^2;
[root,~,~]=newtraph(F1,D1,4.5)
[root,~,~]=newtraph(F1,D1,4.43)
fplot(F1,[0 8])
% Example of fzero, this is matlab's built in
% root finding algorithm (adv. Brent method)
%
fzero(F1,4.5)
% Using the roots function
% This is for polynomials
% Specify the coefficients of the polynomial in descending order
C = [0.5 -4 6 -2];
roots(C)
% Poly - you can go from the roots --> polynomial coefficients
% Last example Problem 6.14
K = 0.05;
pt = 3;
E1 = @(x) x/(1-x)*sqrt(2*pt/(2+x))-K;
xsol = fzero(E1,0.2)