function fixedpoint_main(k) if nargin ~= 1 fprintf(['fixedpoint_main must have a single input, k = 1, 2, or 3\n']); return end % function definitions f=@(x)[2*cos(x) - exp(x)]; % the function df=@(x)[-2*sin(x) - exp(x)]; % derivative of the function phi=@(x,d)[x - d*f(x)./df(x)]; % the fixed point iteration function ... % (modified newton's method) % phi=@(x,d)[x - d*(x.^2-5)]; % parameter inputs d = 1; % damping parameter itpoints = 10; % number of fixed point iterates x0 = -3; % initial starting point xmin = -5; % minimum x (for plotting) xmax = 6; % maximum x (for plotting) ymin = -8; % minimun y (for plotting) ymax = 8; % maximum y (for plotting) pts = 200; % number of points for plotting phi(x, ...) if k == 1 % graph just phi(x, ...) xx = linspace(xmin, xmax, pts); yy = phi(xx, d); % plot(xx, yy, xx, zeros(length(xx))); axis([xmin, xmax, ymin, ymax]); % adds the line y = 0 to the graph % plot(xx, yy, xx, xx); axis([xmin, xmax, ymin, ymax]); % adds the line y = x to the graph plot(xx, yy); axis([xmin, xmax, ymin, ymax]); axis([xmin, xmax, ymin, ymax]); elseif k == 2 % graph just coordinate outputs of fixedpoint() [x, y] = fixedpoint(phi, x0, itpoints, d); plot(x, y, x, y, '.', 'markersize', 7); axis([xmin, xmax, ymin, ymax]); elseif k == 3 % graph phi(x, ...) plus coordinate outputs of fixedpoint() [x, y] = fixedpoint(phi, x0, itpoints, d); xx = linspace(xmin, xmax, pts); yy = phi(xx, d); % plot(xx, yy, xx, zeros(length(xx)), xx, xx, x, y, x, y, '+'); plot(xx, yy, xx, zeros(length(xx)), xx, xx, x, y, x, y, '.', 'markersize', 7); % adds the line y = 0 to the graph % plot(xx, yy, xx, xx, x, y, x, y, '.', 'markersize', 7); axis([xmin, xmax, ymin, ymax]); else fprintf(['k = 1, 2, or 3 are the only allowed inputs to fixedpoint_main\n']); end end