find x points which makes a best fit to a curve

3 ビュー (過去 30 日間)
Lars
Lars 2013 年 12 月 10 日
コメント済み: per isakson 2013 年 12 月 11 日
Hallo Matlab forum.
I need help to figure out how to reduce a curve described by 10.000 points to a curve described by less than 50 points and still have the best fit.
All my curves have the shape showen in the plot above, and as shown the problem is around the maximum and in the "tail" after.
I am currently reducing the amount of points by following two steps:
1: Remove point ii if the slope between point e(ii-1,1) to e(ii,1) is the same as the slope between point e(ii,1) to e(ii+1,1).
n = length(e) ;
for i = 1:n-2
ii = n-i ;
if abs((e(ii-1,2)-e(ii,2))/(e(ii-1,1)-e(ii,1)) - (e(ii,2)-e(ii+1,2))/(e(ii,1)-e(ii+1,1))) < 0.001
e(ii,:) = [];
end
end
2: Remove every 2nd row if there is still more than 50 rows left.
while length(e)>50
n = length(e) ;
dz = e(end-2,1) - e(end-1,1) ; % Stepsize.
for i = 2:2:n-15
ii = n-i ;
if (e(ii,1)-e(ii+1,1)) - dz < 0.001 && (e(ii-1,1)-e(ii,1)) - dz < 0.001
e(ii,:) = [];
end
end
end
What else can i do to improve the fitting of the curve with small amount of points?

採用された回答

Lars
Lars 2013 年 12 月 11 日
Think i have found a solution to the problem. It is slow but working fine.
I went with following reducing technique.
  • Removing row ii where the slope of e(ii-1) to e(ii) = e(ii) to e(ii+1)
  • Deleting every 2nd row if there are more than 40 rows.
  • Loop where it for every loop find the point with biggest difference in x-value between the original curve and the reduced data curve. Then this point is added to my reduced data.
The code i used to find the difference is:
e_temp = [z*-1 zeros(h/dz+1,1)] ;
j = 1 ; % counter
for i = 1:length(e)-1
if abs((e(i,1)-e(i+1,1))-dz) < dz*0.1 % if the two points is right next to each other
e_temp(j,2) = e(i,2) ;
j = j + 1 ;
else % if there is missing some points in between e(i) and e(i+1)
num = round((e(i,1)-e(i+1,1))/dz) ; % Number of "steps" missing
for ii = 1:num
slope = (e(i+1,2)-e(i,2))/(num) ;% Slope
e_temp(j,2) = e(i,2)+slope*(ii-1) ;
j = j+1 ;
end
end
end
e_temp(j,2) = e(end,2) ; % adding last point
Where: e_temp is a matrix with the size of the matrix to create the original curve, but with reduced point inside. The y-values where no reduced point is available is the point calculated by linear interpolation between point e(i) and e(i+1).

その他の回答 (1 件)

per isakson
per isakson 2013 年 12 月 10 日
  2 件のコメント
Lars
Lars 2013 年 12 月 11 日
Thanks a lot for the fast response.
I tried the function but couldn't get it to work properly. It seems like all my functions is compressed on the y-axis (and have a cut-off at y = -0.5).
I tried with different eps values, without any luck.
per isakson
per isakson 2013 年 12 月 11 日
Yes, you have to switch x and y

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFit Postprocessing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by