Interpolation or meshgrid...Error using matlab.internal.math.interp1
조회 수: 3 (최근 30일)
이전 댓글 표시
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
Chad Greene
2021년 3월 29일
편집: Chad Greene
2021년 3월 30일
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?
답변 (1개)
Chad Greene
2021년 3월 30일
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')
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!