How to use pchip to interpolate between data points in cartesian coordinate format
古いコメントを表示
I am trying to use pchip to interpolate between a series of data points. My data are spatial (lat/long) but have been converted into meters in a projected cartesian coordinate system (UTM 31N- i.e. x= 431804m, y=4571664m).
The code that I am using is below, but for some reason I get error messages when trying to create objects 't' and 'p'. long_m is a 1xcolumn matrix of longitude in meters, lat_m is a 1xcolumn matrix of latitude in meters, 0.01667= 1/60th in decimal, specifying that I want to end up 60 times more data points than I have now via the interpolation:
x=long_m;
y=lat_m;
t=x(1,1):0.016667:x(211,1);
p=pchip(x,y,t);
Output message:
t =
Empty matrix: 1-by-0
p =
Empty matrix: 1-by-0
I thought that the function would return a string of numbers that relate to my interpolated points, but instead I end up with an empty 't' matrix and an empty 'p' matrix. What am I doing wrong?
Some example data is below:
x= [518666 521872 519984 519591 518800];
y= [4694989 4667173 4644884 4645622 4647104];
I am new to matlab (although I have knowledge of programming language from R), so any advice would be greatly appreciated.
採用された回答
その他の回答 (1 件)
Shashank Prasanna
2013 年 8 月 23 日
編集済み: Shashank Prasanna
2013 年 8 月 23 日
This works perfectly fine for me:
>> x= [518666 521872 519984 519591 518800];
>> y= [4694989 4667173 4644884 4645622 4647104];
>> p=pchip(x,y,x)
p =
4694989 4667173 4644884 4645622 4647104
p=pchip(x,y,x(1):0.01:x(end));
7 件のコメント
Shashank Prasanna
2013 年 8 月 23 日
編集済み: Shashank Prasanna
2013 年 8 月 23 日
Your understanding of how the function works is not correct.
pchip first builds the model and lets you interpolate based on the pchip model.
If you have new data which is more finely defined:
x_new = x(1):0.01:x(end);
length(x_new)
ans =
13401
p = pchip(x,y,x_new);
pchip will find all the corresponding points for y based on your n_new
Which means you x_new and p are the new x and new y which you can use for plotting.
The output matrix you are referring to is simply:
out = [x_new p];
The doc does a very good job at explaining this as well as provides you with good examples:
HTH
Rhiannon
2013 年 8 月 23 日
Shashank Prasanna
2013 年 8 月 23 日
編集済み: Shashank Prasanna
2013 年 8 月 23 日
x is decreasing in value.
x(1):0.1:x(end) makes no sense for decreasing values.
this does:
x(1):-0.1:x(end)
Rhiannon
2013 年 8 月 26 日
Shashank Prasanna
2013 年 8 月 26 日
Rhiannon, your Y can be increasing or decreasing. You can always sort your X which is meant to be increasing on the X axis. Your Y could be anything and PCHIP will happily interpolate for new X.
[sortX, idx] = sort(X);
sortY = Y(idx);
This way you can always take the sorted X to interpolate
newY = pchip(sortX,sortY,sortX(1):0.01:sortX(end));
And this doesn't change your data or results at all.
Rhiannon
2013 年 8 月 26 日
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!