How can I get curvature contour

5 ビュー (過去 30 日間)
Teerapong Poltue
Teerapong Poltue 2020 年 11 月 4 日
回答済み: Amish 2025 年 2 月 5 日 10:39
I got my surface plot from
Schwarz = @(x,y,z) cos(x) + cos(y) + cos(z);
fimplicit3(Schwarz);
and now I would like to add my own contour line
which created base on curvature (k).
which k can be calculated by (the function of x, y, z)
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);
fyx = diff(fy,x);
fyy = diff(fy,y);
fyz = diff(fy,z);
fzx = diff(fz,x);
fzy = diff(fz,y);
fzz = diff(fz,z);
mat = [fxx fxy fxz fx; fyx fyy fyz fy; fzx fzy fzz fz; fx fy fz 0];
no = det(mat);
de = (fx^2 + fy^2 + fz^2)^2;
k = de/no;
how can I add my curvature contour line ?

回答 (1 件)

Amish
Amish 2025 年 2 月 5 日 10:39
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

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by