how to interpolate x and y coordinates of contour?
35 次查看(过去 30 天)
显示 更早的评论
Hi,
I have been trying to interpolate the x and y coordinates obtained from contour matrix. The way i extract x and y coordinates from contour matrix is as show below.

In the for loop for every iteration I can obtain x and y values from the contour matrix K. I want to interpolate these (x and y coordinate ) values with a resolution of 1800 'res'. I wrote detailed explanation of what im doing beside every line. Basically when I plot 'contour(isq,isd,Psi_abs,v)' it gives me 6 concentric ellipses which are not very smooth, my intention is to make these concentric ellipses more smooth by giving a resolution res=1800 ( with a hope that generated ellipses have more x and y coordinates such that they are very smooth thatn before). But after using the above given code i'm getting weird plots as i attached image.

what i'm expecting is more smooth concentric ellipses. Also in the code Im not including level-verices column and the last columns of K matrix (controur matrix) (since it contains same values as in second column of K matrix). But still its producing the plots as shown above. Is there any way then to interpolate the x and y coordinates.
- Can anyone plz provide me brief or detailed way to do this? The code what I wrote may also be a incorrect, please provide me any sugesstions.
0 个评论
采纳的回答
Bjorn Gustavsson
2019-4-5
You extract the contours wrong, at least if you have multiple contours at the same level v(i). It is simpler if you plot all the contours at once:
[K,c] = contour(idq,isd,Psi_abs,v);
Then you have to extract them segment-by-segment. The format for K is:
K = [level1, x1_1,x1_2,.. ,x1_n1,level2, x2_1,x2_2,..,x2_n2,level3
n1,y1_1,y1_2,..,y1_n1, n2, y2_1,y2_2,..,y2_n2, n3]
So you'll have to extract your contour-segments something like this:
iC = 1;
idxC = 1;
while iC < size(K,2)
nP = K(2,iC); % number of points in current contour
xC{idxC} = K(1,iC+(1:nP)); % x coordinates of current contour
yC{idxC} = K(2,iC+(1:nP)); % y coordinates of current contour
iC = iC+nP+1; % Start-point of next contour
idxC = idxC + 1; % next contourline index
% plot(xC{idxC-1},yC{idxC-1},'b.-') %% If you want to look at the separate
% pause %% segments as they are separated.
end
That would give you a pair of cell-arrays with the x and y coordinates for the contour-segments, that you then can reinterploate and doodle with as you see fit, perhaps the Interparc contribution is of interest to you...
HTH
5 个评论
Bjorn Gustavsson
2019-4-5
Well if you have the x, y and mxn_matrix that interp2 accepts you can call interp2 with just about anything for xi and yi, and if they are both row or column vectors you will get the interpolated values for those points.
HTH
更多回答(2 个)
darova
2019-4-4
Look for interp2 then build countour
3 个评论
darova
2019-4-5
You have some data (isq; isd; Psi_abs). Its your XYZ. Why can you interpolate it?
clc, clear
x = -3:3; % your isq
y = -3:3; % your isd
[X, Y] = meshgrid(x,y);
Z = -X.^2 -3*Y.^2; % your Psi_abs
subplot(221)
contour(x, y, Z)
subplot(223)
surf(x, y, Z)
title('initial data');
[Xq, Yq] = meshgrid(-3:0.3:3); % creating mesh with more points
Zq = interp2(X,Y,Z, Xq,Yq,'cubic'); % interpolating
subplot(222)
contour(Xq, Yq, Zq)
subplot(224)
surf(Xq, Yq, Zq)
title('interpolated data');
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!