how to interpolate x and y coordinates of contour?

68 views (last 30 days)
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.
image.PNG
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.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 5 Apr 2019
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 Comments
Bjorn Gustavsson
Bjorn Gustavsson on 5 Apr 2019
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
manoj saladi
manoj saladi on 6 Apr 2019
Hi,
I did interpolation using interp2 function and its working very fine. But it takes too much of time to simulate when I increase 'v' in the contour.
syntax:[K,c] = contour(idq,isd,Psi_abs,v);
I know the reason , its because im storing all the values into a cell every time. When I did this for v= 1000; it may take nearly 10 min. I there any way to not to store the values of contours but get the result finally?
Thank you.

Sign in to comment.

More Answers (2)

darova
darova on 4 Apr 2019
Look for interp2 then build countour
  3 Comments
darova
darova on 5 Apr 2019
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');
manoj saladi
manoj saladi on 5 Apr 2019
Hi,
Thank you very much for your explanation . I tried using this code, but I got to know that I'm not extracting contour values correctly in every iteration, so I have to interpolate it first.
Best Regards,
Manoj.

Sign in to comment.


manoj saladi
manoj saladi on 5 Apr 2019
I will try it today

Categories

Find more on Contour Plots in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!