ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Projects 2
@ 2000-07-27 22:21 David Arnold
  0 siblings, 0 replies; only message in thread
From: David Arnold @ 2000-07-27 22:21 UTC (permalink / raw)
  Cc: ntg-context

[-- Attachment #1: Type: text/plain, Size: 357 bytes --]

All,

Attached are:

linalg.tex %the project file
environment1.tex %the environment file
activities.tex %the product file
context-matrices.tex %first component
context-vectors %second component

Each component file will compile. I get no pages of output if I try to
compile either the project (linalg) or the product (acitivites). Is this
correct behavior?

[-- Attachment #2: linalg.tex --]
[-- Type: text/plain, Size: 83 bytes --]

\startproject linalg

\environment environment1

\product activities

\stopproject

[-- Attachment #3: environment1.tex --]
[-- Type: text/plain, Size: 713 bytes --]

\startenvironment environment1

\useURL [home] [http://online.redwoods.cc.ca.us/instruct/darnold]
\useURL [darnold] [mailto:darnold@northcoast.com] [] [darnold@northcoast.com]
\useURL [David-Arnold] [mailto:David-Arnold@Eureka.redwoods.cc.ca.us] [] [David-Arnold@Eureka.redwoods.cc.ca.us]

\setupurl[style=type]

\startmode[letter]

\setuppapersize[letter][letter]

\setuplayout
  [topspace=1.5cm,
   backspace=2cm,
   width=middle,
   footer=1.5cm,
   header=1.5cm,
   height=middle]

\setupwhitespace[medium]

\setupindenting[medium]

\setuptyping[margin=standard]

\setupheads[alternative=margin]

\setupbodyfont[lbr,10pt]

\setupfootertexts[pagenumber]

\setupheader[state=empty]

\stopmode

\stopenvironment

[-- Attachment #4: activities.tex --]
[-- Type: text/plain, Size: 114 bytes --]

\startproduct activities

\project linalg

 \component context-vectors
 \component context-matrices

\stopproduct

[-- Attachment #5: context-vectors.tex --]
[-- Type: text/plain, Size: 9650 bytes --]

\startcomponent context-vectors

%\showframe

\setupheadertexts[Matlab and Vectors]

%\starttext

 \start

 \setupalign[middle]

  \tfc Matlab and Vectors

  \tfa\setupinterlinespace

  \vskip 2ex

  Math 45---Linear Algebra

  David Arnold

  \vskip 2ex

 \stop

\midaligned{\currentdate}

  \vskip 2ex

\midaligned{\bf Abstract}

\startnarrower[3*middle]

\noindenting In this exercise you will learn how to enter and edit
vectors in Matlab. Operations involving vectors and scalars will
be discussed. {\em Prerequisites: None}

\stopnarrower

\section{Entering Row Vectors}

Entering row vectors is easy in Matlab. Enter the following
command at the Matlab prompt \footnote{The \type{>>} is the
Matlab prompt. You must enter what comes after the prompt.}.
 \starttyping
 >> v=[1 2 3]
 \stoptyping
There are a number of Matlab constructs introduced in
this command. To enter a vector, first enter a left bracket, the
elements of the vector separated by spaces, and a closing right
bracket. You can also use commas to delimit the entries of your
vector.
 \starttyping
 >> v=[1,2,3]
 \stoptyping
The = sign is Matlab's assignment operator. You use
this operator to assign Matlab items to variables. To see that
the row vector \type{[1,2,3]} has been assigned to the variable
\type{v}, enter the following command at the Matlab prompt.
 \starttyping
 >> v
 \stoptyping

\subsection{Ranges}

Sometime you will need to enter a vector of equally spaced
entries. This is easily accomplished in Matlab with the construct
\type{start:increment:finish}. If you don't supply an increment,
Matlab assumes that the increment is 1.
 \starttyping
 >> x1=0:10
 \stoptyping
You can select your own increment.
 \starttyping
 >> x2=0:2:10
 \stoptyping
You can even go backwards.
 \starttyping
 >> x3=10:-2:0
 \stoptyping
And you can get very fancy indeed.
 \starttyping
 >> x4=0:pi/2:2*pi
 \stoptyping

There will be times, particularly when plotting functions, that
you will need a large number of entries in a vector.
 \starttyping
 >> x=0:.1:10
 \stoptyping

\subsection{Suppressing Output}

You can suppress the output of a Matlab command by appending a
semicolon.
 \starttyping
 >> x=0:.1:10;
 \stoptyping
This is particularly useful when the output is large
and you have no desire to see it.

\subsection{Matlab's Workspace}

You can get a listing of your variables in your workspace at any
time by issuing the following command at the Matlab prompt.
 \starttyping
 >> who
 \stoptyping
You can get even more information about the variables
in your workspace by entering
 \starttyping
 >> whos
 \stoptyping
Note that the size of each of the variables in your workspace is
given. You can also find the size of the vector \type{v} by
entering
 \starttyping
 >> size(v)
 \stoptyping
The information supplied by the result of this last
command indicates that the vector \type{v} has 1 row and 3
columns. Although you can think of the vector \type{v} as a
matrix with 1 row and 3 columns, you can also think of it as a
row vector with length 3. Try the following commands at the
Matlab prompt \footnote{You can easily get help on any of
Matlab's commands. For example, enter \type{help length} at the
Matlab prompt to get help on the \type{length} command.}.
 \starttyping
 >> length(v)
 \stoptyping

\section{Entering Column Vectors}

Entering column vectors is also easy in Matlab. Enter the
following command at the Matlab prompt.
 \starttyping
 >> w=[4;5;6]
 \stoptyping
Note that semicolons delimit the rows of a column
vector. Try the following commands.
 \starttyping
 >> w
 >> who
 >> whos
 >> size(w)
 \stoptyping
The information supplied by the result of this last
command indicates that the vector \type{w} has 3 rows and 1
column. Although you can think of the vector \type{w} as a matrix
with 3 rows and 1 columns, you can also think of it as a column
vector with length 3. Try the following command at the Matlab
prompt.
 \starttyping
 >> length(w)
 \stoptyping

\subsection{The Transpose Operator}

You can use Matlab's transpose operator (a single apostrophe) to
change a row vector to a column vector.
 \starttyping
 >> y=(1:10)'
 \stoptyping
Or a column vector to a row vector.
 \starttyping
 >> y=y'
 \stoptyping

\subsection{Indexing Vectors}

Once you have defined a vector, you can easily access any of its
elements with Matlab's powerful indexing commands. For example,
enter the following vector.
 \starttyping
 >> x=[10,13,19,23,27,31,39,43,51]
 \stoptyping
Now try each of the following commands.
 \starttyping
 >> x(2)
 >> x(7)
 \stoptyping
You can easily change an element of a vector.
 \starttyping
 >> x(6)=100
 \stoptyping
You can also access a range of elements.
 \starttyping
 >> x([1,3,5])
 >> x(1:3)
 >> x(1:2:length(x))
 \stoptyping

\section{Vector Operations}

There are numerous operations involving vectors and scalars that
are handled easily in Matlab.

\subsection{Vector-Scalar Operations}

Scalar-vector operations are straightforward. Try each of the
following Matlab commands and study the resulting output.
 \starttyping
 >> y=1:5
 >> y+2
 >> y-2
 >> 2*y
 >> y/2
 \stoptyping
Of course, these operations are equally valid when
using column vectors.
 \starttyping
 >> w=(1:3:20)'
 >> w+3
 >> w-11
 >> .1*w
 >> w/10
 \stoptyping

\subsection{Vector-Vector Operations}

First enter the following vectors.
 \starttyping
 >> a=1:3
 >>b=4:6
 \stoptyping
Adding and subtracting vectors is natural and easy.
Try the following Matlab commands \footnote{ Because no
semicolons are present to prevent suppression of output, the
command \type{a,b,a+b} will display the vector \type{a}, display
the vector \type{b}, then display the vector \type{a+b}.}.
 \starttyping
 >> a,b,a+b
 >> a,b,a-b
 \stoptyping
Of course, these operations are equally valid when
using column vectors.
 \starttyping
 >> a=(1:3)',b=(4:6)'
 >> a+b,a-b
 \stoptyping
However, you can arrive at unexpected results if you do not
remember that Matlab is a {\em matrix} environment.
 \starttyping
 >> a,b,a*b
 \stoptyping
This last command leads to an error because \type{*} is Matlab's
symbol for {\em matrix} multiplication and you are not using it
properly \footnote{We'll look at Matlab's matrix multiplication
operator in later activities.}.

You can also get into trouble if you attempt to add (or subtract)
vectors of unequal lengths.
 \starttyping
 >> a=1:3,b=4:7,a+b
 \stoptyping

\subsection{Elementwise Operations}

If you wish to multiply the vectors \type{a} and \type{b} on an
{\em element-by-element} basis, you enter the following Matlab
command.
 \starttyping
 >> a,b,a.*b
 \stoptyping
The symbol \type{.*} is Matlab's elementwise
multiplication operator. The output is computed by multiplying
the first elements of the vectors \type{a} and \type{b}, then the
second elements of each vector, etc.

Matlab's elementwise division operator, \type{./}, works in much
the same manner.
 \starttyping
 >> a,b,a./b
 \stoptyping
If you wish to exponentiate on an element-by-element basis, use
\type{.^+}.
 \starttyping
 >> a,a.^2
 \stoptyping

\subsection{More Complicated Expressions}

With a little practice you will soon learn to evaluate quite
complicated expressions. Suppose, for example, that you wish to
evaluate the expression $x^{2}-2x-3$ for values of $x$ ranging
from 1 through 10, in increments of 1.
 \starttyping
 >> x=1:10
 >> y=x.^2-2*x-3
 \stoptyping
Take a moment to check these results with a
calculator.

Let's look at another example. Suppose that you would like to
evaluate the expression $\sin x/x$ for values of $x$ ranging from
$-1$ to 1 in increments of 0.1 \footnote{To examine a list of
Matlab's elementary functions, type \type{help elfun} at the
Matlab prompt.}.
 \starttyping
 >> x=-1:.1:1
 >> y=sin(x)./x
 \stoptyping
Check these results with a calculator.

Matlab's elementwise operators will also work on column vectors.
 \starttyping
 >> xdata=(1:10)'
 >> xdata.^2
 \stoptyping

\section{Homework Assignment}

{\em Answer questions 1-9 on regular notebook paper. Be sure to
test your responses at the computer.}

\startitemize[n,broad]

\item  Write the Matlab commands that generate each of the following vectors.

\startitemize[a]

\item  $\pmatrix{1\cr 2\cr -3}$

\item  $\pmatrix{1,2,-1,3}$

\item  A column vector containing all the odd numbers from 1 to 1000.

\item  A row vector containing all of the even numbers from 2 to 1000.

\stopitemize

\item  If \type{x=0:2:20}, write the Matlab command that will square each
entry of \type{x}.

\item  If \type{x=[0,1,4,9,16,25]}, write the Matlab command
that will take the square root of each entry of \type{x}.

\item  If \type{x=0:.1:1}, write the Matlab command that will raise each
entry of \type{x} to the power of 2/3.

\item  If \type{x=0:pi/2:2*pi}, write the Matlab command that will take
the cosine of each entry of \type{x}.

\item  If \type{x=-1:.1:1}, write the Matlab command that will take the
inverse sine of each entry of \type{x}.

\item  If \type{x=-1:.1:1}, write the Matlab command that will take the
exponential of each entry of \type{x}.

\item  If \type{x=linspace(0,2*pi,1000)}, what is the 50th entry of
\type{x}? What is the length of \type{x}?

\item  If \type{k=0:100}, what is the 12th entry of \type{y=0.5.^k}?

\item Obtain a printout of the graph resulting from the following
sequence of commands.
 \starttyping
 t=linspace(0,2*pi,1000);
 x=2*cos(3*t);
 y=3*sin(5*t);
 plot(x,y)
 xlabel('x-axis')
 ylabel('y-axis')
 title('The graph of x = 2*cos(3*t), y = 3*sin(5*t).')
 \stoptyping
Finally, just for fun, try the following command sequence.
 \starttyping
 close all, comet(x,y)
 \stoptyping

\stopitemize

%\stoptext

\stopcomponent

[-- Attachment #6: context-matrices.tex --]
[-- Type: text/plain, Size: 7968 bytes --]

\startcomponent context-matrices

%\showframe

\setupheadertexts[Matlab and Matrices]

%\starttext

 \start

 \setupalign[middle]

  \tfc Matlab and Matrices

  \tfa\setupinterlinespace

  \vskip 2ex

  Math 45---Linear Algebra

  David Arnold

  \vskip 2ex

 \stop

\midaligned{\currentdate}

  \vskip 2ex

\midaligned{\bf Abstract}

\startnarrower[3*middle]

\noindenting In this exercise you will learn how to enter and edit
matrices in Matlab. You will also experiment with some of
Matlab's built-in matrix builders. You will also learn how to
build matrices from vectors and blocks of matrices. {\em
Prequisites: None}

\stopnarrower

\section{Entering Matrices}

Entering matrices in Matlab is easy. Enter the following at the Matlab
prompt.
 \starttyping
 >> A=[1,2,3;4,5,6;7,8,9]
 \stoptyping
Delimit the entries in a row with commas and delimit the rows with
semicolons. Spaces can also be used to delimit the entries in a
row.
 \starttyping
 >> A=[1 2 3;4 5 6;7 8 9]
 \stoptyping

\subsection{Special Matrices in Matlab}

Matlab has a number of built-in routines to help create matrices
\footnote{For a list of these elementary matrices, type
\type{help elmat} and \type{help specmat} at the Matlab prompt.}.
You can create a matrix of zeros of any size \footnote{Remember,
help is readily available for any Matlab function. For example,
type \type{help zeros} to receive help on the \type{zeros}
function.}.
 \starttyping
 >> A=zeros(5)
 >> B=zeros(3,5)
 \stoptyping
You easily can create a matrix of zeros that is the same size as
a given matrix.
 \starttyping
 >> C=magic(5)
 >> D=zeros(size(C))
 \stoptyping
You can create matrices of ones in a similar manner.
 \starttyping
 >> A=ones(6)
 >> B=ones(2,10)
 >> C=hilb(5)
 >> D=ones(size(C))
 \stoptyping

When performing simulations in Matlab it is useful to build matrices of
random numbers. You can create a matrix of uniformly distributed random
numbers, each between 0 and 1 (0 inclusive), with the following commands.
 \starttyping
 >> A=rand(6)
 >> B=rand(5,3)
 \stoptyping

Scalar multiplication works with matrices in exactly the same manner as with
vectors.
 \starttyping
 >> C=10*rand(5)
 >> D=floor(10*rand(5))
 \stoptyping

The identity matrix has ones on its main diagonal and zeros
everywhere else.\footnote{The import of the identity matrix will
be investigated in later activities.}
 \starttyping
 >> I=eye(5)
 \stoptyping

Other types of diagonal matrices are possible with Matlab's
\type{diag} command.
 \starttyping
 >> E=diag([1,2,3,4,5])
 >> F=diag([1,2,3,4,5],-1)
 >> G=diag(1:5,1)
 \stoptyping

\subsection{The Transpose Operator}

The transpose operator (single apostrophe) plays the same role with matrices
as it does with vectors. Rows are changed to columns and columns are changed
to rows.
 \starttyping
 >> J=[1 2 3;4 5 6;7 8 9]
 >> J'
 \stoptyping

\subsection{Suppressing Output}

Remember, appending a semicolon to any Matlab command will suppress the
output. This is useful if the result is large and you have no desire to view
it.
 \starttyping
 >> K=rand(100);
 \stoptyping

\subsection{Matlab's Workspace}

Take a moment to examine Matlab's workspace with the \type{whos}
command.
 \starttyping
 >> whos
 \stoptyping
Note the size of each of the variables in your workspace is
given. Of course, you could also find the size of the matrix
\type{I} by entering
 \starttyping
 >> size(I)
 \stoptyping

\section{Indexing Matrices}

The following notation is used to denote a matrix with 3 rows and 3 columns.
 \placeformula[-]
 \startformula
 A=\pmatrix{%
 a_{11} & a_{12} & a_{13} \cr
 a_{21} & a_{22} & a_{23} \cr
 a_{31} & a_{32} & a_{33}}
 \stopformula
Note that $a_{12}$ refers to the entry \index{matrix
entries@matrix entries} in row 1, column 2. In general, $a_{ij}$
refers to the entry in row $i$, column $j$.

Matlab uses similar notation to identify the elements of a matrix.
 \starttyping
 >> A=pascal(5)
 >> A(1,2)
 >> A(3,4)
 \stoptyping
In general, \type{A(i,j)} refers to the element in row $i$, column
$j$ of matrix $A$.

You can easily change an entry in a matrix.
 \starttyping
 >> A(3,3)=11111
 \stoptyping

\subsection{Advanced Indexing Techniques}

When indexing a matrix, your subscripts can also be vectors. This is a
powerful tool which allows you to easily select a submatrix of a given
matrix.
 \starttyping
 >> A=magic(6)
 >> A([1,2],[3,4,5])
 \stoptyping
The notation \type{A([1,2],[3,4,5])} references the submatrix
formed using the elements that appear in rows 1 and 2 {\em and}
in columns 3,4, and 5 of the matrix \type{A}.

The command
 \starttyping
 >> A([1,3,5],[1,2,3,4,5,6])
 \stoptyping
produces a submatrix composed of rows 1, 3, and 5 of matrix
\type{A}.

If you recall that the notation \type{1:6} represents the vector
\type{[1,2,3,4,5,6]}, so that \type{A(1,1:6)} is equivalent to
\type{A(1,[1,2,3,4,5,6])}.
 \starttyping
 >> A(1,1:6)
 \stoptyping
Using a colon by itself in place of a subscript denotes {\em all}
of the corresponding row or column. Thus,
 \starttyping
 >> A(:,1)
 \stoptyping
produces the first column of the matrix \type{A}, and
 \starttyping
 >> A(3,:)
 \stoptyping
produces the third row of the matrix \type{A}. In a sense, you
can read the notation \type{A(3,:)} as follows: ``Third row,
every column.'' The command
 \starttyping
 >> A(1:3,:)
 \stoptyping
produces a submatrix composed of the first three rows of the
matrix \type{A}, while the command
 \starttyping
 >> A(:,1:2:6)
 \stoptyping
produces a submatrix composed of columns 1, 3, and 5 of matrix
\type{A}.

\section{Building Matrices}

Matlab allows you to build complex matrices out of vectors and matrices.

\subsection{Building Matrices From Vectors}

Create three row vectors with the following commands.
 \starttyping
 >> v1=1:3
 >> v2=4:6
 >> v3=7:9
 \stoptyping
The command
 \starttyping
 >> M=[v1;v2;v3]
 \stoptyping
builds a matrix with the vectors \type{v1}, \type{v2}, and
\type{v3} each forming a row of the matrix \type{M}.

The command
 \starttyping
 >> N=[v1,v2,v3]
 \stoptyping
produces a significantly different but logical result.

Change the vectors \type{v1}, \type{v2}, and \type{v3} into
column vectors with the transpose operator.
 \starttyping
 >> v1=v1'
 >> v2=v2'
 >> v3=v3'
 \stoptyping
The command
 \starttyping
 >> P=[v1,v2,v3]
 \stoptyping
builds a matrix with the vectors \type{v1,v2}, and \type{v3} each
forming a column of the matrix \type{P}. Of course, you could
have achieved the same result by taking the transpose of the
matrix \type{M}.
 \starttyping
 >> M'
 \stoptyping

\subsection{The Dimensions Need To Match}

When building matrices, be sure that each row and column has the same number
of elements. For example, the following sequence of commands will produce an
error.
 \starttyping
 >> w1=1:3,w2=4:6,w3=7:10
 >> Q=[w1;w2;w3]
 \stoptyping

\subsection{Building Matrices From Other Matrices}

It is a simple matter to augment a row or column vector to a matrix.
Remember, each row and column of a matrix must have the same number of
elements. Thus,
 \starttyping
 >> A=[1,2,3,4;5,6,7,8;9,10,11,12]
 >> b=[1,1,1]'
 >> M=[A,b]
 \stoptyping
is valid, but
 \starttyping
 >> M=[A;b]
 \stoptyping
is not.

You can also augment two or more matrices. Remember, each row and column of
a matrix must have the same number of elements. Thus,
 \starttyping
 >> A=magic(3),B=ones(3,4)}
 >> M=[A,B]
 \stoptyping
is valid, but
 \starttyping
 >> N=[A;B]
 \stoptyping
is not.

\subsection{Your Imagination Is Your Only Limit}

Matlab's matrix building capability is extremely flexible. Here is an
interesting example.
 \starttyping
 >> A=zeros(3),B=ones(3),C=2*ones(3),D=3*ones(3)
 >> M=[A,B;C,D]
 \stoptyping

Here is something called a {\em Vandermonde} matrix.
 \starttyping
 >> x=[1,2,3,4,5]'
 >> N=[ones(size(x)),x,x.^2,x.^3,x.^4]
 \stoptyping
And here is one final piece of chicanery.
 \starttyping
 >> B=zeros(8)
 >> B(1:3,1:3)=[1 2 3;4 5 6;7 8 9]
 >> B(4:8,4:8)=magic(5)
 \stoptyping

%\stoptext

\stopcomponent

[-- Attachment #7: Type: text/plain, Size: 287 bytes --]

-
David Arnold
College of the Redwoods
Mathematics Department
7351 Tompkins Hill Road
Eureka, CA 95501
(707) 476-4222

My Home Page
http://online.redwoods.cc.ca.us/instruct/darnold/index.htm

Ordinary Differential Equations Using Matlab
http://www.prenhall.com/books/esm_0130113816.html

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2000-07-27 22:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-07-27 22:21 Projects 2 David Arnold

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).