Ask Your Question
2

Projection of vector

asked 2016-07-22 14:45:26 +0200

etb gravatar image

updated 2016-07-22 16:28:20 +0200

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 15:44:52 +0200

tmonteil gravatar image

updated 2016-07-22 23:50:20 +0200

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 14:24:05 +0200 )edit

Nice picture, thanks !

tmonteil gravatar imagetmonteil ( 2016-07-24 23:50:35 +0200 )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 10:55:21 +0200 )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 11:47:07 +0200 )edit

Much belated note to @etb - you don't have to have an account to use the Sage cell server, you can just type right into it!

kcrisman gravatar imagekcrisman ( 2020-08-07 15:29:56 +0200 )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 14:45:26 +0200

Seen: 2,225 times

Last updated: Jul 22 '16