1
6.20 Write a MATLAB user-defined function that determines the best fit of an exponential function of the
form y = be mx to a given set of data points. Name the function [b m]= ExpoFit(x,y), where the
input arguments x and y are vectors with the coordinates of the data points, and the output arguments b
and m are the values of the coefficients. The function ExpoFit should use the approach that is described
in Section 6.3 for determining the value of the coefficients. Use the function to solve Problem 6.8.
Solution
The listing of the user-defined function ExpoFit is:
function [b,m] = ExpoFit(x, y)
% ExpoFit calculates the coefficients b and m of the exponential
% equation y = b*exp(m*x) that best fits n data points.
% Input variables:
% x A vector with the coordinates x of the data points.
% y A vector with the coordinates y of the data points.
% Output variables:
% b The coefficient b.
% m The coefficient m.
nx = length(x);
ny = length(y);
if nx ~= ny
disp('ERROR: The number of elements in x must be the same as in y.')
b = 'Error';
m = 'Error';
else
Y=log(y); X=x;
SX = sum(X);
SY = sum(Y);
SXY = sum(X.*Y);
SXX = sum(X.^2);
a1 = (nx*SXY - SX*SY)/(nx*SXX - SX^2);
a0 = (SXX*SY - SXY*SX)/(nx*SXX - SX^2);
b=exp(a0);
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
2
m=a1;
end
The user-defined function ExpoFit is used to solve Problem 6-8 in the following script file:
TC=[-40 -20 0 20 40];
S=[0.0012 0.002 0.0032 0.006 0.0118];
[b,m] = ExpoFit(TC, S)
% Estimated solubility at 10C
S10=b*exp(m*10)
%Plot;
Tp=-40:40;
Sp=b*exp(m*Tp);
plot(Tp,Sp,TC,S,'*')
xlabel('Temp (C)')
ylabel('Water Solubility (% wt.)')
When the script is executed the following answers are displayed in the Command Window, and the following figure
is displayed in the Figure Window.
b =
0.0035
m =
0.0284
S10 =
0.0047
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
3
0.012
0.01
Water Solubility (% wt.)
0.008
0.006
0.004
0.002
0
-40 -30 -20 -10 0 10 20 30 40
Temp (C)
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.