function fixedpoint_demo(examples)
% Plot fixed-point examples 1--3, for example fixedpoint_demo(1:3).

% Define three example problems.
g = {@(x) cos(x), @(x) (x + 2 ./ x) / 2, @(x) 1.2 * x - 0.2};
pstart = [0.5, 1, 0.8];
xlimits = [0, 1; 0.5, 2; -0.5, 1.5];
names = {'cos(x)', '(x + 2/x) / 2', '1.2x - 0.2'};

% Plot the selected examples.
for example = examples
    xmin = xlimits(example, 1);
    xmax = xlimits(example, 2);
    x = linspace(xmin, xmax, 1000);

    figure
    plot(x, g{example}(x), x, x, 'k', 'LineWidth', 2)
    hold on

    % Compute and plot ten fixed-point iterations.
    p0 = pstart(example);
    for n = 1:10
        p = g{example}(p0);
        plot([p0, p0], [p0, p], 'r', 'LineWidth', 1.5)
        plot([p0, p], [p, p], 'r', 'LineWidth', 1.5)
        p0 = p;
    end

    title(names{example})
    axis equal
    axis([xmin, xmax, xmin, xmax])
    grid on
end
