3D mesh from a set of function points or samples
Here I share with you the technique for creating a mesh or a surface in Octave / Matlab:
Suppose you have samples of a function f(x,y) = z
Then you will have three vectors: x, y and z with the data from which you want to create the surface. Something like:
x = [x1 x2 x3 ... xN]
y = [y1 y2 y3 ... yN]
z = [z1 z2 z3 ... zN]
thus
f(x1,y1) = z1, f(x2,y2) = z2 and so on.
First of all you can use the plot3 function to have an idea of the function form:
plot3(x,y,z);
Well here is where the magic comes:
xlin = linspace(min(x), max(x), 50);
ylin = linspace(min(x), max(x), 50);
[X Y] = meshgrid(xlin, ylin);
Z = griddata(x,y,z,X,Y);
mesh(X,Y,Z);
It's important to know that the mesh wont be an exact representation of your function, points will be interpolated! I encourage you to play with the third argument of the linspace (I have set it to 50) and check if you have a nice result.
A sample result:
Another technique that you can try is the following:
dx = 0.1;
dy = 0.1;
xlin = [floor(min(x)):dx:ceil(max(x))];
ylin = [floor(min(y)):dy:ceil(max(y))];
[X Y] = meshgrid(xlin, ylin);
Z = griddata(x,y,z,X,Y);
mesh(X,Y,Z);
Here you can play with the dx and dy for obtaining different results.
Note that in both examples you can use surf(X,Y,Z) instead of mesh(X,Y,Z).
Nice coding,
Alan Karpovsky






