How to pick a curve with a series of segmented lines in Matlab?

enter image description here

I have a simple loglog curve as above. Is there any function in Matlab that can match this curve with segmented lines and display the start and end points of these line segments? I checked the curve fitting toolkit in Matlab. It seems that they execute the curve with either a single line or some functions. I do not want to curve with only one line.

If there is no direct function, any alternative to achieve the same goal is fine with me. My goal is to fit the curve along the segmented lines and get the location of the end points of these segments.

+5
source share
3 answers

, . - , , , . , - .

:

  • . , →
  • , .

, . , () , . - .

enter image description here

function fitLogLog()
   x = 2:1000;
   y = log(log(x));

   %# Find section sizes, by using an inverse of the approximation of the derivative
   numOfSections = 20;
   indexes = round(linspace(1,numel(y),numOfSections));
   derivativeApprox = diff(y(indexes));
   inverseDerivative = 1./derivativeApprox;
   weightOfSection =  inverseDerivative/sum(inverseDerivative);   
   totalRange = max(x(:))-min(x(:));   
   sectionSize = weightOfSection.* totalRange;

   %# The relevant nodes
   xNodes = x(1) + [ 0 cumsum(sectionSize)];
   yNodes = log(log(xNodes));

   figure;plot(x,y);   
   hold on;
   plot (xNodes,yNodes,'r');
   scatter (xNodes,yNodes,'r');
   legend('log(log(x))','adaptive linear interpolation');
end
+7

Andrey . , , , , . , .

Nsamp = 1000;     %number of data samples on x-axis
x = [1:Nsamp];    %this is your x-axis
Nlines = 5;       %number of lines to fit

fx = exp(-10*x/Nsamp);  %generate something like your current data, f(x)
gx = NaN(size(fx));     %this will hold your fitted lines, g(x)

joins = round(linspace(1, Nsamp, Nlines+1));  %define equally spaced breaks along the x-axis

dx = diff(x(joins));   %x-change
df = diff(fx(joins));  %f(x)-change

m = df./dx;   %gradient for each section

for i = 1:Nlines
   x1 = joins(i);   %start point
   x2 = joins(i+1); %end point
   gx(x1:x2) = fx(x1) + m(i)*(0:dx(i));   %compute line segment
end

subplot(2,1,1)
h(1,:) = plot(x, fx, 'b', x, gx, 'k', joins, gx(joins), 'ro');
title('Normal Plot')

subplot(2,1,2)
h(2,:) = loglog(x, fx, 'b', x, gx, 'k', joins, gx(joins), 'ro');
title('Log Log Plot')

for ip = 1:2
    subplot(2,1,ip)
    set(h(ip,:), 'LineWidth', 2)
    legend('Data', 'Piecewise Linear', 'Location', 'NorthEastOutside')
    legend boxoff
end

MATLAB plotted output

+5

, , , ( ) - , ( - ) .

-, , , , , -, MATLAB, wikipedia, .

,

function [x, y] = intervalAggregate(Xdata, Ydata, aggFun, intStep, intOverlap)
% intOverlap in [0, 1); 0 for no overlap of intervals, etc.
% intStep    this is the size of the interval being aggregated.

minX = min(Xdata);
maxX = max(Xdata);

minY = min(Ydata);
maxY = max(Ydata);

intInc = intOverlap*intStep; %How far we advance each iteraction.
if intOverlap <= 0
   intInc = intStep;
end
nInt = ceil((maxX-minX)/intInc); %Number of aggregations

parfor i = 1:nInt
    xStart = minX + (i-1)*intInc;
    xEnd   = xStart + intStep;
    intervalIndices = find((Xdata >= xStart) & (Xdata <= xEnd));
    x(i) = aggFun(Xdata(intervalIndices));
    y(i) = aggFun(Ydata(intervalIndices));
end

, X Y, 0,1, 1/3 (. ):

Scatter plot example for Xdat and Ydat

[x, y] = intervalAggregate (Xdat, Ydat, @mean, 0.1, 0.333)

x =

1 8

0.0552    0.0868    0.1170    0.1475    0.1844    0.2173    0.2498    0.2834

9 15

0.3182    0.3561    0.3875    0.4178    0.4494    0.4671    0.4822

y =

1 8

0.9992    0.9983    0.9971    0.9955    0.9927    0.9905    0.9876    0.9846

9 15

0.9803    0.9750    0.9707    0.9653    0.9598    0.9560    0.9537

, x y . / .

( , , , Xdata.)

0

All Articles