3D - Surface Response Plot - Surface of best fit

I'm having trouble plotting a curve/surface of best fit through data points. I have z - matrix of 9 data points, which correspend to different combinations of values from an x-vector of 3 and a y-vector of 3. I have managed to plot a surface plot which uses interpolation and fits a curve through the data. Instead of this, I would like to plot a curve of best fit through the data points using a low order polynomial instead.
Assistance would be greatly appreciated.
This is a plot of what I have so far.

 Accepted Answer

If you have a curve fitting toolbox, you can use fit(): https://www.mathworks.com/help/curvefit/fit.html function with fitype chosen from polyij as given here: https://www.mathworks.com/help/curvefit/list-of-library-models-for-curve-and-surface-fitting.html#btbcxlm. For example
x; % x-values 9x1
y; % y-values 9x1
z; % z-values 9x1
X = [x y];
Y = z;
fitted_model = fit(X, Y, 'poly22')
If you don't have the toolbox, you can still use mldivide (\) to do least square curve-fitting. For example, suppose you want to fit following model
Then you can do something like this
X = [ones(size(x)) x y x.^2 y.^2 x.*y];
Y = z;
a = X\Y;

11 Comments

Would it look something like this?
z(1) is at x(1), y(1).
z(2) is at x(1), y(2).
z(3) is at x(1), y(3).
z(4) is at x(2), y(1).
z(5) is at x(2), y(2).
z(6) is at x(2), y(3).
How do I then plot the fitted model through the data points on a 3D plot, while plotting each data point as well?
You can evaluate a fitted model using functional syntax (fitted_model([x y])). See this example
x = rand(10, 1);
y = rand(10, 1);
z = rand(10, 1);
X = [x y];
Y = z;
fitted_model = fit(X, Y, 'poly22');
xg = linspace(min(x), max(x), 20);
yg = linspace(min(y), max(y), 20);
[Xg, Yg] = meshgrid(xg, yg);
Zg = fitted_model([Xg(:) Yg(:)]);
Zg = reshape(Zg, size(Xg));
figure();
ax = axes();
hold(ax);
view(3);
grid(ax);
plot3(x, y, z, 'r+', 'MarkerSize', 10, 'LineWidth', 2)
surf(Xg, Yg, Zg);
I'm just unsure how to define my initial x, y and z vectors
You already have x, y, and z vectors as you showed in your last comment. I just use rand() as an example.
Oh no I got that, I just mean is i matching the correct values together?
This is my output which is quite wrong as my z-values should be around 10
I should also have nine data points on the graoh instead of the three shown.
Can you share your x, y, and z values as text. You attach it as an image so I cannot try anything. Also, try using a higher order polynomial. For example, 'poly33'.
This is what I initially had as a simple surface plot
Flow_1 = [760 980 1245] % l/min
rpm = [1550 2730 3650]
Ra_1f = [10.65366667 9.462 10.266; 13.73666667 14.53466667 12.15333333; 12.725 11.236 13.26933333];
x = Flow_1;
y = rpm;
z = [Ra_1f(:,1)'; Ra_1f(:,2)'; Ra_1f(:,end)'];
xlin = linspace(min(x),max(x),33);
ylin = linspace(min(y),max(y),33);
[X,Y] = meshgrid(xlin,ylin);
[M,N] = meshgrid(x,y); % Plot of points
Z = griddata(x,y,z,X,Y,'cubic');
Z1 = griddata(x,y,z1,X,Y,'cubic');
Z2 = griddata(x,y,z2,X,Y,'cubic');
mesh(X,Y,Z) %interpolated
axis tight; hold on
xlabel('Air-flow Rate (l/min)')
ylabel('Motor Speed (rpm)')
zlabel('Surface Roughness (µm)')
plot3(M,N,z,'.','MarkerSize',20)
legend('','760 l/min','980 l/min','1245 l/min' )
title('Test 1: Final Ra')
colorbar;
hold off
This code shows how to use the fitted_model to draw the surface
Flow_1 = [760 980 1245] % l/min
rpm = [1550 2730 3650]
Ra_1f = [10.65366667 9.462 10.266; 13.73666667 14.53466667 12.15333333; 12.725 11.236 13.26933333];
x = Flow_1;
y = rpm;
z = [Ra_1f(:,1)'; Ra_1f(:,2)'; Ra_1f(:,end)'];
[xg, yg] = meshgrid(x,y); % Plot of points
xg = xg(:);
yg = yg(:);
zg = z(:);
fitted_model = fit([xg yg], zg, 'poly22');
xlin = linspace(min(x),max(x),33);
ylin = linspace(min(y),max(y),33);
[X, Y] = meshgrid(xlin,ylin);
Z = fitted_model([X(:) Y(:)]);
Z = reshape(Z, size(X));
mesh(X,Y,Z) %interpolated
axis tight; hold on
xlabel('Air-flow Rate (l/min)')
ylabel('Motor Speed (rpm)')
zlabel('Surface Roughness (µm)')
plot3(M,N,z,'.','MarkerSize',20)
legend('','760 l/min','980 l/min','1245 l/min' )
title('Test 1: Final Ra')
colorbar;
hold off
This is perfect.
Thank you so much for the assistance!
Much appreciated.
I am glad to be of help!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!