% For Week 1 Friday Q&A
% Demonstration of fzero, which will be used in HW1-Q3
%
% Announcements to the students:
%  1. Scorelator has been set up. Please emailbox.
%  2. Students who added the class after Wed (Jan 7)
%     may not have registered to Scorelator. Please
%     let TA know in class on Friday (Jan 9)
%
% Tasks:
%  1. Q&A about video lectures
%  2. Demonstration of fzero
%      a. Take Prof Kutz's example
%      b. Introduce function handle @
%         (There is also a supplemental video on @)
%      c. Introduce fzero
%      d. Introduce options
%
%  Demonstrate the codes line by line.

%%
clear all;close all;clc

%% Function handles

myFunc = @(myVar) 2*myVar + 5;



% p1([1 2])

p2 = @(x) x.^2 + 3.*x - 5; 

p2(5);


anything = @(v1,v2,v3) (v1+v2+v3)/3;


avg = anything(5,6,7)

xAxis = [-10:0.1:10];

Y = p2(xAxis);    

plot(Y)


%%
f = @(x) exp(x) - tan(x);

x = [-8:0.01:8]*(pi/2); %So, our interval is [-4*pi, 4*pi]

y = f(x);


figure(1)
plot(x,y)       % Discuss the crazily large numbers
% plot(x,f(x));
grid on
ylim([-10 10]) % Discuss the vertical lines is not real

%%
% Show the discontinuity

indices = abs(y)>100;
y(abs(y)>100)=nan; % Look at this a bit
%%
figure(2)
plot(x,y)    % Discuss the effect of NaNs
grid on
ylim([-10 10])

%%

% Repeat Prof. Kutz's choice of interval
x0 = fzero(f,[-4 -2.8])
format long    % Discuss the number of digits shown
x0

%%

% If a wrong interval is chosen
x0 = fzero(f,[-3 -2.8])

%%

% Show all iterations
% Note: Those iterations generated by fzero are not
%       required by Scorelator
x0 = fzero(f,[-4 -2.8],optimset('Display','iter'))

%%

% Specify the desired precision
x0 = fzero(f,[-4 -2.8],optimset('TolX',1e-4))

%%

% With one estimate
x0 = fzero(f,-4,optimset('Display','iter'))

%% Debugging Loops (if we have time)








