Interpolate between specific values in a data set

I am having trouble trying to figure out how to interpolate between specific y values for 2 given x values from excel.
For example in excel:
X Y
1 0.2
2.2 0.35
3 0.4
4 0.5
then given an X value of 3.5 from a matlab output I would like to find the corresponding Y value, in this case it would be 0.45. Then store that and use that in the future. I am not sure how to search for where 3.5 would sit within the X column and then find the surrounding values to do the interpolation.

 採用された回答

the cyclist
the cyclist 2022 年 5 月 8 日
編集済み: the cyclist 2022 年 5 月 8 日

0 投票

You can use the interp1 function to do interpolation.
x = [1, 2.2, 3, 4];
y = [0.2, 0.35, 0.4, 0.5];
x0 = 3.5;
y0 = interp1(x,y,x0)
y0 = 0.4500

4 件のコメント

Reece Smith
Reece Smith 2022 年 5 月 8 日
oh okay thanks, didn't realise it was as simple as that. I implemented it into the code but the values seem to be slightly off incomparison to my hand calculated values which I have double checked are right, I am guessing this is just since interp1 does it a little different?
Torsten
Torsten 2022 年 5 月 8 日
編集済み: Torsten 2022 年 5 月 8 日
0.45 = 0.4 + (0.5-0.4)/(4-3) * (4-3.5)
Linear interpolation between (3/0.4) and (4/0.5).
What's your method ?
Reece Smith
Reece Smith 2022 年 5 月 8 日
Yea its the one you just put. I am comparing my answers to someone elses interpolations so maybe they just rounded earlier and made it slightly different.
the cyclist
the cyclist 2022 年 5 月 8 日
@Reece Smith, if you can upload an example of the data and code that give a different result from what you expect, we could try to help you understand the difference.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInterpolation についてさらに検索

製品

リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by