How do i subtract or sum two 3D plot in matlab?

How i do Sum or Subtract two or more 3D surface plot which is plotted from meshgrid data?

Answers (1)

Hi mahdi,
As per my understanding, you would like to add or subtract 3D surface plots which is plotted from meshgrid data, assuming meshgrid data is the data from “meshgrid” function.
You can add or subtract the surface plots only when 2D or 3D grid which returns from “meshgrid” function should have equal size for both surface plots.
Here is the code for adding and subtracting 3D surface plots, you can check:
% First surface plot
[X1,Y1] = meshgrid(-10:0.1:10);
plot(X1,Y1);
%sample function 1
Z1 = sin(X1) + cos(Y1);
subplot(2,2,1);
surf(X1,Y1,Z1);
title('Surface plot 1');
% second surface plot.
[X2,Y2] = meshgrid(-10:0.1:10);
%sameple function 2
Z2 = sin(X2) + sin(Y2);
subplot(2,2,2);
surf(X2,Y2,Z2);
title('Surface plot 2');
% addition of surfaces
Z_sum = Z1 + Z2;
subplot(2,2,3);
surf(X1,Y1,Z_sum);
%plotting the function over 2D grid
title('Surface plot 1 + 2');
% Subtraction of surfaces
Z_diff = Z1 - Z2;
subplot(2,2,4);
%plotting the function over 2D grid
surf(X1,Y1,Z_diff);
title('Surface plot 1 - 2');
For further reference, please check the following link to know more about “meshgrid” and “surf” function.
I hope this resolves the issue you were facing.

Asked:

on 17 May 2023

Answered:

on 8 Jun 2023

Community Treasure Hunt

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

Start Hunting!