Math 2680 Differential Equations, Spring 2014

You must be logged in to reply to this topic.

  • Direction fields in Matlab
  • #13658

    Suman Ganguli
    Participant

    Here is the code I put up today for creating the direction field in Matlab for v’ = 9.8 – (v/5)

    (as shown shown in Sec 1.1, Fig 1.1.2 in Boyce/DiPrima):

    >>[t,v]=meshgrid(0:1:10,40:1:60)
    >>dv = 9.8 – (v/5)
    >>dt = ones(size(dv))
    >>quiver(t,v,dt,dv)

    So to create other direction fields you’ll just need to change the line for the differential equation, and also change the bounds of graph in the meshgrid command.

    Here’s an explanation of the various commands, adapted from pp7-9 http://www.math.tamu.edu/~efendiev/math442_spring04/matlabode.pdf (which I highly recommend you read):

    create a meshgrid:

    >>[x,y]=meshgrid(a:k:b, c:j:d)

    creates a set of points (x; y), where x lies between a and b, incremented by k, and y lies between c and d, incremented by j (so this is where you set the bounds of the section of the coordinate plane for which you will draw the direction field)

    Example:
    >>[x,y]=meshgrid(1:.5:2,0:1:2)

    creates the set of nine points: (1; 0), (1; 1),(1; 2), (1:5; 0), (1:5; 1), (1:5; 2), (2; 0), (2; 1), (2; 2)

    quiver():

    >>quiver(a,b,x,y)

    plots an arrow at (a; b) in the direction of the vector v = (x; y)

    Example: quiver(0,0,1,1) begins at the point (0; 0) and draws an arrow inclined 45 degrees from the horizontal

    Recall: vector v = (x; y) has slope y/x

    So quiver(x, y, dx, dy) begins at the point (x; y) and draws a vector with slope dy/dx

    size(A) returns the dimensions of the matrix A

    ones(m,n) creates a matrix with m rows and n columns with a 1 in each entry

    direction field for y'(x) = x + sin(y) :

    >>[x,y]=meshgrid(-3:.3:3,-2:.3:2);
    >>dy = x + sin(y);
    >>dx=ones(size(dy));
    >>quiver(x,y,dx,dy)

    The expression dx=ones(size(dy)) may become more clear if you think of our ODE as

    dy/dx = f(x,y)/1

    Optional–normalize length of vectors:

    >>[x,y]=meshgrid(-3:.3:3,-2:.3:2);
    >>dy = x + sin(y);
    >>dx=ones(size(dy));
    >>dyu = dy./sqrt(dx.^2+dy.^2);
    >>dxu = dx./sqrt(dx.^2+dy.^2);
    >>quiver(x,y,dxu,dyu)

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.