echo on; clc;
%---------------------------------------------------------------------------
%A2_8   MATLAB script file for implementing Algorithm 2.8
%
% NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1995
% To accompany the text:
% NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
% Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
% Prentice Hall, Inc.; USA, Canada, Mexico ISBN 0-13-624990-6
% Prentice Hall, International Editions:   ISBN 0-13-625047-5
% This free software is compliments of the author.
% E-mail address:      in%"mathews@fullerton.edu"
%
% Algorithm 2.8 (Muller's Method).
% Section	2.5, Aitken's Process & Steffensen's & Muller's Methods, Page 97
%---------------------------------------------------------------------------

clc; clear all; format long;

% - - - - - - - - - - - - - - - - - - - - - - - - - - -
%
% This program implements Muller's method.
%
%
% Define and store the function f(x) in the M-file  f.m
%
% function y = f(x)
% y = x.^3 - 3.*x + 2;

pause % Press any key to continue.

clc;
%.......................................................................
% Begin a section which enters the function(s) necessary for the example
% into M-file(s) by executing the diary command in this script file.
% The preferred programming method is not to use these steps.
% One should enter the function(s) into the M-file(s) with an editor.
delete output
delete f.m
diary  f.m; disp('function y = f(x)');...
            disp('y = x.^3 - 3.*x + 2;');...
diary off;
% Remark. f.m and muller.m are used for Algorithm 2.8
f(0); % Test for file f.m
pause % Press any key to see the graph y = f(x).

clc;
% ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
% Prepare graphics arrays to plot f(x).
% ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
a = -2.5;
b = 2.5;
h = (b-a)/100;
X = a:h:b;
Y = f(X);

clc; figure(1); clf;

%~~~~~~~~~~~~~~~~~~~~~~~
% Begin graphics section
%~~~~~~~~~~~~~~~~~~~~~~~
a = -2.5;
b =  2.5;
c = -5;
d =  5;
whitebg('w');
plot([a b],[0 0],'b',[0 0],[c d],'b');
axis([a b c d]);
axis(axis);
hold on;
plot(X,Y,'-g');
xlabel('x');
ylabel('y');
title('Graph of y = f(x).');
grid;
hold off;

figure(gcf); pause % Press any key to perform Muller's iteration.

clc;

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
%
% Example 2.16, page 95  Use Muller's Method of iteration for finding 
% a zero of the function  f(x) = x^3 - 3x + 2.
%
% Enter the starting values in  p0  p1  and  p2
% Enter the abscissa tolerance in  delta
% Enter the ordinate tolerance in  epsilon
% Enter the number of iterations in  max1

p0  = -2.6;
p1  = -2.5;
p2  = -2.4;
delta = 1e-12;
epsilon = 1e-12;
max1 = 10;

[p2,y2,err,P] = muller('f',p0,p1,p2,delta,epsilon,max1);

pause % Press any key for the list of iterations.


clc;
% ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
% Prepare arrays to graph and print results.
% ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
a = -2.65;
b = -1.95;
h = (b-a)/100;
X = a:h:b;
Y = f(X);
max1 = length(P);
n0 = min(7,max1);
X0 = P(1:n0);
Z0 = zeros(1,n0);

clc; figure(2); clf;

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Begin graphics section for the results.
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a = -2.65;
b = -1.95;
c = -9;
d =  1;
whitebg('w');
plot([a b],[0 0],'b',[0 0],[c d],'b');
axis([a b c d]);
axis(axis);
hold on;
plot(X,Y,'-g',X0,Z0,'or');
xlabel('x');
ylabel('y');
title('Graphical analysis for Muller`s method.');
grid;
hold off;

figure(gcf); pause	% Press any key to continue.

% .. .. .. .. .. 
% Prepare results
% .. .. .. .. .. 
J = 1:max1;
Yp = f(P);
points = [J;P;Yp];

clc;
%............................................
% Begin section to print the results.
% Diary commands are included which write all
% the results to the Matlab textfile   output
%............................................
Mx1 = 'Iterations for Muller`s method.';
Mx2 = ['     k                  p(k)               f(p(k))'];
Mx3 = 'The solution is:';
Mx4 = 'The error estimate for p is  ± ';
clc,echo off, diary output,...
disp(''), disp(Mx1),disp(''), disp(Mx2), disp(points'),...
disp('Iteration converged quadratically to the root.'),...
disp(''),disp(Mx3),disp(''),disp('p = '),...
disp(p2),disp('f(p) = '),disp(y2),...
disp([Mx4,num2str(err)]),diary off,echo on
pause % Press any key to perform Muller's iteration.

clc;

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
%
% Example 2.16, page 95  Use Muller's Method of iteration for finding 
% a zero of the function  f(x) = x^3 - 3x + 2.
%
% Enter the starting values in  p0  p1  and  p2
% Enter the abscissa tolerance in  delta
% Enter the ordinate tolerance in  epsilon
% Enter the number of iterations in  max1

p0  = 1.4;
p1  = 1.3;
p2  = 1.2;
delta = 1e-12;
epsilon = 1e-12;
max1 = 10;

[p2,y2,err,P] = muller('f',p0,p1,p2,delta,epsilon,max1);

pause % Press any key for the list of iterations.

clc;
% ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
% Prepare arrays to graph and print results.
% ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
a = 0.975;
b = 1.425;
h = (b-a)/100;
X = a:h:b;
Y = f(X);
max1 = length(P);
n0 = min(7,max1);
X0 = P(1:n0);
Z0 = zeros(1,n0);

clc; figure(3); clf;

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Begin graphics section for the results.
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a =  0.975;
b =  1.425;
c = -0.05;
d =  0.55;
whitebg('w');
plot([a b],[0 0],'b',[0 0],[c d],'b');
axis([a b c d]);
axis(axis);
hold on;
plot(X,Y,'-g',X0,Z0,'or');
xlabel('x');
ylabel('y');
title('Graphical analysis for Muller`s method.');
grid;
hold off;

figure(gcf); pause % Press any key to continue.

% .. .. .. .. .. 
% Prepare results
% .. .. .. .. .. 
J = 1:max1;
Yp = f(P);
points = [J;P;Yp];

clc;
%............................................
% Begin section to print the results.
% Diary commands are included which write all
% the results to the Matlab textfile   output
%............................................
clc,echo off, diary on,...
disp(''), disp(Mx1),disp(''), disp(Mx2), disp(points'),...
disp('Iteration converged quadratically to the root.'),...
disp(''),disp(Mx3),disp(''),disp('p = '),...
disp(p2),disp('f(p) = '),disp(y2),...
disp([Mx4,num2str(err)]),diary off,echo on
