How to use pchip to interpolate between data points in cartesian coordinate format
Show older comments
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.
Accepted Answer
More Answers (1)
Shashank Prasanna
on 23 Aug 2013
Edited: Shashank Prasanna
on 23 Aug 2013
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 Comments
Shashank Prasanna
on 23 Aug 2013
Edited: Shashank Prasanna
on 23 Aug 2013
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
on 23 Aug 2013
Shashank Prasanna
on 23 Aug 2013
Edited: Shashank Prasanna
on 23 Aug 2013
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
on 26 Aug 2013
Shashank Prasanna
on 26 Aug 2013
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
on 26 Aug 2013
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!