Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Vector-valued functions in MATLAB

  1. May 4, 2015 #1
    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.
     
  2. jcsd
  3. May 4, 2015 #2

    kreil

    User Avatar
    Gold Member

  4. May 4, 2015 #3
    It just gives me a blank page and a warning
    "Matrix is singular to within working precision." What does this mean?
     
  5. May 4, 2015 #4

    kreil

    User Avatar
    Gold Member

    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.
     
  6. May 4, 2015 #5
    Code (Text):

    %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 [Broken]
     
    Last edited by a moderator: May 7, 2017
  7. May 4, 2015 #6

    kreil

    User Avatar
    Gold Member

    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 (Text):

    %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)
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Vector-valued functions in MATLAB
Loading...