Mathematica Lab 6

MATH 220E Spring 2025

Review problems about loops

In the previous Mathematica lab, you learned about For loops. Here are two practice problems to recall how these loops work.

Problem 1

Write a For loop that prints the numbers 0, 5, 10, 15, ..., 100. (See Mathematica Homework #5 for help.)

Problem 2

Write a nested For loop that prints the pairs 00 through 55, with each digit running from 0 to 5. (So you’ll print 00, 01, 02, 03, 04, 05, 10, 11, 12, ..., 50, 51, 52, 53, 54, 55, vertically on the screen.)

Visualizing span of one vector

In this assignment you will use your skill of writing For loops to visualize the span of a set of vectors.

Given two vectors x and y in (ℝ^3), we’ll try to visualize whether y is in span(x).  Remember that span(x) is the collection of all the scalar multiples of x, and so we will graph span(x) as a bunch of dots.  Rather than draw y as a vector, we’ll simply visualize its head as a yellow dot in space.

Here is a function that requires two inputs, both vectors. The function will graph span(x) and draw a big yellow dot for y. We’ll demonstrate this code first, and then discuss how it works.

visualSpan1[x_,y_]:=Module[{A,i},
(* initalize an empty list *) 
A={};

(* main loop *) 
For[i=-50,i≤50,i++,
A=Append[A,0.25*i*x]
];

(* draw the plot *) 
ListPointPlot3D[{A,{y}},PlotStyle->{PointSize[0.005],PointSize[0.02]}]
]

We now test the function with x={4,1,3} and y={5,6,7}. We can see that y is not in span(x) since the yellow dot (that is, y) is not on the line (that is, span(x)).

visualSpan1[{4,1,3},{5,6,7}]

Next, here is an example where y is in span(x).  You should click-and-hold on the graph and drag your mouse so that you can look at the graph from different perspectives!

visualSpan1[{4,1,3},3.14*{4,1,3}]

Now we’ll dissect the code. Here is another copy of the code:

visualSpan1[x_,y_]:=Module[{A,i},
(* initalize an empty list *) 
A={};

(* main loop *) 
For[i=-50,i≤50,i++,
A=Append[A,0.25*i*x]
];

(* draw the plot *) 
ListPointPlot3D[{A,{y}},PlotStyle->{PointSize[0.005],PointSize[0.02]}]
]

Let's explain what each line of code does:

VisualSpan1[x_,y_]:=    This is the start of a function definition that has two inputs, x and y.

Module[{A,i}, ...]          This tells Mathematica that we’ll use the variables A and i inside of our function definition.

A={};         This defines A as an empty list. We’ll see that the For loop puts scalar multiples of the vector x inside of A as it runs.

For[i=-50, i≤50, i++, A=Append[A,0.25*i*x]];   This defines a loop with index variable i from -50 to 50, in increments of 1. For each value of i, we will append a certain scalar multiple of x to the list A. In fact, we’ll eventually get A = {-12.5x, -12.25x, -12x, ..., -0.25x, 0, 0.25x, ...., 12x, 12.25x, 12.5x}.

ListPointPlot3D[{A,{y}}, PlotStyle -> {PointSize[0.005],PointSize[0.02]}]   This makes a 3D plot showing the points in the set A (as dots of size .005) and the point y (as a yellow dot of size .02).

Problem 3

Keep x the same as above (x={4,1,3}) but choose two new y vectors, one that is in span(x) and one that is not. Show the pictures using visualSpan1.

Problem 4

Use visualSpan1 with x={4,1,3} and y={100,25,75}. Even though y is in span(x), it won’t be clear from the image because y is so far from the origin.  Change the definition of visualSpan1 so that you can tell that y is in span(x). Call your new visualization function newVisualSpan1.

Visualizing span of two or three vectors

Next we’ll visualize whether vector z is in the span of two vectors x and y.

Problem 5

Write a new function called visualSpan2 that takes three inputs — vectors x, y, and z — and plots span(x,y) together with the head of vector z.  Instead of using a For loop to put the points 0.25·i·x into the list A, you’ll want to use a nested For loop to put the points 0.25·i·x + 0.25·j·y inside the list A. To speed things up a bit, you could have your parameters run from -40 to 40 instead of -50 to 50.

When you have finished writing your function, test it on these two cases:

(a)    x={4,1,3}, y={5,6,7}, z={9,7,40}

(b)    x={4,1,3}, y={5,6,7}, z=2.3·{4,1,3}-1.7·{5,6,7}

Problem 6

Write a function called visualSpan3 that simply shows the span of 3 vectors. Mathematica will get bogged down by too many calculations, so have your parameters run from -10 to 10 instead of -50 to 50.

When you have finished writing your function, test it on the following cases:

(a)    Vectors {1,0,0}, {0,1,0}, {0,0,1}. What should you get for the span of these?

(b)    Vectors {1,0,0}, {0,1,0}, {1,0,1}. What should you get for the span of these?

(c)    A different set of three vectors whose span is all of (ℝ^3) (you create the set).

(d)    A different set of three vectors whose span turns out to be a plane in (ℝ^3) (you create the set).

(e)    A different set of three vectors whose span turns out to be a line in (ℝ^3) (you create the set).