# Vmls - sum of vectors, displacement

**URL:** <https://discourse.julialang.org/t/vmls-sum-of-vectors-displacement/93779>\
**Category:** Numerics\
**Tags:** linear-algebra\
**Created:** [January 30, 2023, 3:58pm UTC](https://discourse.julialang.org/t/vmls-sum-of-vectors-displacement/93779 "2023-01-30T15:58:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [January 30, 2023, 3:58pm UTC](https://discourse.julialang.org/t/vmls-sum-of-vectors-displacement/93779/1 "2023-01-30T15:58:54Z")

</div>

Hello, good afternoon.

I’m reading the VMLS book introduction about algebra linear and trying to draw the vector displacement in a plot. When sum two vectors a and b, the a+b vector dont need to connect to the end of b as shown in the book?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/2/2255f9158db0073d13a13e4d9faeea12939ae800.png)

In my experiments the a+b vector crosses the b vector

```julia
function sumsdisp2(a::Vector, b::Vector)
    if (length(a) != 2) || (length(b) != 2)
        error("This function only works with 2d vectors");
    end
    ab = a + b;
    plot([0, a[1]], [0, a[2]], arrow=true, label="a");
    plot!([a[1], b[1]], [a[2], b[2]], arrow=true, label="b");
    plot!([0, ab[1]], [0, ab[2]], arrow=true, label="a+b");
end

```

![image](https://global.discourse-cdn.com/julialang/original/3X/4/1/4152e199ca9f4a3cf158738581e3779e941b3ace.png)  
What Im doing wrong?

---

<div class="post-metadata">

**Author:** ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Post date:** [January 30, 2023, 4:28pm UTC](https://discourse.julialang.org/t/vmls-sum-of-vectors-displacement/93779/2 "2023-01-30T16:28:13Z")

</div>

I recommend working through a single case by hand  
starting with a, b, and calculating a+b. Either of the  
cases in the image you show would work.

Then determine what the plot command would be  
to display each vector. It doesn’t look like any of the  
vectors are being plotted correctly.

Can you reproduce the top left example specifically?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 30, 2023, 4:33pm UTC](https://discourse.julialang.org/t/vmls-sum-of-vectors-displacement/93779/3 "2023-01-30T16:33:31Z")

</div>

I’m not sure how you produced your example plot (I can’t get your output from various input vectors), but here’s how to do it with `quiver`, which is the Plots function for plotting vector fields:

```julia
julia> a = [1, 2]; b = [1, 0];

julia> a + b
2-element Vector{Int64}:
 2
 2

julia> quiver([0], [0], quiver = ([a[1]], [a[2]]))

julia> quiver!([a[1]], [a[2]], quiver = ([b[1]], [b[2]]))

julia> quiver!([0], [0], quiver = ([a[1] + b[1]], [a[2] + b[2]]))

```

It looks like in your example you added the result of `a + b` to `a`, rather than adding `b` (you get to `[3, 4]` which is `[1, 2] + [2, 2]` instead of `[1, 2] + [1, 0]` which I think is what you were trying to do)

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [January 30, 2023, 4:40pm UTC](https://discourse.julialang.org/t/vmls-sum-of-vectors-displacement/93779/4 "2023-01-30T16:40:32Z")

</div>

The book doesn’t give the vectors values, only the values. Then I’m trying to reproduce ploting, but have another thing that im not seeing in the process.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [January 30, 2023, 4:50pm UTC](https://discourse.julialang.org/t/vmls-sum-of-vectors-displacement/93779/5 "2023-01-30T16:50:10Z")

</div>

It worked. I’ll read more about quiver function in documentation. But what I notice is quiver maintains the vector magnitude when ploting above the origin (0, 0). Thank you.

![image](https://global.discourse-cdn.com/julialang/original/3X/f/1/f13c9a671a8e6634e8ebb8ed4990c8c061664484.png)

---

<div class="post-metadata">

**Author:** ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Post date:** [January 30, 2023, 7:52pm UTC](https://discourse.julialang.org/t/vmls-sum-of-vectors-displacement/93779/6 "2023-01-30T19:52:33Z")

</div>

From the graph: a = (5,1), b = (-1,3), and a+b = (4,4)
