How to Scale Gradient Field for large z-values?

조회 수: 4 (최근 30일)
Niklas Kurz
Niklas Kurz 2021년 4월 29일
댓글: Niklas Kurz 2024년 5월 12일
I want to plot a function R^2 -> R with gradient Field beneath:
f2 = @(x,y) 1./sqrt(x.^2+y.^2);
[u2,v2] = meshgrid(-1:0.01:1);
[du2,dv2] = gradient(f2(u2,v2));
s = surf(u2,v2,f2(u2,v2));
hold on
contour(u2,v2,f2(u2,v2))
hold on
norm = 1./sqrt(du2.^2+dv2.^2);
quiver(u2,v2,du2./norm,dv2./norm,'LineWidth',2)
axis([-1 1 -1 1 0 10])
caxis([0,10])
colormap(cool)
alpha(s,0.95)
shading flat
Sadly the gradient field is not visible. Probably because it's too small, f2 get's too large and I'm lacking of the mathmatical knowledge to adjust it properly

채택된 답변

Anurag Ojha
Anurag Ojha 2024년 5월 8일
Hello Niklas
One way to adjust it is by normalizing the gradient vectors before plotting them.
Here's an updated version of your code:
f2 = @(x,y) 1./sqrt(x.^2+y.^2);
[u2,v2] = meshgrid(-1:0.01:1);
[du2,dv2] = gradient(f2(u2,v2));
% Normalize the gradient vectors
norm = sqrt(du2.^2+dv2.^2);
du2_norm = du2./norm;
dv2_norm = dv2./norm;
s = surf(u2,v2,f2(u2,v2));
hold on
contour(u2,v2,f2(u2,v2))
hold on
quiver(u2,v2,du2_norm,dv2_norm,'LineWidth',2)
axis([-1 1 -1 1 0 10])
caxis([0,10])
colormap(cool)
alpha(s,0.95)
shading flat
This code normalizes the gradient vectors by dividing the du2 and dv2 components by their magnitude (norm). This ensures that the length of each vector is 1, making them visible in the plot.
  댓글 수: 1
Niklas Kurz
Niklas Kurz 2024년 5월 12일
Thanks for cleaning up open questions like this one that I have asked long ago x) Today I'd probably know better and woud have done it similar to you.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by