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

Hi all,

I have been learning from this site:
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…

``````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)
``````

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

``````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

Not sure about efficiency, but perhaps simpler:

``````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()

1 Like

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

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:

``````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):

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

(I prefer stacking the vectors differently from @ rafael.guerra allowing the use of `unzip` --which is basically `SplitApplyCombine.invert` here – to clean up the indexing.)

1 Like

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.