# Plotting vector fields in Octave in 2D

The adjoining figure shows a representation of vector field
f(x,y )= < -x/√(x² +y²+4),y/√(x² + y² + 4) >

I tried to plot the above vector field in Octave. Let us see what happened by looking at the following commands and figure.

% Define limits on x and y
x = -2:0.2:2;
y = -2:0.2:2;

[X,Y] = meashgrid(x,y);
error: ‘meashgrid’ undefined near line 1, column 9
[X,Y] = meshgrid(x,y);
Fx1 = X;
Fy1 = Y;
figure(1);
quiver(X,Y,Fx1,Fy1);

Fx2 = -X/sqrt(X.^2 + Y.^2 +4);
warning: matrix singular to machine precision
Fx2 = -X/sqrt(X.^2 + Y.^2 +4).;
error: parse error:

syntax error

Fx2 = -X/sqrt(X.^2 + Y.^2 +4).;
^
Fx2 = -X/sqrt(X.^2 + Y.^2 +4);
warning: matrix singular to machine precision
Fy2 = Y/sqrt(X.^2 + Y.^2 + 4);
warning: matrix singular to machine precision
figure(2);
quiver(X,Y,Fx2,Fy2);

You will notice the difference between the figure given by author and my uploaded figure.

What is wrong in the plotting the vector fields?

Would any member explain me?

You probably want element-wise division instead of matrix division.
Try replacing the `/` operator with the `./` operator.

2 Likes

As per you suggestion, I changed this program. But why my figure still don’t match with that of author’s figure?

Fx2 = -X./sqrt(X.^2 + Y.^2 +4);
Fy2 = Y./sqrt(X.^2 + Y.^2 + 4);
figure(2);
quiver(X,Y,Fx2,Fy2);
My figure:

Author’s figure:

I think the author’s figure and my figure is same.

Since a vector has no position, author indicated a vector field in graphical form by placing the vector f(x,y) with its tail at (x,y). But the octave did reverse. Octave indicated a vector field in graphical form by placing the vector f(x,y) with its face at (x,y).

Am I correct?

if you do a simple test with
`quiver(0,1,2,3)`

you will see a single arrow with it’s base located at (x,y) = [0,1], with magnitude (2,3), meaning it’s tip ends at (0+2,1+3) = (2,4)

1 Like

I get the same quiver field as you. f(1,0) = <-1/sqrt(5), 0>, whereas his arrows indicate a postitive x-direction at (1,0). therefore the plot shown must be some other scaled version of the simple quiver field (his answer shows -f(x,y) as you’ve defined it). note that it is fairly common to define a potential vector field as -grad(f)

1 Like

It would look more like the original if you added the command

axis equal

after the quiver() command

true, but I think the reversed arrow directions were the primary concern.