%% Assignment 4 - DRIVER SECTION
% This is the part of the code from which the tests are called

% UNCOMMENT THE CALLS TO TEST FUNCTIONS AS YOU COMPLETE THEM
function Assignment4()        
        %%
        matlab_bug_workaround()
        %% Series sum test
        %partial_sum_test_1()
        %partial_sum_test_2()
        %% Sequences of iterates: a graphic approach
        %iterates_test_1()
        %iterates_test_2()
        %iterates_test_3()
        %iterates_test_4()
        %iterates_test_5()
        %iterates_test_6()
        %% Extra credit
        %extra_credit_test()
end    



%% Functions section
% This sections contains all the functions that you need to write

%%%DO NOT PUT CODE OUTSDIDE OF FUNCTIONS%%%%%%%%
%%%%%COMPLETE THE FUNCTIONS BELOW%%%%%%%%%%%%%%%
function [v]=dummy(a)
%dummy function helps to work around a bug in MATLAB publishing code
v = 2 + 2;
end

%%A function that computes the N'th partial sum of the series
% sum_{k = k0}^infinity f(k) -
%(adds up the first N terms starting from k0'th)
%INPUT: f - a function of one variable
%       k0, N - non-negative integers
%OUTPUT: P_N, the N'th partial sum of \sum_{n=1}^infinity f(n)
function [Pn] = partial_sum(f, k0, N)

end


%%Draws a segment from (x0, y0) to (x1, y1) on the current plot
%The segment is plotted as *black* and *dashed*.
%HINT: see http://www.mathworks.com/help/matlab/ref/plot.html
function seg(x0, y0, x1, y1)
    
end

%%Plots N iterations of f on x0
%(Draws the connecting segments to the line y=x)
%INPUT: f - a function of one variable
%       x0 - a starting point
%       N - iteration count
%OUTPUT: x = f(f(f(f(...f(x0)))..))   (N times)
%        draws the plot of iterations: the first step
%        connects (x0, 0) to (x0, f(x0))
%           and   (x0, f(x0)) to (f(x0), f(x0)).
%     Each next step connects the last point to the line y=x
%     with a horizontal segment, and then to the graph with a vertical one.
%NOTE:   use your seg function to draw segments in a consistent style.
function [x] = plot_iterates(f, x0, N)    
%       >YOUR CODE HERE
end

%Plots the function, the line y=x, and N iterations of f on x0
%INPUT: f - a function of one variable
%       window - an array with 4 elements: [xmin xmax ymin ymax]
%                that specifies the plot window
%       x0 - initial value
%       N  - number of iterations to do
%OUTPUT: -creates a new figure with the plot with the following properties:
%       - the plot is from window(1) to window(2) on the x axis
%       - the plot is from window(3) to window(4) on the y axis
%       - the function is labeled (the legend displays the function)
%       - the title says 'N'th iterate', where N is replaced with the 
%         value of the variable
%HINT: refer to the help for legend, title, func2str, axis commands
function plot_func_and_iterates(f, window, x0, N)    
%> YOUR CODE HERE    
end

%% Extra credit functions


%%Finds an element in L  to which a is eps-close, returns 0 if none found
%INPUT:L - an array of numbers
%      a - a number
%      eps - a small number
%OUTPUT: an integer pos such that L(pos) is with eps of a, 0 if none found
function [pos] = search_approx(L, a, eps)

end

%%Function does N steps of the Newton's method starting from x0
%INPUT f - a function
%      x0 - a number, initial guess
%      N - number of iterations to make
%OUTPUT: f(f(f(...(f(x0))))...)))    [N times]
function root = newton(f, x0, N)
    
end

%%Draws the Newton fractal: colors points in the region of the complex
%plane specified by the bounds according to which root Newton iterations
%converge to.
%INPUT: f - a smooth function of one (complex) variable
%       bounds - an array of 4 numbers
%       N = number of steps to make with Newton's method
function [] = draw_newton_fractal(f, bounds, N)
   roots = []; %to store the list of found roots
   %>YOUR CODE HERE, IF YOU NEED IT
   %set up M, an N x N matrix
    for r = 1:N
       for c = 1:N
         	%set z = complex number corresponding to row r, col c in M
            %run newton's method on it to get a root, r
            %look up r in the list of roots
            %if not found, append r to the end of the list of found roots.
            % set M(r,c) = index of the root within 0.1 of r
            %NOTE: use a sufficiently high value for number of steps
            %>YOUR CODE HERE
       end
    end
    imagesc(M);
    %roots'  %<-uncomment this to see which roots you find. Too many -->
    %%make more Newton steps.
end


%% Tests section
% This sections contains code that calls your functions to verify that they
% work as intended

function matlab_bug_workaround()
    %This is a workaround to bug #496201.     
    dummy(1);
end

    function partial_sum_test_1()       
        disp('Sum of 1./2.^n from n = 0 to n = 20 is approximately 2:');
        S = partial_sum(@(n) 1./(2.^n), 0, 20);
        fprintf('S = %f \n',S);
    end
    
    %Write a test that computes sqrt(6S), where S is the sum of the series
    %\sum 1/k^2, k=1..infinity. (Play with the number of terms you need
    %to add up before you think you are close to the answer)
    function partial_sum_test_2()
        disp('Exploring sum of inverses of squares');
        %>YOUR CODE HERE
    end

    %This test looks at 20 iterates of x^2 starting from x0=0.8
    function iterates_test_1()
        %The sequence of iterates approaches 0
        plot_func_and_iterates( @(x) x.^2, [0 1 0 1], 0.8, 20);         
    end

    %This test looks at 20 iterates of 2x^2 starting from x0=0.51
    %The plot window is 0..1 on the X axis, 0..1 on Y axis
    function iterates_test_2()    
        %The sequence of iterates approaches ...
        %>YOUR CODE
    end
    
    %This test looks at 20 iterates of 1-x^2 starting from x0=0.65
    %The plot window is 0..1 on the X axis, 0..1 on Y axis
    function iterates_test_3()    
        %The sequence of iterates approaches ...
        %>YOUR CODE        
    end

    %This test looks at 20 iterates of 1+1/x starting from x0=0.8
    %The plot window is 0..5 on the X axis, 0..5 on Y axis
    function iterates_test_4()    
        %The sequence of iterates approaches ...
        %%>YOUR CODE
    end
    
    %This test looks at 20 iterates of (x + 2./x)/2 starting from x0=0.8
    %The plot window is 0..5 on the X axis, 0..5 on Y axis
    function iterates_test_5()    
        %The sequence of iterates approaches ...
        %This could be used to compute ...
        %>YOUR CODE
     end
     
    %This test looks at 20 iterates of x + sin(x) starting from x0=0.8
    %The plot window is 0..4 on the X axis, 0..4 on Y axis
    function iterates_test_6()    
        %The sequence of iterates approaches ...
        %>YOUR CODE
    end



    
  