Vector-valued functions in MATLAB

In summary, the conversation discusses the creation of a vector field plot of an equation in x and y. The desired function is F(x, y) = p(x, y)i + q(x, y)j, which defines a force field with direction and magnitude plotted at points in the x-y plane. The components of the vector are defined by scalar functions p and q. The individual has tried to define the functions and pass their values into the vector, but has encountered errors. They are seeking help with using the QUIVER function in MATLAB to create the plot, and have provided code and a resource for reference. The expert responds by providing a revised code that addresses the errors and offers suggestions for further exploration.
  • #1
jack476
328
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
  • #3
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?
 
  • #4
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.
 
  • #5
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:
  • #6
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

1. What is a vector-valued function in MATLAB?

A vector-valued function in MATLAB is a function that takes in one or more input variables and returns a vector as the output. The function can be defined using the "function" keyword and can accept both scalar and vector inputs.

2. How do I create a vector-valued function in MATLAB?

To create a vector-valued function in MATLAB, you can use the "function" keyword followed by the name of the function, the input variables in parentheses, and the output vector in square brackets. You can then write the body of the function using MATLAB syntax.

3. Can a vector-valued function in MATLAB have multiple outputs?

Yes, a vector-valued function in MATLAB can have multiple outputs. You can specify the outputs in square brackets after the input variables when defining the function. The outputs can be of different sizes and data types.

4. How do I call a vector-valued function in MATLAB?

To call a vector-valued function in MATLAB, you can simply use its name followed by the input variables in parentheses. If the function has multiple outputs, you can assign them to variables using square brackets.

5. What are some applications of vector-valued functions in MATLAB?

Vector-valued functions in MATLAB are commonly used in data analysis, signal processing, and image processing tasks. They can also be used to model physical systems and solve differential equations. Additionally, they are useful for creating plots and animations in MATLAB.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
829
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
999
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top