# smooth a surface plot

22 visualizaciones (últimos 30 días)
Lauren E el 2 de Abr. de 2015
Comentada: imran saeed el 15 de Feb. de 2021
I have an array of data which I'm plotting using the surface function. I'd like to smooth the data but interp2 doesn't work since I don't have a mesh (I'm plotting data, not a function). Is there a way I can smooth this?
My code isn't much to look at:
surface = surf(P1,P2,L);
where P1, P2 and L are imported from a data file.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (1)

Andrew Newell el 2 de Abr. de 2015
Editada: Andrew Newell el 2 de Abr. de 2015
You can make a mesh:
[P1,P2] = meshgrid(P1,P2);
Then use interp2.
EDIT: Corrected typo.
##### 5 comentariosMostrar 3 comentarios más antiguosOcultar 3 comentarios más antiguos
Andrew Newell el 2 de Abr. de 2015
Sorry, I was careless with the notation. Here is a working example of what I mean, starting with an example from the MATLAB documentation:
x = -8:1:8; y = x;
[X,Y] = meshgrid(x,y);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
If we ignore how the data were generated, we now have vectors x, y and a matrix Z, and we could plot
surf(x,y,Z)
Now lets' interpolate to a finer grid. I like the spline method because it makes a smoother interpolation:
Xq = -8:.5:8; Yq = Xq;
[Xq,Yq] = meshgrid(Xq,Yq);
Zq = interp2(X,Y,Z,Xq,Yq,'spline');
figure
surf(Xq,Yq,Zq)
imran saeed el 15 de Feb. de 2021
Based on above discussion I made this funciton, it will make coding easier, you can call this function many times to make your function smoother and smoother,
function [X,Y,Z]=smooth_spline(x,y,z)
n=length(x); sp=min(x); ep=max(x);
X=linspace(sp,ep,2*n);
sp=min(y); ep=max(y);
Y=linspace(sp,ep,2*n);
[Kxx,Kyy]=meshgrid(X,Y);
Z = interp2(x,y,z,Kxx,Kyy,'spline');
end

Iniciar sesión para comentar.

### Categorías

Más información sobre Smoothing en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by