echo off;
% NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
% To accompany the text:
% NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
% Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
% This free software is complements of the author.

% Algorithm 5.3 (Non-linear Curve Fitting).
% Section 5.2, Curve Fitting, Page 280
echo on; clc; format short; hold off; clear

% This program performs curve fitting, by using

% the method of data linearization. Given a set of

% data points { (x , y ), (x , y ) ,..., (x , y ) }.
% 1 1 2 2 n n

% The abscissas and ordinates are stored in X and Y, respectively.

% X = [x , x ,..., x ]; Y = [y , y ,..., y ];
% 1 2 n 1 2 n

% Remark. crvfit.m crvfun.m crvnam.m are used for Algorithm 5.3

pause % Press any key to continue.

clc;
% Place the abscissas for the points in X.

% Place the ordinates for the points in Y.

X = [0.0 1.0 2.0 3.0 4.0];

Y = [1.5 2.5 3.5 5.0 7.5];

pause % Press any key to graph data points.

clc; clg;
a = min(X)-0.2; b = max(X)+0.2;
c = min(Y)-1.0; d = max(Y)+1.0;
axis([a b c d]);...
plot(X,Y,'or');...
hold on;...
plot([a b],[0 0],'b',[0 0],[c d],'b');...
xlabel('x');...
ylabel('y');...
title('The given x-y data points.');...
grid;...
axis;...
hold off;...
shg; pause % Press any key to continue.

points = [X;Y];
clc,disp('The given x-y points:'),...
disp(' x y'),disp(points')
pause % Press any key for the menu of curves.

clc;
Mx1=' Available curves are:';
Mx2='(1) y = A/x + B (2) y = D/(x + C) (3) y = 1/(Ax + B)';
Mx3='(4) y = x/(A + Bx) (5) y = A ln(X) + B (6) y = C exp(Ax)';
Mx4='(7) y = C x^A (8) y = 1/(Ax + B)^2';
Mx5='(9) y = C x exp(-Dx) (10) y = L/(1 + C exp(Ax))';
clc,disp(''),disp(Mx1),disp(''),disp(Mx2),disp(''),...
disp(Mx3),disp(''),disp(Mx4),disp(''),disp(Mx5),...
ct = input(' Enter the curve type <1-10> ');

pause % Press any key to fit the curve.

clg; clc;
if length(ct)==0, ct=2; end
Clist = crvfit(X,Y,ct);
h = (b-a)/200;
Xs = a:h:b;
Ys = crvfun(Clist,ct,Xs);
axis([a b c d]);...
plot(X,Y,'or',Xs,Ys,'-g');...
hold on;...
plot([a b],[0 0],'b',[0 0],[c d],'b');...
xlabel('x');...
ylabel('y');...
title(crvnam(Clist,ct)); end;...
grid;...
axis;...
hold off;...
shg; pause % Press any key to continue.

Mx1 = crvnam(Clist,ct);
Mx2 = 'The given x-y points:';
clc,echo off,diary output,...
disp(''),disp(Mx1),disp(''),disp(Mx2),...
disp(' x y'),disp(points'),diary off,echo on
pause % Press any key to continue.
points = [X;Y;crvfun(Clist,ct,X);Y-Crvfun(Clist,ct,X)]';
Mx3=' x(k) y(k) f(x(k)) error';
clc,echo off,diary output,disp(''),...
disp([' ',Mx1]),disp(Mx3),disp(points),diary off, echo on