# 213 - A3.6 - Vectors

## 383 days ago by Professor213

John Travis

Mississippi College

vector = (2,3) G = arrow((0,0),vector) G.show(aspect_ratio=true)
vector1 = (2,3) vector2 = (1,-2) vector3 = (3,1) G = arrow((0,0),vector1) G += arrow((0,0),vector2,color='green') G += arrow((0,0),vector3,color='red') G.show(aspect_ratio=true)
vector1 = (5,0) G = arrow((0,0),vector1) for t in range(-3,4): G += arrow((0,0),(5-2*t,t),color='orange') G += plot((5-x)/2,(x,-2,12)) G.show(aspect_ratio=true) # would be nice to have the vectors animate...
%auto @interact def _(A=random_matrix(QQ,2,2), b=matrix(QQ,2,1,(1,2)), x1=input_box(default=1,width=5,label="$x_1$"), x2=input_box(default=1,width=5,label="$x_2$"), Show_Exact=checkbox(False,label="Click to show the exact answer."), auto_update=false ): mida = min(max(A)) b = b.transpose()[0] x = A\b pretty_print(html("Experiment with these values to make the tip of the <font color=red><b>red</b></font> vector <br>line up with the sum (the end) of the <font color=blue><b>blue ones</b></font>")) At = A.transpose() Col1 = arrow((0,0),x1*At[0]) Col2 = arrow(x1*At[0],x1*At[0]+x2*At[1]) ColB = arrow((0,0),b,rgbcolor=(1,0,0),width=5) # Now, we show what the final result should look like Exact1 = arrow((0,0),x[0]*At[0],rgbcolor=(0,1,0),width=1) Exact2 = arrow(x[0]*At[0],x[0]*At[0]+x[1]*At[1],rgbcolor=(0,1,0),width=1) # answer = text("Actual value ($x_1,x_2$)="+str(x),(0,0),color=(0,1,0)) if Show_Exact: pretty_print(html("The <font color=green><b>light green</b></font> vectors represent one (of possibly many) exact solution.")) pretty_print(html("Actual value ($x_1,x_2$)="+str(x)) ) G = Col1+Col2+ColB+Exact1+Exact2 else: G = Col1+Col2+ColB show(G,frame=False)

$x_1$
$x_2$
Click to show the exact answer.

## Click to the left again to hide and once more to show the dynamic interactive window

Experiment in the 2D case above by playing with several of the randomly generated problems.  If you don't like one that the computer has come up with, just generate a nicer one.

Without using the "exact answer" selection above, approximate the solution to the following linear system using the routine above.  Double click on this cell and place your answer in the slot below.

$3x_1 + 5x_2 = 12$

$-x_1 + 7x_2 = 0$

Your answer:  $x_1 =$ _______ and $x_2 =$ _________

For a little harder problem in 3D, see if you can find values of $x_1, x_2$ and  $x_3$ so that a linear combination of $A_1, A_2$ and $A_3$ equals the red vector $b$.

%hide %auto # Finding when a vector b is in the span of other vectors in 3-space @interact def _(A=random_matrix(QQ,3,3), b=matrix(QQ,3,1,(1,2,3)), x1=input_box(default=1,width=5,label="$x_1$"), x2=input_box(default=1,width=5,label="$x_2$"), x3=input_box(default=1,width=5,label="$x_3$"), Show_Exact=checkbox(False,label="Click to show the exact answer."), auto_update=false ): mida = max(min(A)) b = b.transpose()[0] if det(A)<>0: dependent = False x = A\b else: dependent = True print 'Vectors are dependent!' pretty_print(html("Experiment with these values to make the tip of the <font color=red><b>red</b></font> vector <br>line up with the sum (the end) of the <font color=blue><b>blue ones</b></font>")) At = A.transpose() Col1 = line3d([(0,0,0),x1*At[0]],arrow_head=True) Col2 = line3d([x1*At[0],x1*At[0]+x2*At[1]],arrow_head=True) Col3 = line3d([x1*At[0]+x2*At[1],x1*At[0]+x2*At[1]+x3*At[2]],arrow_head=True) ColB = line3d([(0,0,0),b],arrow_head=True,rgbcolor=(1,0,0),line_width=5) # Now, we show what the final result should look like if not dependent: Exact1 = line3d([(0,0,0),x[0]*At[0]],arrow_head=True,opacity=0.1,rgbcolor=(0,1,0),thickness=4) Exact2 = line3d([x[0]*At[0],x[0]*At[0]+x[1]*At[1]],arrow_head=True,opacity=0.1,rgbcolor=(0,1,0),thickness=4) Exact3 = line3d([x[0]*At[0]+x[1]*At[1],x[0]*At[0]+x[1]*At[1]+x[2]*At[2]],arrow_head=True,opacity=0.1,rgbcolor=(0,1,0),thickness=4) if Show_Exact: pretty_print(html("The <font color=green><b>light green</b></font> vectors represent one (of possibly many) exact solution.")) pretty_print(html("Actual value ($x_1,x_2,x_3$)="),str(x)) G = Col1+Col2+Col3+ColB+Exact1+Exact2+Exact3 else: G = Col1+Col2+Col3+ColB show(G,frame=True)

$x_1$
$x_2$
$x_3$
Click to show the exact answer.

## Click to the left again to hide and once more to show the dynamic interactive window

