#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    How to draw the resultant vector


    I have a project in which I graph the resultant vector from a set of vectors. The goal is first identify the two major vectors module and then plot the resultant between them. To identify which of all vectors have the most module, I use a list in which I keep all the values, then I order it, invert it and I take the first 2 values​​. The problem is that sometimes one of the vectors of greater module may be a negative one like eg-8) so it's not taken into account. Attached the code to make my point clearer.

    Code:
    import matplotlib
    import matplotlib.pyplot as plt
    import matplotlib.collections as collections
    import matplotlib.ticker as ticker
    from math import sqrt, atan
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    rect = matplotlib.patches.Rectangle((-4,-4), 8, 8, facecolor="blue", edgecolor="k", linewidth=4.0, alpha = 150)
    ax.add_patch(rect)
    ax.scatter([0],[0],color="k",s=200)
    
    ax = plt.axes()
    a=8
    b=6
    c=-3
    d=-4
    ax.arrow(0, 0, a, 0, head_width=1, head_length=1, width=0.2, length_includes_head=True, fc='c', ec='c', alpha=80)
    ax.arrow(0, 0, 0, b, head_width=1, head_length=1, width=0.2, length_includes_head=True, fc='k', ec='k', alpha=80)
    ax.arrow(0, 0, c, 0, head_width=1, head_length=1, width=0.2, length_includes_head=True, fc='r', ec='r', alpha=80)
    ax.arrow(0, 0, 0, d, head_width=1, head_length=1, width=0.2, length_includes_head=True, fc='m', ec='m', alpha=80)
    
    lista = [a,b,c,d]
    lista = [ abs(i) for i in lista]
    lista.sort()
    lista.reverse()
    lista
    
    
    print lista
    
    ax.arrow(0, 0, lista[0], lista[1], head_width=1, head_length=1, width=0.2, length_includes_head=True, fc='c', ec='c', alpha=80)
    
    ax.annotate('TOP VIEW', xy=(-8, 8),
                    xycoords='axes points',
                    horizontalalignment='right', verticalalignment='bottom',
                    fontsize=20)
    
    patches = []
    plt.xlim([-10, 10])
    plt.ylim([-10, 10])
    
    p = collections.PatchCollection(patches)
    ax.add_collection(p)
    ax.xaxis.set_major_locator(ticker.MultipleLocator(20)) 
    ax.yaxis.set_major_locator(ticker.MultipleLocator(20))
    
    plt.show()
    How can I solve this problem?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481

    you can solve your trouble by learning what is a vector.


    In my opinion, ....

    You've taken a bunch of numbers and arbitrarily arranged them.

    Here's a vector of length 0.
    ohhhh
    Code:
    >>> import numpy
    >>> A = numpy.array([])  # a vector of length 0.
    >>> A.dot(A)  # the square of its length, which is good enough for sorting.
    0.0
    >>> B = numpy.array([-7])  # a vector of length 1
    >>> B.dot(B)  # the square of its length, which is good enough for sorting.
    49
    >>> E = numpy.array([-7,2,5,2,1])  # a vector of length 5
    >>> E.dot(E)  # the square of its length, which is good enough for sorting.
    83
    >>> C0 = numpy.array([6,8])    # vector of length 2
    >>> numpy.sqrt(C0.dot(C0))      # its surprising magnitude
    10.0
    >>> C1 = numpy.array([5,12])    # another vector of length 2
    >>> L = [C0, C1]                # a list of two vectors that happen to be length 2
    >>> L.sort(key = lambda x: x.dot(x))    # sort the list.
    >>> L
    [array([6, 8]), array([ 5, 12])]
    >>> [x.dot(x) for x in L]      # is it correct?  Yes.  We knew this by inspection.
    [100, 169]
    >>> RESULTANT = C0 + C1  # the resultant is the sum.
    >>> RESULTANT
    array([11, 20])
    >>>
    Last edited by b49P23TIvg; October 4th, 2013 at 06:23 PM. Reason: insert title
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    I dont understand your point about what I asked for


    Originally Posted by b49P23TIvg
    In my opinion, ....

    You've taken a bunch of numbers and arbitrarily arranged them.

    Here's a vector of length 0.
    ohhhh
    Code:
    >>> import numpy
    >>> A = numpy.array([])  # a vector of length 0.
    >>> A.dot(A)  # the square of its length, which is good enough for sorting.
    0.0
    >>> B = numpy.array([-7])  # a vector of length 1
    >>> B.dot(B)  # the square of its length, which is good enough for sorting.
    49
    >>> E = numpy.array([-7,2,5,2,1])  # a vector of length 5
    >>> E.dot(E)  # the square of its length, which is good enough for sorting.
    83
    >>> C0 = numpy.array([6,8])    # vector of length 2
    >>> numpy.sqrt(C0.dot(C0))      # its surprising magnitude
    10.0
    >>> C1 = numpy.array([5,12])    # another vector of length 2
    >>> L = [C0, C1]                # a list of two vectors that happen to be length 2
    >>> L.sort(key = lambda x: x.dot(x))    # sort the list.
    >>> L
    [array([6, 8]), array([ 5, 12])]
    >>> [x.dot(x) for x in L]      # is it correct?  Yes.  We knew this by inspection.
    [100, 169]
    >>> RESULTANT = C0 + C1  # the resultant is the sum.
    >>> RESULTANT
    array([11, 20])
    >>>
    Your reply doesn't solve my doubt!
    Actually, I'm working with variables but I think with an If condition I could solve this

IMN logo majestic logo threadwatch logo seochat tools logo