# Vectors Addition only work with initial (0,0)?

**URL:** <https://discourse.julialang.org/t/vectors-addition-only-work-with-initial-0-0/85144>\
**Category:** General Usage\
**Tags:** question, plotting, plots\
**Created:** [August 2, 2022, 6:31am UTC](https://discourse.julialang.org/t/vectors-addition-only-work-with-initial-0-0/85144 "2022-08-02T06:31:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [August 2, 2022, 6:31am UTC](https://discourse.julialang.org/t/vectors-addition-only-work-with-initial-0-0/85144/1 "2022-08-02T06:31:21Z")

</div>

Hi all,

I have been learning from this site:  
[https://docs.juliahub.com/CalculusWithJulia/AZHbv/0.0.5/differentiable\_vector\_calculus/vectors.html#](https://docs.juliahub.com/CalculusWithJulia/AZHbv/0.0.5/differentiable_vector_calculus/vectors.html#)

then I change the initial point and see the second vector is not in the place it should be, needs to modify the code…

```julia
using CalculusWithJulia

using Plots

gr() # better arrows than plotly()
quiver([0],[0], quiver=([1],[2]))

unzip(vs) = Tuple(eltype(first(vs))[xyz[j] for xyz in vs] for j in eachindex(first(vs)))

u = [2, 1]
v = [-2, 1]
w = u + v
p = [1,1]
quiver(unzip([p])..., quiver=unzip([u])) # initial at (1,1) terminal at (3,2)
quiver!(unzip([u])..., quiver=unzip([v])) # initial at (2,1) terminal at (0,2)
quiver!(unzip([p])..., quiver=unzip([w])) # initial at (1,1) terminal at (1,3)

```

![Capture d’écran_2022-08-02_13-30-55](https://global.discourse-cdn.com/julialang/original/3X/4/6/469f8c493735affc473d050f67386db61b0987ed.png)

Is there any code that can work for any kind of vectors for all kind of initial points with addition operations?

Just realize and edit it, it will work if we edit the second vector with

```julia
quiver!(unzip([u+p])..., quiver=unzip([v])) # initial at (3,2) terminal at (1,3)

```

Feel free to share more efficient code for vectors addition

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 2, 2022, 7:37am UTC](https://discourse.julialang.org/t/vectors-addition-only-work-with-initial-0-0/85144/2 "2022-08-02T07:37:18Z")

</div>

Not sure about efficiency, but perhaps simpler:

```julia
P = [p p p]
V = [u v w]
C = repeat([:red,:green,:blue], inner=4)
quiver(P[1,:], P[2,:], quiver=(V[1,:],V[2,:]), c=C)

```

_ **NB:** cannot get rid of warning message related to NaNs inserted by quiver()_

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [August 2, 2022, 1:56pm UTC](https://discourse.julialang.org/t/vectors-addition-only-work-with-initial-0-0/85144/3 "2022-08-02T13:56:59Z")

</div>

> [@rafael.guerra](#):
>
> ```julia
> P = [p p p]
> V = [u v w]
> C = repeat([:red,:green,:blue], inner=4)
> quiver(P[1,:], P[2,:], quiver=(V[1,:],V[2,:]), c=C)
> 
> ```

It is better and simpler, I got this warning:  
**Warning: Indices Base.OneTo(12) of attribute `seriescolor` does not match data indices 2:15.**

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [August 2, 2022, 3:45pm UTC](https://discourse.julialang.org/t/vectors-addition-only-work-with-initial-0-0/85144/4 "2022-08-02T15:45:11Z")

</div>

The `CalculusWithJulia` package has an `arrow!` plot recipe that makes it a bit easier to plot a single vector anchored at a point `p` with length `v`. The example therein is:

```julia
u = [1, 2]
v = [4, 2]
w = u + v
p = [0,0]
plot(legend=false)
arrow!(p, u)
arrow!(u, v) # <-- plots v anchored at point u
arrow!(p, w)

```

This is a convenience for plotting 1 vector at a time and for 3-d vectors, as I’m not sure all backends for `Plots` handle 3-d arrows for Plots. For plotting many vectors at once, the use of quiver is likely significantly more efficient. As in the above (with slight modification and skipping the `seriescolor` flourish):

```julia
P = [p,u,p]
V = [u,v,w]
quiver(unzip(P)...; quiver=unzip(V))

```

(I prefer stacking the vectors differently from @ [rafael.guerra](https://discourse.julialang.org/u/rafael.guerra) allowing the use of `unzip` --which is basically `SplitApplyCombine.invert` here – to clean up the indexing.)

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [August 3, 2022, 5:51am UTC](https://discourse.julialang.org/t/vectors-addition-only-work-with-initial-0-0/85144/5 "2022-08-03T05:51:33Z")

</div>

Yes, as a matter of fact, today I am learning how to plot 3d vectors rightly,

Simple vector addition in 3 dimension like (1,0,0) + (0,1,0) + (0,0,1) will result in (1,1,1). Need to create the Julia code for this. I think I want to try CairoMakie because their plot is interactive and can be rotated to any angle.
