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

1 view (last 30 days)
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 Comments
Chad Greene
Chad Greene on 29 Mar 2021
Edited: Chad Greene on 30 Mar 2021
Hi Tasha, welcome to the forum. Happy to help, but please clarify a few points.
The data in your data.mat file is just a single matrix that's 900x5, and there are no variable names associated with any of the five columns. The text of your question mentions at least four variable names, and another handful of variables appear in the code you posted, but there isn't any overlap between the variables you describe and the variables in the code.
Can you simplify the question by describing the relevant variables and removing the stuff that's unnecessary?
Tasha Williams
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.

Sign in to comment.

Answers (1)

Chad Greene
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 Comments
Tasha Williams
Tasha Williams on 8 Apr 2021
Hi, thanks for this help. However I noticed that rr_spaced did not get spaced out properly as rr2_spaced did. This was the original problem I am having. I need both arrays to have equal spacing. Is there a reason it only worked for one set of values and not the other?
Tasha Williams
Tasha Williams on 8 Apr 2021
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)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!