Problem with answer of interp1

1 回表示 (過去 30 日間)
noa
noa 2013 年 6 月 5 日
Hi
I am trying to use interp1 and i keep getting a WRONG answer i was hoping you could help me find the bug
P_vect = 6.1201 6.12 6.232 6.28 4.12 3.32
P = 4.747
Y_vect = 3000 3500 4500 5000 5500 5800
What is Y?
Y=interp1(P_vect, Y_vect, P)
matlab gives me 4873 when the answer should be 5354
thanks
  3 件のコメント
noa
noa 2013 年 6 月 5 日
well if P is between the 4th and the 5th values in P_vect
then Y should also be in those locations
Walter Roberson
Walter Roberson 2013 年 6 月 5 日
It is not between the 4th and 5th values once you have sorted Y_vect according to P_vect

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

回答 (3 件)

Walter Roberson
Walter Roberson 2013 年 6 月 5 日
interp1() does not appear to define the behavior when the x coefficients are not monotonic.
  1 件のコメント
Jan
Jan 2013 年 6 月 5 日
編集済み: Jan 2013 年 6 月 5 日
[EDITED] The fact that INTERP1 calls HISTC is interesting, but does not matter the problem. But the call of SORT for the x-values clears, that not monotonic inputs are handled correctly.
Therefore I think Matlab's interpolation is correct, and assumptions of the OP contain a mistake.

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


Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 5 日
編集済み: Azzi Abdelmalek 2013 年 6 月 5 日
Let us sort your vector
[P1,idx]=sort(P_vect )
Y1=Y_vect(idx)
Y=interp1(P1, Y1, P)
% in this case P is between the second(4.12) and the third (6.12) which correspond to value between 5500 and 3500
  1 件のコメント
Jan
Jan 2013 年 6 月 5 日
編集済み: Jan 2013 年 6 月 5 日
After sorting the calculated result still equals the results of the OP.
I think your first comment hits the point: Why is the result 5354 assumed?

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


Jan
Jan 2013 年 6 月 5 日
編集済み: Jan 2013 年 6 月 5 日
I get 4873 also, when I calculate this manually. So I assume Matlab is correct and your assumption is not.
The relevant data are:
x = [4.12, 6.12]
y = [5500, 3500]
delta_x = 2.0
delta_y = -2000
z = 4.747;
result = y(1) + (delta_y / delta_x) * (z - x(1))
= 5500 - 1000 * 0.637
= 4873

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by