changeset 440:0ef8965dd745

Improve inteface and functionality of plotConvergence()
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 13 Mar 2017 14:40:14 +0100
parents 2663be328d9a
children e81325da6f8f 12db86a8ec7b
files plotConvergenceFit.m
diffstat 1 files changed, 39 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/plotConvergenceFit.m	Mon Mar 13 14:34:56 2017 +0100
+++ b/plotConvergenceFit.m	Mon Mar 13 14:40:14 2017 +0100
@@ -1,16 +1,45 @@
-% Draws a line in a loglog plot with slope `slope` and position `pos`
-% and `width` orders of magnitude wide.
-%    ex: pos = [1e-1 1e-4]
+% Draws a line in a loglog plot with slope `slope` fitted to the data in `x`
+% and `y`. `xlength` scales how much of the interval [x(1) x(end)] is coverd
+% by the line. `offset` is a multiplicative offset to where the line is drawn
+% relative to the data.
+function hand = plotConvergenceFit(slope, x, y, xlength, offset)
+    default_arg('xlength', 0.8)
+    default_arg('offset', 1);
+
+    % Optimise for log(y) = p*log(x) + q
+
+    p = slope;
 
-function hand = plotConvergenceFit(slope, pos, width)
-    x0 = pos(1);
-    y0 = pos(2);
+    logx = log(x);
+    logy = log(y);
+
+    N = length(logx);
+
+    q = 1/N*sum(logy-p*logx);
 
-    x = [x0*10^-(width/2) x0*10^(width/2)];
-    y = x.^slope * x0^-slope * y0;
+    logxlength = xlength * abs(logx(end)-logx(1));
+    logxends = (logx(1)+logx(end))/2 + [-logxlength/2, logxlength/2];
 
-    hand = line(x,y);
+    xends = exp(logxends);
+    yends = exp(q)*xends.^p;
+
+    hand = line(xends, yends);
     hand.Color = Color.black;
     hand.LineStyle = '--';
     hand.LineWidth = 2;
-end
\ No newline at end of file
+end
+
+
+
+% function hand = plotConvergenceFit(slope, pos, width)
+%     x0 = pos(1);
+%     y0 = pos(2);
+
+%     x = [x0*10^-(width/2) x0*10^(width/2)];
+%     y = x.^slope * x0^-slope * y0;
+
+%     hand = line(x,y);
+%     hand.Color = Color.black;
+%     hand.LineStyle = '--';
+%     hand.LineWidth = 2;
+% end
\ No newline at end of file