MATLAB Answers

Find x for known y from fit

19 ビュー (過去 30 日間)
Adam
Adam 2017 年 10 月 4 日
回答済み: Walter Roberson 2017 年 10 月 4 日
Hi all
I would like to find values of x when y values are known using fzero. Assuming I have the following data:
x = [1 3 6 8 14 19 20 22];
y = [0.3 0.5 0.8 0.85 1 1.05 1.5 1.9];
xf=linspace(0,22,300)
yf=interp1(x,y,xf,'spline');
plot(x,y,'LineStyle','none','Color','k', 'MarkerSize',4 ,'Marker','square');
hold on
plot(xf,yf,'-r')
To find for example the value of x at yy=1.5 I used:
xdatax=fzero(@(xi)interp1(x,y,xi,'spline')-yy,5)
it works and it gives 3. But changing the value of yy (e.g.yy=1.5) gives xdata=-5.04 which is worong!!!
Does anyone know why it gives wrong results for some yy values? I appreciate your help and thanks..
Adam
  1 件のコメント
Adam
Adam 2017 年 10 月 4 日
Correction:
xdatax=fzero(@(xi)interp1(x,y,xi,'spline')-yy,5)
must be
xdatax=fzero(@(xf)interp1(x,y,xf,'spline')-yy,5)

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

採用された回答

Walter Roberson
Walter Roberson 2017 年 10 月 4 日
-5.04 is a valid answer considering that you did not constrain the search range. Perhaps you want
xdatax = fzero(@(xf)interp1(x,y,xf,'spline')-yy,[min(x) max(x)])
Note that this will give an error if y(1)-yy is the same sign as y(end)-yy
Also, there are values such as 0.9 that occur multiple times; your code does not define which of the values will be located.
With spline fit, you are going to get "overcorrections". If, for example, you have a line that angles up to the right and it has a peak, then the spline will typically have its peak a little higher, because splines do not have sharp angles. This can result in false matches.
I suggest you reconsider your algorithm. The false matches you can get cannot be justified unless you know that the underlying physical process happens to have a spline response (for example your measurements happen to be along the edge of some bent wood.)

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by