Ask Your Question
2

Projection of vector

asked 2016-07-22 07:45:26 -0500

etb gravatar image

updated 2016-07-22 09:28:20 -0500

I'm working to reproduce an example made in GeoGebra* using Sage. What I got in GeoGebra looks like this, image description

  • would add link if I could …

The goal is to find the length of _j_ only knowing _A_, _B_, and _C_.

Here's where I'm at in Sage, it seems really smart, but all help files and templates I find online feels way too advanced for where I'm currently at. I've written the code below and it's currently look like this, image description

code (thanks to @tmonteil I've been able to update my code with some calculations, I am still struggling to produce the plot),

A = (9, 5); B = (2, 4); C = (16, -2);
AB = vector(B)-vector(A)
BC = vector(C)-vector(B)
P = plot(x,(x,2,5), color='red')
P += AB.plot(color='green', start=A)
P += BC.plot(color='green', start=B)
print "Length of AB proj. onto BC"
show(P, figsize=5, aspect_ratio=1)

# show(AB.inner_product(BC)/BC.norm()^2*v2)
show(AB.inner_product(BC)/BC.norm()) 
RDF(AB.inner_product(BC)/BC.norm())
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2016-07-22 08:44:52 -0500

tmonteil gravatar image

updated 2016-07-22 16:50:20 -0500

Your approach will not work since you try to play with plots. Instead, you have to play with vectors, not their plots.

To my understanding of the picture, D is the orthogonal projection of A to the line (B,C).

If v1 denotes the vector BA and v2 denotes the vector BC, the length j is the inner product of v1 by v2 divided by the norm of v2. Here is how to do it in Sage:

sage: A = (9, 5); B = (2, 4); C = (16, -2);
sage: v1 = vector(A)-vector(B)
sage: v2 = vector(C)-vector(B)
sage: v1.inner_product(v2)/v2.norm()
23/29*sqrt(58)

If you want a floating approximation, you can do:

sage: RDF(v1.inner_product(v2)/v2.norm())
6.040095911547238

EDIT : When you write

sage: plot(x,(x,2,5),color='red')

You ask Sage to plot the function x (i.e. the identity function), for x varying from 2 to 5.

To compute the coordinates of D, you can do:

sage: BA = -AB
sage: D = vector(B) + BC*BA.inner_product(BC)/BC.norm()/BC.norm()
sage: D
(219/29, 47/29)

To add the point D on the picture, you can do:

sage: P += point(D)

If you want the x-axis and the y-axis to have the same scale (so that the right angles are visible), you can do:

sage: P.set_aspect_ratio(1)
edit flag offensive delete link more

Comments

2

You may look at an edited version of your code here.

ndomes gravatar imagendomes ( 2016-07-24 07:24:05 -0500 )edit

Nice picture, thanks !

tmonteil gravatar imagetmonteil ( 2016-07-24 16:50:35 -0500 )edit

@ndomes, thanks! That is amazing! If you add it as an answer I'll be happy to mark it answering my question!

etb gravatar imageetb ( 2016-07-25 03:55:21 -0500 )edit

@ndomes, how did you create this page http://sagecell.sagemath.org/?q=dgweis do you have an account somewhere or? I am interested to share some sage code online.

etb gravatar imageetb ( 2016-09-28 04:47:07 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-07-22 07:45:26 -0500

Seen: 224 times

Last updated: Jul 22 '16