How to plot gradients ?

4 views (last 30 days)
qweasd
qweasd on 1 Oct 2020
Edited: Niklas Kurz on 13 Mar 2022

Accepted Answer

Star Strider
Star Strider on 1 Oct 2020
  14 Comments
Star Strider
Star Strider on 29 Sep 2021
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 on 13 Mar 2022
Edited: Niklas Kurz on 13 Mar 2022
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!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!