interpolate two functions and plot the result on a surface plot in matlab

To interpolate two functions and plot the result on a surface plot in MATLAB, you can use the griddata function to create a grid of interpolated values and then plot it using the surf function.

Here's an example code snippet:

main.m
% define the x and y coordinates for the data points
x = linspace(0, 1, 100);
y = linspace(0, 1, 100);
[X, Y] = meshgrid(x, y);

% define the two functions to be interpolated
f1 = @(x, y) sin(2*pi*x).*cos(2*pi*y);
f2 = @(x, y) cos(2*pi*x).*sin(2*pi*y);

% evaluate the two functions at the data points
Z1 = f1(X, Y);
Z2 = f2(X, Y);

% create a grid of interpolated values
Z = griddata(X, Y, Z1 - Z2, X, Y, 'cubic');

% plot the interpolated values on a surface plot
surf(X, Y, Z);
481 chars
19 lines

In this example, we first define a grid of x and y coordinates using the linspace and meshgrid functions. We then define two functions, f1 and f2, which we evaluate at each point on the grid to get Z1 and Z2.

We then subtract Z2 from Z1 to get the difference between the two functions, and use the griddata function to interpolate this difference across the entire grid.

Finally, we plot the interpolated values on a surface plot using the surf function. The resulting plot will show the difference between the two functions as a surface.

related categories

gistlibby LogSnag