Hi Teerapong,
In order to generate a curvature contour line to your 3D implicit surface plot in MATLAB, you will need to generate a grid of points over which to evaluate the curvature and then use the contour3 function to overlay curvature contours on the surface plot.
The following code tries to achieve the same:
Schwarz = @(x,y,z) cos(x) + cos(y) + cos(z);
% Define symbolic variables
syms x y z
f = cos(x) + cos(y) + cos(z);
fx = diff(f, x);
fy = diff(f, y);
fz = diff(f, z);
fxx = diff(fx, x);
fxy = diff(fx, y);
fxz = diff(fx, z);
fyy = diff(fy, y);
fyz = diff(fy, z);
fzz = diff(fz, z);
% Calculate curvature k
mat = [fxx fxy fxz fx; fxy fyy fyz fy; fxz fyz fzz fz; fx fy fz 0];
no = det(mat);
de = (fx^2 + fy^2 + fz^2)^2;
k = de / no;
% Convert symbolic expression to function handle
k_func = matlabFunction(k, 'Vars', [x, y, z]);
% Define a 2D grid on a specific plane, e.g., z = 0
zSlice = 0; % Choose a constant value for z
[xGrid, yGrid] = meshgrid(linspace(-pi, pi, 100));
% Evaluate curvature over the 2D grid
kValues = k_func(xGrid, yGrid, zSlice * ones(size(xGrid)));
% Plot the implicit surface
figure;
fimplicit3(Schwarz, [-pi, pi, -pi, pi, -pi, pi]);
hold on;
% Plot curvature contours on the z = 0 plane
contour3(xGrid, yGrid, zSlice * ones(size(xGrid)), kValues, 10, 'LineWidth', 1.5);
xlabel('x');
ylabel('y');
zlabel('z');
title('Surface with Curvature Contours on z = 0');
colorbar;
hold off;
The documentation for the contour3 function can be found at: https://www.mathworks.com/help/matlab/ref/contour3.html
Hope this helps