# Interpolation or meshgrid...Error using matlab.int​ernal.math​.interp1

26 views (last 30 days)
Tasha Williams on 29 Mar 2021
Commented: Tasha Williams on 8 Apr 2021 at 17:57
I have four array in total that are 900x1 each (R1,R2,T1,T2) I am having trouble taking two arrays (T1 and T2) and converting them to have equal spacing. I would like the 2 plots thetas to be changing by the same delta_theta.
is it possible to use meshgrid or interpolation or something else with points that in a polar coodinate system that preserves the same curves? R is not in ascending or descending order but theta is, if so How can I create these equally spaced arrays that produce accurate plots.
Initially, I created equally spaced theta points then try to create new R_data by using interp1 but wasn't able to do this because the R data needed to be in ascending order which messes up the plot.
data=[rr_sort tt_sort tt_spaced rr2_sort tt2_sort]
Code used
rr_new=zeros(900,1);
rr_new(1)=rr_sort(1);
for i=2:900
rr_new(i)=interp1(tt_sort,rr_sort,tt_spaced(i));
end
##### 2 CommentsShowHide 1 older comment
Tasha Williams on 29 Mar 2021
The data matrix is in the order seen: data=[rr_sort tt_sort tt_spaced rr2_sort tt2_sort],
I attempted to to alter the 1st two columns of data.mat with the code above. I am trying to make 2 and 5 column of data.mat change by the same amount from data point to the next while still maintaining the curve.
Polarplot(data(:,2),data(:,1)) one of the curves I would like to preserve.
Column 3 was the equally spaced theta values that I used for interpolation.

Chad Greene on 30 Mar 2021
Ah, it looks like tt_sort and tt2_sort are in fact sorted, but they contain some duplicate values. The easiest way to work around this is to simply ignore the values of rr_sort that correspond to the duplicate entries of tt_sort. This discards the extra datapoints, so depending on your application you may wish to do something else clever (like average all the duplicate value points), but this will get you going:
tt_sort = Data(:,2);
rr_sort = Data(:,1);
tt2_sort = Data(:,5);
rr2_sort = Data(:,4);
tt_spaced = Data(:,3);
ind = diff(tt_sort)>0; % indices where dtheta is nonzero
rr_spaced = interp1(tt_sort(ind),rr_sort(ind),tt_spaced);
polarplot(tt_sort,rr_sort,'o')
hold on
polarplot(tt_spaced,rr_spaced,'x')
ind = diff(tt2_sort)>0; % indices where dtheta is nonzero
rr2_spaced = interp1(tt2_sort(ind),rr2_sort(ind),tt_spaced);
figure
polarplot(tt2_sort,rr2_sort,'o')
hold on
polarplot(tt_spaced,rr2_spaced,'x')
Now one thing I noticed with the first set (tt_sort and rr_sort): I wonder if you want to wrap the phase to the range 0 to 2*pi before interpolating, because the method above interpolates over a large gap in the middle, while it looks like the curve wants to be continuous from +pi to -pi. To see what I mean, try plotting in cartesian coordinates like this:
plot(tt_sort,rr_sort,'o')
hold on
plot(tt_spaced,rr_spaced,'x')
##### 2 CommentsShowHide 1 older comment
Tasha Williams on 8 Apr 2021 at 17:57
Also, after the interpolation is done I'm getting When I should be getting Doesn't seem to start and end at the appropriate values. This is for tt_spaced and rr_spaced)