MATLAB: How to subtract a surface equation from a surface ‘scatter plot’

different surface types

Hi guys,
I have a somewhat complex question. Simply stated, I have two surfaces, and I want to create a third surface by subtracting one of the two surfaces with the other one. So I want to subtract surfaceA from surfaceB to come up with surfaceC.
But the issue is that surfaceA was created from actual data points, and surfaceB was created from an equation.
How do I create surfaceC given that surfaceA and surfaceB were created differently and are of different type?
Please see my sample code and evaluate my attempt. What do I need to do differently?
Thank you!
%Creating surfaceA from datapoints
Xval = [1 2 3 1 2 3 1 2 3];
Yval = [3 1 2 3 1 2 3 1 2];
Zval = [4 10 5 3 4 6 4 3 2];
xvalue = reshape(Xval,3,3)';
yvalue = reshape(Yval,3,3)';
zvalue = reshape(Zval,3,3)';
%surfaceA graphed
figure(1)
surf(xvalue,yvalue,zvalue);
hold off
%Creating surfaceB from equation
x = [1:.5:3];
y = [1:.5:3];
Test = @(x,y)x+y;
[X,Y] = meshgrid(x,y);
Z1 = Test(X,Y);
%surfaceB graphed
figure(2)
surf(X, Y, Z1);
%Both surfaceA and surfaceB graphed together
figure(3)
surf(xvalue,yvalue,zvalue);
hold on
surf(X, Y, Z1);
hold off
%%%Attempting to graph surfaceC%%%
%figure(4)
%surf(X,Y,zvalue-Z1);
%or
%surf(xvalue,yvalue,zvalue-Z1);

Best Answer

Simple.
dZ=zvalue-Test(xvalue,yvalue);
Just evaluate the equation at the existing points and compute the difference.
Or, you could use interp2 to interpolate the original to the same mesh as you used to create the first calculated function. The examples for interp2 illustrate that use, iirc.
Related Question