delkov
delkov

Reputation: 311

Smooth Contour Plot in matlab

I want to plot smooth contour plot from X Y Z matrix.

sf = fit([X Y] Z, 'poly23');
plot(sf);

I have not enought smooth curve.. What I need?

enter image description here

Upvotes: 1

Views: 1780

Answers (1)

Capo Mestre
Capo Mestre

Reputation: 301

You can use the functions such as griddata and csaps. Together they will lead you to the result as smooth as you wish. The first function adds additional points to your data matrix set. The second one makes the result smoother. The example of the code is below. In the example smoothing is done first in X direction and then in Y direction. Try to play around with the resolution and smoothing_parameter (the current set of these parameters should be OK though).

x = min_x:step_x:max_x;
y = min_y:step_y:max_y;
resolution = 10;

xg = min_x:(step_x/resolution):max_x;
yg = min_y:(step_y/resolution):max_y;
[X,Y] = meshgrid(x,y);
[XG,YG] = meshgrid(xg,yg);    


smoothing_parameter = 0.02;
fitted = griddata(X,Y,Z,XG,YG,'cubic');
fitted_smoothed_x = csaps(xg,fitted,smoothing_parameter,xg);
fitted_smoothed_xy = csaps(yg,fitted_smoothed_x',smoothing_parameter,yg);

surf(XG,YG,fitted_smoothed_xy');

EDIT: If you want to get just a contour plot, you can do, for example, as presented below. As I don't have the real data, I will use build-in function peaks to generate some.

[X,Y,Z] = peaks(30);
figure
surfc(X,Y,Z)
view([0 90])
zlim([-10 -8])

Here you just look at your contour plot from above being below the surface.

Upvotes: 2

Related Questions