For more information on surf function,please refer to

# How i can combine two or three 3D plot ? how get the template of such graph?

##### 3 Comments

### Answers (1)

Hi @salim saeed,

First define the range for the variables x and t using linspace, which generates linearly spaced vectors. The meshgrid function is then used to create a grid of X and T values. The core mathematical expression is defined in the variable Z. This expression involves hyperbolic cosine functions and an exponential term, which is computed over the grid defined by X and T. The, use surf function to create a 3D surface plot of the real part of Z. The plot will be enhanced with titles and labels for clarity, and a color bar is added to indicate the scale of values. To combine multiple 3D graphs, use the hold on command, which allows us to overlay additional plots on the same figure. A second mathematical expression Z2 is defined and plotted with a specified transparency using the FaceAlpha property. This will help in visualizing both surfaces simultaneously without obscuring one another. Finally, a legend is added to distinguish between the two expressions, and the hold off command is used to finalize the plotting.

% Define the range for x and t x = linspace(-10, 10, 100); t = linspace(-10, 10, 100); [X, T] = meshgrid(x, t);

% Define the mathematical expression Z = ((cosh(0.5 * X + 20 * T) + 1) ./ cosh(0.5 * X + 20 * T)).^(1 / 6) .* exp(1i * (-2 * X - 40.34722222 * T));

% Plot the real part of Z figure; surf(X, T, real(Z)); title('3D Surface Plot of the Given Expression'); xlabel('X-axis'); ylabel('T-axis'); zlabel('Real Part of Z'); colorbar; grid on;

% Combine multiple 3D graphs hold on;

% Define another mathematical expression for demonstration Z2 = ((cosh(0.3 * X + 15 * T) + 1) ./ cosh(0.3 * X + 15 * T)).^(1 / 6) .* exp(1i * (-1.5 * X - 30 * T));

% Plot the real part of the second expression surf(X, T, real(Z2), 'FaceAlpha', 0.5); % Set transparency for better visualization

% Add legend and title legend('Expression 1', 'Expression 2'); title('Combined 3D Surface Plots'); hold off;

Please see attached.

Please let me know if you have any further questions.

##### 5 Comments

Hi @salim saeed,

To achieve the desired effect of displaying both a 3D surface plot and a 2D contour plot in MATLAB, you can use the surf function for the 3D surface and the contour or contourf function for the 2D representation. Here is a step-by-step guide:

*Define Your Functions:* First, ensure you have your function defined for both the surface and contour plots. For example, let’s say we have a function (z = f(x, t) ).

% Define the function z = @(x, t) sin(sqrt(x.^2 + t.^2)); % Example function

*Create a Meshgrid:* Use meshgrid to create matrices for your x and t values over which you will evaluate your function.

[X, T] = meshgrid(-5:0.1:5, -5:0.1:5); % Create grid for x and t Z = z(X, T); % Compute Z values

*Plot the 3D Surface:* Use the surf function to create your 3D plot.

figure; surf(X, T, Z); % Create surface plot hold on; % Retain current plot when adding new plots

*Overlay the Contour Plot:* Use the contour3 function to add contours on top of your surface plot. The contour3 function creates contour lines in 3D space.

contour3(X, T, Z, 20, 'k'); % Overlay contour lines (20 levels) hold off; % Release current plot hold

For more information on contour3 function, please refer to

https://www.mathworks.com/help/matlab/ref/contour3.html

Enhance Visualization: You may want to add labels and titles to make your graph more informative.

title('3D Surface with Contours'); xlabel('X-axis'); ylabel('T-axis'); zlabel('Z-axis');

You can customize the number of contour levels by changing the number in contour3(X, T, Z, N) where N is the number of levels you desire. Consider using colormaps (`colormap`) to enhance visual distinction between different levels of contours. MATLAB also allows for interactive features such as rotating the plot (`view`) and zooming in on specific areas to better analyze data.

By following these steps, you can effectively overlay a contour plot on top of a 3D surface plot in MATLAB, allowing for an enriched visualization that conveys more information about their data.

**Note: We are not allowed to share personal information at Mathworks forum. If you have any questions, please post them on the forum, we will be happy to help you out.**

If there are further questions or if additional clarification is needed on specific aspects of this process, please feel free to ask!

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!