Using this worksheet, solve problem number 14 on page 38 of the text.  Place your answers below.

b = random_matrix(QQ,1,2) print b bt = b.transpose() A=random_matrix(QQ,2) At =A.transpose() # Notice the correct exact answer is given by x = A\b # Finding when a vector b is in the span of other vectors in 2-space @interact def _(x1=slider(-3,3,1/20,1), x2=slider(-3,3,1/20,1),zoomin=checkbox(default=false,label='Zoom In')): G = Graphics() if zoomin==false: G += arrow((0,0),At[0],color='lightgray') G += arrow(At[0],At[0]+At[1],color='lightgray') G += arrow((0,0),x1*At[0],rgbcolor=(0,0,1)) G += arrow(x1*At[0],x1*At[0]+x2*At[1],rgbcolor=(0,1,0)) G += arrow((0,0),b[0],rgbcolor=(1,0,0),width=5) G += text("A1",(x1*At[0][0]/2,x1*At[0][1]/2),fontsize=15,color='purple') G += text("A2",(x1*At[0][0]+x2*At[1][0]/2,x1*At[0][1]+x2*At[1][1]/2),fontsize=15,color='purple') G += text("$b_1$",(0.5,0),fontsize=20,color='purple') G += point(x1*At[0],color='blue',pointsize=40) G += point(x1*At[0]+x2*At[1],color='green',pointsize=40) # Add fixed originals and dashed modified version of these scalex=2 scaley=2 show(G,frame=False,xmin=-1*scalex,xmax=scalex,ymin=-1*scaley,ymax=scaley)

[2 1]
x1
x2
Zoom In

## Click to the left again to hide and once more to show the dynamic interactive window

# this cell is a copy of the first cell but prepared for use as a webwork question A=random_matrix(QQ,2,2)*3 while det(A)==0: A=random_matrix(QQ,2,2) b=random_matrix(QQ,2,1) while b==matrix([[0],[0]]): b=random_matrix(QQ,2,1)*2 mida = min(max(A)) b = b.transpose()[0] x = A\b At = A.transpose() # Finding when a vector b is in the span of other vectors in 2-space @interact def _(x1=slider(-3,3,1/100,1,label="$x_1$"), x2=slider(-3,3,1/100,1,label="$x_2$")): html("When solving $Ax=b$solution $x$ can be determined by finding a vector whose values are the coefficients") html("which make a linear combination of the columns of A equal b. In the interactive cell below") html("<font color=blue><b>$A_1$</b></font> represents the first column of A while") html("<font color=green><b>$A_2$</b></font> represents the second column of A.") html("Determine values of $x_1$ and $x_2$ so that $x_1 A_1 + x_2 A_2 =$<font color=red><b>$b$</b></font>.") Col1 = arrow((0,0),x1*At[0],rgbcolor=(0,0,1))+text("$A_1$",(x1*At[0][0]/2,x1*At[0][1]/2),fontsize=30,color="black") Col2 = arrow(x1*At[0],x1*At[0]+x2*At[1],rgbcolor=(0,1,0))+text("$A_2$",(x1*At[0][0]+x2*At[1][0]/2,x1*At[0][1]+x2*At[1][1]/2),fontsize=30,color="black") ColB = arrow((0,0),b,rgbcolor=(1,0,0),width=5)+text("$b$",(b[0]/2,b[1]/2),fontsize=30,color="black") G = Col1+Col2+ColB show(G,frame=False)

$x_1$
$x_2$

## Click to the left again to hide and once more to show the dynamic interactive window

# this cell is a variant of the first cell but prepared for use as a webwork question A=matrix(QQ,[[1,2],[3,4]])*3 b=matrix(QQ,[[2],[1]]) mida = min(max(A)) b = b.transpose()[0] x = A\b At = A.transpose() # Finding when a vector b is in the span of other vectors in 2-space @interact def _(x1=slider(-3,3,1/100,1,label="$x_1$"), x2=slider(-3,3,1/100,1,label="$x_2$")): pretty_print(html("When solving $Ax=b$solution $x$ can be determined by finding a vector whose values are the coefficients")) pretty_print(html("which make a linear combination of the columns of A equal b. In the interactive cell below")) pretty_print(html("<font color=blue><b>$A_1$</b></font> represents the first column of A while")) pretty_print(html("<font color=green><b>$A_2$</b></font> represents the second column of A.")) pretty_print(html("Determine values of $x_1$ and $x_2$ so that $x_1 A_1 + x_2 A_2 =$<font color=red><b>$b$</b></font>.")) Col1 = arrow((0,0),x1*At[0],rgbcolor=(0,0,1))+text("$A_1$",(x1*At[0][0]/2,x1*At[0][1]/2),fontsize=30,color="black") Col2 = arrow(x1*At[0],x1*At[0]+x2*At[1],rgbcolor=(0,1,0))+text("$A_2$",(x1*At[0][0]+x2*At[1][0]/2,x1*At[0][1]+x2*At[1][1]/2),fontsize=30,color="black") ColB = arrow((0,0),b,rgbcolor=(1,0,0),width=5)+text("$b$",(b[0]/2,b[1]/2),fontsize=30,color="black") G = Col1+Col2+ColB show(G,frame=False)

$x_1$
$x_2$

## Click to the left again to hide and once more to show the dynamic interactive window