3d plot to latex in matlab

To plot a 3D plot in MATLAB and include it in LaTeX, you can use either the surf or the mesh function.

Here is an example code to plot a function of two variables and include it in LaTeX:

main.m
% Define the function to plot
[x,y] = meshgrid(-3:0.2:3);
z = sin(sqrt(x.^2 + y.^2))./(sqrt(x.^2 + y.^2));

% Plot the surface
surf(x,y,z);

% Add labels and title
title('3D plot of $z=\frac{\sin(\sqrt{x^2+y^2})}{\sqrt{x^2+y^2}}$','Interpreter','latex');
xlabel('x');
ylabel('y');
zlabel('z');

% Set the axis limits
xlim([-3 3]);
ylim([-3 3]);
zlim([-1 1]);

% Export to LaTeX
matlab2tikz('my_3d_plot.tex', 'height', '\figureheight', 'width', '\figurewidth', 'showInfo', false );
481 chars
21 lines

The code generates a surface plot of the function z=sin(sqrt(x^2+y^2))/(sqrt(x^2+y^2)), adds labels and title, sets the axis limits, and exports it to LaTeX using the matlab2tikz function.

To include the plot in LaTeX, simply add the following code to your LaTeX document:

\begin{figure}[htb]
    \centering
    \setlength\figureheight{6cm}
    \setlength\figurewidth{6cm}
    \input{my_3d_plot.tex}
    \caption{3D plot of $z=\frac{\sin(\sqrt{x^2+y^2})}{\sqrt{x^2+y^2}}$}
    \label{fig:my_3d_plot}
\end{figure}
240 chars
9 lines

This code imports the my_3d_plot.tex file generated by MATLAB, sets the figure height and width, and adds a caption and label to the plot.

related categories

gistlibby LogSnag