MATLAB Vector-valued functions in MATLAB

  • Thread starter Thread starter jack476
  • Start date Start date
  • Tags Tags
    Functions Matlab
AI Thread Summary
The discussion centers on creating a vector field plot in MATLAB using a force field defined by the functions F(x, y) = p(x, y)i + q(x, y)j. The user encounters errors related to function handles and matrix singularities while attempting to plot the vector field using the QUIVER function. Key points include the need to use element-wise division (./) instead of regular division (/) to avoid singular matrix warnings. Additionally, the meshgrid function is generating too many points, which can overwhelm the plotting function. Suggestions include simplifying the code by directly operating on x and y without using anonymous functions and adjusting the meshgrid to create a more manageable range of points. The corrected code provided demonstrates these adjustments, leading to a successful vector field plot.
jack476
Messages
327
Reaction score
125
I'm trying to create a vector field plot of an equation in x and y.

Basically, I would like to create a function F(x, y) = p(x, y)i + q(x, y)j that defines a force field, and have the field direction and magnitude plotted at points in the x-y plane, and both components of the vector are defined by the scalar functions p and q.

What I have tried so far is to define the functions p and q and then pass their values into the vector, but that keeps throwing "conversion to double from function_handle is not possible" errors.

Any help with this would be greatly appreciated. I can't find anything in the documentation to help me get started, and I'm not that great with MATLAB to begin with.
 
Physics news on Phys.org
kreil said:
I believe what you're looking for is the QUIVER function:

http://www.mathworks.com/help/matlab/ref/quiver.html

Let me know if that doc page isn't clear enough!

It just gives me a blank page and a warning
"Matrix is singular to within working precision." What does this mean?
 
You need to redefine your vectors for x, y, p, and q, so that the first arrow is plotted at (x(1),y(1)) with components p(1) and q(1).

Can you show the code for what you've tried so far? It will be easier to debug that way.
 
Code:
%orbital period constant, just any scalar
omega = 1;

%object masses
m1 = 10;
m2 = 1;

%mass ratio factors
alpha = m2./(m1 + m2);
beta = m1./(m1 + m2);

%Radius
R = 10;%Force vector field function components
u = @(x, y) (omega.^2).*((x - (beta.*(x + alpha.*R).*R.^3)/(((x + alpha.*R).^2 +y.^2).^1.5))-(alpha.*(x-beta.*R).*R.^3)/(((x - beta.*R).^2 +y.^2).^1.5));
v = @(x, y) (omega.^2).*((y - (beta.*y.*R.^3)/(((x + alpha.*R).^2 +y.^2).^1.5))-(alpha.*y*R.^3)/(((x - beta.*R).^2 +y.^2).^1.5));

[x, y] = meshgrid(-20:0.01:20, -20:0.01:20);

quiver(u(x, y), v(x, y));

I am trying to generate the force field from the FΩ equation on page 3 of http://www.physics.montana.edu/faculty/cornish/lagrange.pdf
 
Last edited by a moderator:
Several things:

1. You get the "matrix is singular" warning because you're using / instead of ./

I noticed you were careful to use the elementwise operators in other cases, so you just need to use ./ as well.

2. The call to meshgrid produces too fine of a mesh. You're generating 4,000 points in each dimension -- which means quiver will try to draw 16 million arrows and probably crash your computer (like it did mine).

3. You don't need to make u and v anonymous functions. You can just directly operate on x and y to keep everything vectorized. To do this you need to call meshgrid beforehand, though.Try this instead. You might want to play with it more, as the magnitude of the arrows varies greatly if you use a larger domain. This was the first scale where I got a reasonable output so I figured it's a good starting point.

Code:
%orbital period constant, just any scalar
omega = 1;

%object masses
m1 = 10;
m2 = 1;

%mass ratio factors
alpha = m2./(m1 + m2);
beta = m1./(m1 + m2);

%Radius
R = 10;

[x, y] = meshgrid(-0.5:0.01:0.5);

%Force vector field function components
u = (omega.^2).*((x - (beta.*(x + alpha.*R).*R.^3)./ ...
    (((x + alpha.*R).^2 +y.^2).^1.5))-(alpha.*(x-beta.*R).*R.^3)./ ...
    (((x - beta.*R).^2 +y.^2).^1.5));
v = (omega.^2).*((y - (beta.*y.*R.^3)./ ...
    (((x + alpha.*R).^2 +y.^2).^1.5))-(alpha.*y*R.^3)./ ...
    (((x - beta.*R).^2 +y.^2).^1.5));

quiver(x,y,u,v)
 
  • Like
Likes Greg Bernhardt

Similar threads

Replies
2
Views
3K
Replies
4
Views
1K
Replies
1
Views
5K
Replies
4
Views
2K
Replies
1
Views
4K
Replies
2
Views
3K
Back
Top