# Surface fit of 3D plot

20 views (last 30 days)
Davide Bassignana on 3 Dec 2017
Answered: Elizabeth Reese on 5 Dec 2017
I have the plot presented in figure, every line was plotted using a constant x coordinate, a vector y which is the same for every line and a vector z which depend on the x-coordinate (pdf attached). I would like to add a fitting surface, but since every line was created with a plot command I don't know how could I possibly do it.
figure
hold on
p0=plot3(x_coord(:,1),y_beam,z.N2.p0,'LineWidth',2.5);
p1=plot3(x_coord(:,2),y_beam,z.N2.p1,'LineWidth',2.5);
p2=plot3(x_coord(:,3),y_beam,z.N2.p2,'LineWidth',2.5);
p3=plot3(x_coord(:,4),y_beam,z.N2.p3,'LineWidth',2.5);
p4=plot3(x_coord(:,5),y_beam,z.N2.p4,'LineWidth',2.5);
p5=plot3(x_coord(:,6),y_beam,z.N2.p5,'LineWidth',2.5);
p6=plot3(x_coord(:,7),y_beam,z.N2.p6,'LineWidth',2.5);
grid on
view(3);
rotate3d on
plot3(x_points,zeros(length(x_points),1),zeros(length(x_points),1),'-.',...
'Color','k','LineWidth',2.5);

Elizabeth Reese on 5 Dec 2017
Let's assume that your x_coord is n x 7 and that y_beam is n x 1. Then each of x.N2.pi for i = 0:6 is n x 1.
In order to use fit, the surface fitting function, these need to be the same size and all vectors. You can build these vectors with the following:
X = x_coord(:) % this returns the elements of x_coord in column order
Y = repmat(y_beam, 7,1) % this creates a column vector of 7 y_beams stacked on top of one another
Z = [z.N2.p0 ; z.N2.p1 ; z.N2.p2 ; z.N2.p3 ; z.N2.p4 ; z.N2.p5 ; z.N2.p6] % This stacks the z.N2.pi vectors on top of one another
Then you can do the fitting:
f = fit([X,Y],Z,'fittype'); % change this to be the fit type that best matches your problem
plot(f, [X,Y], Z);
Here is the documentation for the fit function.