ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 07 Aug 2020 08:29:56 -0500Projection of vectorhttps://ask.sagemath.org/question/34190/projection-of-vector/ I'm working to reproduce an example made in GeoGebra* using Sage. What I got in GeoGebra looks like this,
![image description](http://i.stack.imgur.com/gLkEJ.png)
* 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](http://i.stack.imgur.com/8TOHN.png)
code (thanks to [@tmonteil](http://ask.sagemath.org/users/1305/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())
Fri, 22 Jul 2016 07:45:26 -0500https://ask.sagemath.org/question/34190/projection-of-vector/Answer by tmonteil for <p>I'm working to reproduce an example made in GeoGebra* using Sage. What I got in GeoGebra looks like this,
<img alt="image description" src="http://i.stack.imgur.com/gLkEJ.png"/></p>
<ul>
<li>would add link if I could …</li>
</ul>
<p>The goal is to find the length of _j_ only knowing _A_, _B_, and _C_.</p>
<p>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,
<img alt="image description" src="http://i.stack.imgur.com/8TOHN.png"/></p>
<p>code (thanks to <a href="http://ask.sagemath.org/users/1305/tmonteil/"><a href="/users/1305/tmonteil/">@tmonteil</a></a> I've been able to update my code with some calculations, I am still struggling to produce the plot),</p>
<pre><code>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())
</code></pre>
https://ask.sagemath.org/question/34190/projection-of-vector/?answer=34193#post-id-34193Your 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)
Fri, 22 Jul 2016 08:44:52 -0500https://ask.sagemath.org/question/34190/projection-of-vector/?answer=34193#post-id-34193Comment by kcrisman for <p>Your approach will not work since you try to play with plots. Instead, you have to play with vectors, not their plots.</p>
<p>To my understanding of the picture, <code>D</code> is the orthogonal projection of <code>A</code> to the line <code>(B,C)</code>.</p>
<p>If <code>v1</code> denotes the vector <code>BA</code> and <code>v2</code> denotes the vector <code>BC</code>, the length <code>j</code> is the inner product of <code>v1</code> by <code>v2</code> divided by the norm of <code>v2</code>. Here is how to do it in Sage:</p>
<pre><code>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)
</code></pre>
<p>If you want a floating approximation, you can do:</p>
<pre><code>sage: RDF(v1.inner_product(v2)/v2.norm())
6.040095911547238
</code></pre>
<p><strong>EDIT :</strong> When you write </p>
<pre><code>sage: plot(x,(x,2,5),color='red')
</code></pre>
<p>You ask Sage to plot the function <code>x</code> (i.e. the identity function), for <code>x</code> varying from <code>2</code> to <code>5</code>.</p>
<p>To compute the coordinates of <code>D</code>, you can do:</p>
<pre><code>sage: BA = -AB
sage: D = vector(B) + BC*BA.inner_product(BC)/BC.norm()/BC.norm()
sage: D
(219/29, 47/29)
</code></pre>
<p>To add the point <code>D</code> on the picture, you can do:</p>
<pre><code>sage: P += point(D)
</code></pre>
<p>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:</p>
<pre><code>sage: P.set_aspect_ratio(1)
</code></pre>
https://ask.sagemath.org/question/34190/projection-of-vector/?comment=52909#post-id-52909Much 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!Fri, 07 Aug 2020 08:29:56 -0500https://ask.sagemath.org/question/34190/projection-of-vector/?comment=52909#post-id-52909Comment by etb for <p>Your approach will not work since you try to play with plots. Instead, you have to play with vectors, not their plots.</p>
<p>To my understanding of the picture, <code>D</code> is the orthogonal projection of <code>A</code> to the line <code>(B,C)</code>.</p>
<p>If <code>v1</code> denotes the vector <code>BA</code> and <code>v2</code> denotes the vector <code>BC</code>, the length <code>j</code> is the inner product of <code>v1</code> by <code>v2</code> divided by the norm of <code>v2</code>. Here is how to do it in Sage:</p>
<pre><code>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)
</code></pre>
<p>If you want a floating approximation, you can do:</p>
<pre><code>sage: RDF(v1.inner_product(v2)/v2.norm())
6.040095911547238
</code></pre>
<p><strong>EDIT :</strong> When you write </p>
<pre><code>sage: plot(x,(x,2,5),color='red')
</code></pre>
<p>You ask Sage to plot the function <code>x</code> (i.e. the identity function), for <code>x</code> varying from <code>2</code> to <code>5</code>.</p>
<p>To compute the coordinates of <code>D</code>, you can do:</p>
<pre><code>sage: BA = -AB
sage: D = vector(B) + BC*BA.inner_product(BC)/BC.norm()/BC.norm()
sage: D
(219/29, 47/29)
</code></pre>
<p>To add the point <code>D</code> on the picture, you can do:</p>
<pre><code>sage: P += point(D)
</code></pre>
<p>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:</p>
<pre><code>sage: P.set_aspect_ratio(1)
</code></pre>
https://ask.sagemath.org/question/34190/projection-of-vector/?comment=34980#post-id-34980@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.Wed, 28 Sep 2016 04:47:07 -0500https://ask.sagemath.org/question/34190/projection-of-vector/?comment=34980#post-id-34980Comment by etb for <p>Your approach will not work since you try to play with plots. Instead, you have to play with vectors, not their plots.</p>
<p>To my understanding of the picture, <code>D</code> is the orthogonal projection of <code>A</code> to the line <code>(B,C)</code>.</p>
<p>If <code>v1</code> denotes the vector <code>BA</code> and <code>v2</code> denotes the vector <code>BC</code>, the length <code>j</code> is the inner product of <code>v1</code> by <code>v2</code> divided by the norm of <code>v2</code>. Here is how to do it in Sage:</p>
<pre><code>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)
</code></pre>
<p>If you want a floating approximation, you can do:</p>
<pre><code>sage: RDF(v1.inner_product(v2)/v2.norm())
6.040095911547238
</code></pre>
<p><strong>EDIT :</strong> When you write </p>
<pre><code>sage: plot(x,(x,2,5),color='red')
</code></pre>
<p>You ask Sage to plot the function <code>x</code> (i.e. the identity function), for <code>x</code> varying from <code>2</code> to <code>5</code>.</p>
<p>To compute the coordinates of <code>D</code>, you can do:</p>
<pre><code>sage: BA = -AB
sage: D = vector(B) + BC*BA.inner_product(BC)/BC.norm()/BC.norm()
sage: D
(219/29, 47/29)
</code></pre>
<p>To add the point <code>D</code> on the picture, you can do:</p>
<pre><code>sage: P += point(D)
</code></pre>
<p>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:</p>
<pre><code>sage: P.set_aspect_ratio(1)
</code></pre>
https://ask.sagemath.org/question/34190/projection-of-vector/?comment=34211#post-id-34211@ndomes, thanks! That is amazing! If you add it as an answer I'll be happy to mark it answering my question!Mon, 25 Jul 2016 03:55:21 -0500https://ask.sagemath.org/question/34190/projection-of-vector/?comment=34211#post-id-34211Comment by tmonteil for <p>Your approach will not work since you try to play with plots. Instead, you have to play with vectors, not their plots.</p>
<p>To my understanding of the picture, <code>D</code> is the orthogonal projection of <code>A</code> to the line <code>(B,C)</code>.</p>
<p>If <code>v1</code> denotes the vector <code>BA</code> and <code>v2</code> denotes the vector <code>BC</code>, the length <code>j</code> is the inner product of <code>v1</code> by <code>v2</code> divided by the norm of <code>v2</code>. Here is how to do it in Sage:</p>
<pre><code>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)
</code></pre>
<p>If you want a floating approximation, you can do:</p>
<pre><code>sage: RDF(v1.inner_product(v2)/v2.norm())
6.040095911547238
</code></pre>
<p><strong>EDIT :</strong> When you write </p>
<pre><code>sage: plot(x,(x,2,5),color='red')
</code></pre>
<p>You ask Sage to plot the function <code>x</code> (i.e. the identity function), for <code>x</code> varying from <code>2</code> to <code>5</code>.</p>
<p>To compute the coordinates of <code>D</code>, you can do:</p>
<pre><code>sage: BA = -AB
sage: D = vector(B) + BC*BA.inner_product(BC)/BC.norm()/BC.norm()
sage: D
(219/29, 47/29)
</code></pre>
<p>To add the point <code>D</code> on the picture, you can do:</p>
<pre><code>sage: P += point(D)
</code></pre>
<p>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:</p>
<pre><code>sage: P.set_aspect_ratio(1)
</code></pre>
https://ask.sagemath.org/question/34190/projection-of-vector/?comment=34209#post-id-34209Nice picture, thanks !Sun, 24 Jul 2016 16:50:35 -0500https://ask.sagemath.org/question/34190/projection-of-vector/?comment=34209#post-id-34209Comment by ndomes for <p>Your approach will not work since you try to play with plots. Instead, you have to play with vectors, not their plots.</p>
<p>To my understanding of the picture, <code>D</code> is the orthogonal projection of <code>A</code> to the line <code>(B,C)</code>.</p>
<p>If <code>v1</code> denotes the vector <code>BA</code> and <code>v2</code> denotes the vector <code>BC</code>, the length <code>j</code> is the inner product of <code>v1</code> by <code>v2</code> divided by the norm of <code>v2</code>. Here is how to do it in Sage:</p>
<pre><code>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)
</code></pre>
<p>If you want a floating approximation, you can do:</p>
<pre><code>sage: RDF(v1.inner_product(v2)/v2.norm())
6.040095911547238
</code></pre>
<p><strong>EDIT :</strong> When you write </p>
<pre><code>sage: plot(x,(x,2,5),color='red')
</code></pre>
<p>You ask Sage to plot the function <code>x</code> (i.e. the identity function), for <code>x</code> varying from <code>2</code> to <code>5</code>.</p>
<p>To compute the coordinates of <code>D</code>, you can do:</p>
<pre><code>sage: BA = -AB
sage: D = vector(B) + BC*BA.inner_product(BC)/BC.norm()/BC.norm()
sage: D
(219/29, 47/29)
</code></pre>
<p>To add the point <code>D</code> on the picture, you can do:</p>
<pre><code>sage: P += point(D)
</code></pre>
<p>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:</p>
<pre><code>sage: P.set_aspect_ratio(1)
</code></pre>
https://ask.sagemath.org/question/34190/projection-of-vector/?comment=34203#post-id-34203You may look at an edited version of your code [here.](http://sagecell.sagemath.org/?q=dgweis)Sun, 24 Jul 2016 07:24:05 -0500https://ask.sagemath.org/question/34190/projection-of-vector/?comment=34203#post-id-34203