How to plot gradients ?
3 次查看(过去 30 天)
显示 更早的评论
采纳的回答
Star Strider
2020-10-1
14 个评论
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
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
