KatyB
KatyB

Reputation: 3980

draw a line of best fit through data with shaded region for error

I have the following data:

dat = [9.3,0.6,0.4,0.7;...
    3.2,1.2,0.7,1.9;...
    3.9,1.8,0.7,1.9;...
    1.0,7.4,5.6,10.7;...
    4.7,1.0,0.5,1.3;...
    2.2,2.6,1.2,2.7;...
    7.2,1.0,0.5,1.1;...
    1.0,4.8,7.5,10.3;...
    2.7,1.8,1.7,4.0;...
    8.2,0.8,0.4,0.9;...
    1.0,4.9,5.7,8.2;...
    12.9,1.3,0.6,1.6;...
    7.7,0.8,0.5,1.3;...
    5.8,0.9,0.6,1.9;...
    1.1,4.5,6.2,12.1;...
    1.1,4.5,2.8,4.8;...
    16.4,0.3,0.3,0.5;...
    10.4,0.6,0.3,0.7;...
    2.2,3.1,2.2,4.6];

where the first column shows the observed values the second column shows the modeled values and the third and fourth columns show the min and max respectively.

I can plot the relationship between observed and modeled by

scatter(d(:,1),d(:,2))

Next, I would like to draw a smooth line through these points to show the relationship. How can this be done? Obviously a simple straight line would not be much use here.

Secondly, I would like to use the min and max (3rd and 4th columns respectively) to draw a shaded region around the modeled values in order to show the associated error.

I would eventually like to have something that looks like

http://www.giss.nasa.gov/research/briefs/rosenzweig_03/figure2.gif

Upvotes: 0

Views: 322

Answers (1)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38032

Something like this?

%// Rename and sort your data
[x,I] = sort(dat(:,1));
y     = dat(I,2);
mins  = dat(I,3);
maxs  = dat(I,4);

%// Assume a model of the form y = A + B/x. Solve for A and B 
%// using least squares
P = [ones(size(x))  1./x] \ y;

%// Initialize figure
figure(1), clf, hold on
set(1,  'Renderer', 'OpenGl');

%// Plot "shaded" areas
fill([x; flipud(x)], [mins; flipud(maxs)], 'y',...
    'FaceAlpha', 0.2,...
    'EdgeColor', 'r');

%// Plot data and fit
legendEntry(1) = plot(x, P(1) + P(2)./x, 'b',...
    'LineWidth', 2);
legendEntry(2) = plot(dat(:,1), dat(:,2), 'r.',...
    'Markersize', 15);

Results

Upvotes: 1

Related Questions