How to plot gradients ?

조회 수: 3 (최근 30일)
qweasd
qweasd 2020년 10월 1일
편집: Niklas Kurz 2022년 3월 13일

채택된 답변

Star Strider
Star Strider 2020년 10월 1일
  댓글 수: 14
Star Strider
Star Strider 2021년 9월 29일
Yes, it is!
x = linspace(3, 4, 25);
y = linspace(5, 6, 25);
[X,Y] = meshgrid(x,y);
z = @(x,y) x.^2+y.^5+3*x+4*y;
Z = z(X,Y);
[px,py] = gradient(Z);
figure
contour(X,Y,Z)
hold on
quiver(X,Y,px,py)
hold off
.
Niklas Kurz
Niklas Kurz 2022년 3월 13일
편집: Niklas Kurz 2022년 3월 13일
I would opt to normalise the Arrows for better scaling:
Norm = sqrt(px.^2+py.^2)
quiver(X,Y,px./Norm,py./Norm,0.5)
Otherwise the code is pretty neat!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by