# Plot a function of three variables on a surface plot

27 views (last 30 days)

Show older comments

##### 1 Comment

### Answers (1)

Star Strider
on 30 Mar 2023

If all the data are vectors (that would appear to be true since ‘x’ is a vector), it would be necessary to use either the griddata or scatteredInterpolant function to create a surface from them.

.

##### 2 Comments

Star Strider
on 2 Apr 2023

Here is an example using scatteredInterpolant and random vectors —

x = randn(1,20); % Create Data Vector

y = randn(1,20); % Create Data Vector

z = randn(1,20); % Create Data Vector

xv = linspace(min(x), max(x), numel(x)); % Interpolatioon Vector

yv = linspace(min(y), max(y), numel(y)); % Interpolation Vector

[X,Y] = ndgrid(xv,yv); % Interpolation Matrices

F = scatteredInterpolant(x(:),y(:),z(:)); % Create Interpolant Function

Z = F(X,Y); % Calculate 'Z' By Interpolation

figure

surfc(X, Y, Z) % Plot Result

grid on

xlabel('X')

ylabel('Y')

zlabel('Z')

colormap(turbo) % Introduced In R2020b

colorbar

The griddata function works similarly, however has different arguments and produces a lsightly different result.

.

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!