Vector-valued functions in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter jack476
  • Start date Start date
  • Tags Tags
    Functions Matlab
Click For Summary

Discussion Overview

The discussion revolves around creating a vector field plot in MATLAB using vector-valued functions. Participants explore the implementation of a force field defined by functions p(x, y) and q(x, y), addressing issues related to plotting and function definitions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks assistance in plotting a vector field defined by a function F(x, y) = p(x, y)i + q(x, y)j, encountering errors related to function handles in MATLAB.
  • Another participant suggests using the QUIVER function for plotting vector fields and provides a link to the MATLAB documentation.
  • A follow-up post indicates that the documentation page is not helpful, as it results in a blank page and a warning about a singular matrix.
  • Another participant advises redefining vectors for x, y, p, and q to ensure proper plotting of arrows at specified coordinates and requests to see the code for debugging purposes.
  • A participant shares a code snippet for generating the force field, referencing a specific equation from a physics document, but does not achieve the desired output.
  • Several suggestions are made regarding the code, including the need to use elementwise division, adjusting the meshgrid to avoid excessive points, and simplifying the definition of functions u and v to maintain vectorization.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to implement the vector field plot, with no consensus reached on the optimal solution or the interpretation of error messages.

Contextual Notes

Participants note limitations related to the use of function handles, the density of points generated by meshgrid, and the need for elementwise operations in MATLAB, which remain unresolved.

jack476
Messages
327
Reaction score
124
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   Reactions: Greg Bernhardt

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K