use polyval to plot a surface plot in matlab

polyval function in MATLAB can be used to evaluate a polynomial at specific values. To plot a surface using polyval, you first need to create a set of polynomial coefficients that define the surface. Then, you would evaluate the polynomial at a set of x and y values to create the surface. Here's an example code:

main.m
% Define the polynomial coefficients
coefficients = [1 2 3; 4 5 6; 7 8 9];

% Evaluate the polynomial at a set of x and y values
x_values = 0:.1:1;
y_values = 0:.1:1;
[X,Y] = meshgrid(x_values,y_values);
Z = polyval(coefficients, X) + polyval(coefficients, Y);

% Plot the surface
surf(X,Y,Z);
294 chars
12 lines

In the code above, we defined the coefficients of a 2D polynomial surface. We then created a set of x and y values using meshgrid, and used polyval to evaluate the polynomial surface at each point in the grid. Finally, we plotted the surface using surf.

Note that the specific function that you want to plot using polyval might not correspond to a polynomial surface, so this method might not be appropriate for all functions.

gistlibby LogSnag