Geoscience Reference
In-Depth Information
We can now use the resulting coei cients p(1) and p(2) to calculate the
function values fittedcurve using the model and compare the results with
the original data in a graphics.
fittedcurve_1 = p(1)*exp(data(:,1)) + p(2);
plot(data(:,1),data(:,2),'o')
hold on
plot(data(:,1),fittedcurve_1,'r')
xlabel('x-Axis'), ylabel('y-Axis')
title('Unweighted Fit')
hold off
As we can see from the output of p and the graphics, the i tted red curve
describes the data reasonably well. We can now also use nlinfit to perform
a weighted regression. Let us assume that we know the one-sigma errors of
the values in data(:,2) . We can generate synthetic errors and store them in
the third column of data .
data(:,3) = abs(randn(size(data(:,1))));
errorbar(data(:,1),data(:,2),data(:,3),'o')
xlabel('x-Axis'), ylabel('y-Axis')
We can now normalize the data points so that they are weighted by the inverse
of the relative errors. We therefore normalize data(:,3) so that the total of
all errors in data(:,3) is one, and store the normalized errors in data(:,4) .
data(:,4) = sum(data(:,3))./data(:,3);
To make a weighted i t, we dei ne the model function model , and then use
nlinfit with the parameter Weights .
model = @(phi,t)(data(:,4).*(phi(1)*exp(t) + phi(2)));
p0 = [0 0];
p = nlinfit(data(:,1),data(:,5),model,p0,'Weights',data(:,4))
p =
0.2191 2.3442
As before, nlinfit will compute weighted parameter estimates p . We again
use the resulting coei cents p(1) and p(2) to calculate the function values
fittedcurve using the model and compare the results with the original data.
fittedcurve_2 = p(1)*exp(data(:,1)) + p(2);
errorbar(data(:,1),data(:,2),data(:,3),'o')
hold on
plot(data(:,1),fittedcurve_2,'r')
xlabel('x-Axis'), ylabel('y-Axis')
title('Weighted Fit')
hold off
Search WWH ::




Custom Search