% Script 2 for Week 2
% In the script below we use a rough implementation of the bisection algorithm to find a root of G=@(x) exp(x)-tan(x) on the interval [-4,-2.8].
% This is the kind of thing you do when you are designing the pieces of a finished algorithm. Think of it like designing a machine. You get the wheels and axle working together, then the frame and body, then you put them together. Nothing complicated gets designed "all at once."
% Copy the whole block and past it into a command window and hit return. Think about it till you understand every part and its function.
%***************************************
clear % clear all variables
xr=-2.8; % initial right boundary
xl=-4; % initial left boundary
for j=1:100 % starts a loop: j counts the cuts
xc=(xl+xr)/2; % calculate midpoint
fc=exp(xc)-tan(xc); % calculates function
if fc>0 xl=xc; % move left boundary if fc pos
else xr=xc; % move right boundary if fc not pos
end % ends this "if"
if abs(fc)<10^(-5) break % quits the j-loop if this "if" condition met
end % ends this "if"
end % ends the j-loop
xc % print the value of the root.
fc % print the value of the function.
j % print number of steps
%***********************************
% Below we use the incremental search algorithm (provided) from the text. It locates intervals that contain sign changes of a function, so if the function is continuous at least one root is in each such interval. You would use this to localize one root to a small-ish interval and then use other methods to find it more precisely.
(Carefully examine this and also the next two function m-files as you use them.)
% narargin is the number of arguments input by the user. This script assigns default values to important parameters if the user doesn't want to specify them.
% This script can miss zeros w/o sign change and catch discontinuities with sign change
G=@(x) exp(x)-tan(x)
incsearch(G,-20,-1)
%***********************************
% Below we use the bisection algorithm function from the text.
% varargin is a cell array (note curly brackets) that contains additional parameters that may be required. In this case these additional parameters are values that might be required by the function "func." Usually there are none and varargin is empty.
bisect(G,-4,-2.4)
%****************************************
% The false position algorithm has identical form with bisect with just a single line replaced:
% xr = (xl + xu)/2;
% is replaced by
% xr=xu-func(xu,varargin{:})*(xl-xu)/( func(xl,varargin{:})-func(xu,varargin{:} ) );
% In false position, xr is the x-axis intercept of the straight line connecting
% the point (xl, func(xl,varargin{:}) ) to (xu, func(xu,varargin{:}) ).
% Below we use a Newton-Raphson algorithm implementation.
% It is essentially Newton's method with tweaks.
%****************************************
H=@(x) exp(x)-tan(x);
dH=@(x) exp(x)-(sec(x)).^2;
[root,ea,iter]=newtraph(H,dH,1)
% fzero and optimset are highly optimized and flexible built-in root finding tools. The format is:
% [x,fx]=fzero(function, x0,options,p1,p2,...) or
% [x,fx]=fzero(function, x0,[],p1,p2,...)
options=optimset('display','iter');
[x,fx]=fzero(@(x) x^10-1,0.5, options)
G=@(x) exp(x)-tan(x)
[x,fx]=fzero(G,-3, options)
fzero(G,-3)
[x,fx]=fzero(G,-3)
format long
x
format short
% playing with polynomials
x=roots([1,0,-4])
c=poly(x)
flip(c)
polyval(c,0)
t=-1:.1:1;
y=polyval(c,t);
plot(t,y)
conv([1,-1],[1,1])
deconv([1,0,-4],[1,-2])
[a,b]=deconv([1,0,-4],[1,1])
% Below we calculate quotient and remainder: (x^3-4*x^2-x+6)/(x^2-x-1)
[a,b]=deconv([1,-4, -1, 6],[1,-1,-1])
b+conv([1,-1,-1],a)
% So .... multiplying and doing polynomial long division can be (should be) done by MATLAB.