How to make the contour smooth other way(spline) and want to color a specific part

15 views (last 30 days)
I wnat make a contour more smooth.
The code using __fit('cubicinterp')
'cubicinterp' is 3rd interpolation by each point?. i want interpolation by all point
how to make the contour more smooth (use spline? )
and I want to fill in the only red part (x1,y1,z1)
plz help me
Thanks
x=[250 250 250 250 250 250 300 300 300 300 300 300 350 350 350 350 350 350 400 400 400 400 400 400 450 450 450 450 450 450 500 500 500 500 500 500]';
y=[-3 -2 -1 0 1 2 -3 -2 -1 0 1 2 -3 -2 -1 0 1 2 -3 -2 -1 0 1 2 -3 -2 -1 0 1 2 -3 -2 -1 0 1 2]';
z=[45.32387079 29.24976486 18.98522122 20.34655974 9.322369104 6.099755477 35.95644961 36.63977979 22.23230132 15.44277474 26.46045535 27.33915193 47.61434049 38.89634871 31.30817744 27.35747741 13.92859198 6.494014321 55.87879512 45.20216626 36.04598958 30.00851201 12.3192618 4.23514123 58.91006549 50.26502058 37.81663326 31.22407039 21.04293731 14.41272158 60.07610895 52.66807669 41.19547341 31.91688177 23.86284429 22.12621742]';
f = fit( [x, y],z, 'cubicinterp' )
Piecewise cubic interpolant: f(x,y) = piecewise cubic surface computed from p Coefficients: p = coefficient structure
D = plot(f, 'Style', 'Contour');
set(D, 'LevelList',3:4:56, 'ShowText','on', 'Fill','off', 'LineColor', 'auto')
grid off;
set(gca,'FontSize',15)
hold on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x1=[250 250 250 250 250 250 300 300 300 300 300 300 350 350 350 350 350 350 400 400 400 400 400 400 450 450 450 450 450 450 500 500 500 500 500 500]';
y1=[-3 -2 -1 0 1 2 -3 -2 -1 0 1 2 -3 -2 -1 0 1 2 -3 -2 -1 0 1 2 -3 -2 -1 0 1 2 -3 -2 -1 0 1 2]';
z1=[0.102819727 -0.017654204 0.026066466 -0.041190188 -0.212698548 -0.152755654 0.227364491 0.11989207 -0.062549974 0.12148117 0.708627406 1.036772875 0.224630519 0.150396798 0.109177532 -0.017412138 -0.237425239 -0.297834215 0.295633059 0.196812704 0.130896878 -0.056606636 -0.359548825 -0.44208297 0.348611924 0.23943992 0.129783908 0.057714851 -0.050285245 -0.086700291 0.372194136 0.275552906 0.150646442 0.071323847 0.055922125 0.091577766]';
f1 = fit( [x1, y1], z1, 'cubicinterp' )
Piecewise cubic interpolant: f1(x,y) = piecewise cubic surface computed from p Coefficients: p = coefficient structure
d = plot(f1, 'Style', 'Contour');
set(d, 'LevelList',0:0.1:0, 'ShowText','off', 'Fill','off', 'LineColor', 'red')
grid off;
  1 Comment
Adam Danz
Adam Danz on 10 Dec 2021
I've edited the question to format the code and produce the outputs usng the run feature (green arrow in toolbar).

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 10 Dec 2021
> 'cubicinterp' is 3rd interpolation by each point?
Matlab documentation describes cubicinterp as Piecewise cubic interpolation. I looked for more details in the documentation to determine how this is implemented and its difference from cublicspline but didn't find much. However, these terms are general terms and you can look them up to find background info on the internet.
> how to make the contour more smooth (use spline? )
If you're just looking for a visually more smoother contour line, you could try several methods until you're satisfied. Here is a list of model for curve and surface fitting:
> and I want to fill in the only red part
  1. Redefine the LevelList to segment your desired contour levels. Based on your data and your description, you want to segement all negative levels.
  2. Fill in the remaining contour areas. To do this, I use getContourLineCoordinates from the file exchange to get the contour coordinates and then patch to fill in the areas.
% SKipping the first part of your code that plots
% the first layer of contour lines
x1=[250 250 250 250 250 250 300 300 300 300 300 300 350 350 350 350 350 350 400 400 400 400 400 400 450 450 450 450 450 450 500 500 500 500 500 500]';
y1=[-3 -2 -1 0 1 2 -3 -2 -1 0 1 2 -3 -2 -1 0 1 2 -3 -2 -1 0 1 2 -3 -2 -1 0 1 2 -3 -2 -1 0 1 2]';
z1=[0.102819727 -0.017654204 0.026066466 -0.041190188 -0.212698548 -0.152755654 0.227364491 0.11989207 -0.062549974 0.12148117 0.708627406 1.036772875 0.224630519 0.150396798 0.109177532 -0.017412138 -0.237425239 -0.297834215 0.295633059 0.196812704 0.130896878 -0.056606636 -0.359548825 -0.44208297 0.348611924 0.23943992 0.129783908 0.057714851 -0.050285245 -0.086700291 0.372194136 0.275552906 0.150646442 0.071323847 0.055922125 0.091577766]';
f1 = fit( [x1, y1], z1, 'cubicinterp' )
d = plot(f1, 'Style', 'Contour');
% Set LevelList to segment negative contours
set(d, 'LevelList', [d.LevelList(1), 0], 'ShowText','off', 'Fill','off', 'LineColor', 'red')
% Get contour line coordinates (from file exchange)
% https://www.mathworks.com/matlabcentral/fileexchange/74010-getcontourlinecoordinates
contourTable = getContourLineCoordinates(d);
% Loop through each contour patch
hold on
for i = 1:max(contourTable.Group)
rowidx = contourTable.Group == i;
patch(contourTable.X(rowidx),contourTable.Y(rowidx), 'r', 'FaceAlpha', .5)
end
grid off;

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!