Reputation: 593
I would like to plot the gradient of the following function in MATLAB.
g(x,y) = [(x^2)-1; -y]
My code is:
x = linspace(-3,3);
y = linspace(-3,3);
[xx, yy] = meshgrid(x,y);
z = [xx.^2-1;-yy];
[dx,dy] = gradient(z,.3,.3);
contour(x,y,z)
hold on
quiver(x,y,dx,dy)
But I'm just getting this error:
The size of Y must match the size of Z or the number of rows
of Z.
I've no idea how I could make the size of both match. y
is a 1x100 matrix and z
a 200x100. To match them I would need y
to be a 1x200 or z
to be 100x100, but would I be able to plot it then?
Upvotes: 2
Views: 2444
Reputation: 341
Instead of
z = [xx.^2-1;-yy];
try each component separately:
z1 = [xx.^2-1];
z2 = [-yy];
[dx,dy] = gradient(z1,.3,.3);
contour(x,y,z1)
%etc.
Use hold on
again if you really want them in the same plot.
Upvotes: 2