Week 15 - papademas.net

advertisement
CSC 170 SESSION 15 NOTES
Welcome Message
Welcome to Session 15 .
Course Web Site(s)
Here is the link to the Course Web site ( CSC 170 ) .
http://www.papademas.net/occ/csc170
Here is the link to the Online Course Web site ( CSC 171 / 172 / 173 ) .
http://online.oakton.edu
List of Topics for this Week’s Lecture

Some Linux Commands

Using Mathematica

Web Page Design Hints and Tips

Using JavaScript / VBScript

Algorithm Design Hints and Tips

Writing Complete Computer Programs

Review of Binary Numbers

Review of Hexadecimal Numbers

What about FTP? File Transfer Protocol

A review of Machine Problem 6 ( MP6 )
( Vectors in Three Space )
__________________________________________________________________
Some Linux Commands
Here is a review of various compile commands:
C++
g++ myFile.cpp -o myFile
FORTRAN
f90 myFile.f90 -o myFile
Java
javac myFile.java
Using Mathematica / WolframAlpha
Here is a review of Mathematica using vector operations.
Constructing Vectors
Table
construct a vector from an expression
Example:
A table of the first five squares:
Table[i^2, {i, 5}]
Array
construct a vector from a function
Example:
Generate a 3 by 3 array of zeros:
Array[0 &, {3, 3}]
ConstantArray
construct a vector of constants
Example:
Make a 4 by 3 array of c's:
ConstantArray[c, {4, 3}]
Elements of Vectors
Length
number of elements in a vector
Example:
Find the length of a list.
Length[{a, b, c, d}]
Mathematical Operations
+, *, ^, ...
automatically element-wise:
Example:
{a, b}+{c, d} returns {a+b, c+d}
Dot (.)
scalar dot product
Example:
{a, b, c} . {x, y, z}
Cross ()
vector cross product (entered as Esc cross Esc)
Example:
The cross product of two vectors.
Cross[{a, b, c}, {x, y, z}]
Norm
norm of a vector
Example:
Norm[{x, y, z}]
Total
total of elements in a vector
Example:
Total[{a, b, c, d}] returns a + b + c + d
Vector Space Operations
VectorAngle
angle between two vectors
Example:
VectorAngle[{1, 0}, {1, 1}] returns PI / 4
UnitVector
unit vector along a coordinate direction
Example:
The unit vector in the x direction in 2 dimensions:
UnitVector[1] returns {1, 0}
Normalize
normalize a vector to unit length
Example:
Normalize[{1, 5, 1}] rteuns the length of vector {1, 5, 1}
Projection
find the projection of one vector on another
Example:
Project the vector (5, 6, 7) onto the x axis:
Projection[{5, 6, 7}, {1, 0, 0}]
Web Page Design Hints and Tips
Review of HTML Hyperlinks
<html>
<body>
<p>
<a href = "index.htm">
This text</a> is a link to a page on this Web site.
</p>
<p>
<a href = "http://www.fye.com/">
This text</a> is a link to a page on the World Wide Web.
</p>
</body>
</html>
Using JavaScript / VBScript
Here is a simple VBScript example using a subroutine.
Sub ConvertTemp()
temp = InputBox("Please enter the temperature in degrees F.", 1)
MsgBox "The temperature is " & Celsius(temp) & " degrees C."
End Sub
Here is a simple VBScript example using a function procedure.
Function Celsius(fDegrees)
Celsius = (fDegrees - 32) * 5 / 9
End Function
Algorithm Design Hints and Tips
What is an Algorithm? Review the " Matrix Inversion " in Mathematics SP12
( Special Project 12 ) .
What are the specifications behind purchasing a typical home computer system?
That is what will be researched in this lab project.
To use the MINVERSE() command in Excel, here is an example.
Enter the following data:
A1 = 1, B1 = 2
A2 = -3, B1 = 4
Highlight cells A4 through B5. With this range highlighted, type the following formula:
=MINVERSE(A1:B2)
Use CTRL + SHIFT + ENTER to construct the inverse matrix.
Observe the result.
A4 = 0.4, B4 = -0.2
A5 = 0.3, B5 = 0.1
This computing the inverse of a matrix is an array computation in MS Excel, the target range to
place your answer must be highlighted in advance of the computation. This ways it returns 4
values.
To perform the above matrix inversion by hand:
Take the original matrix and calculate its determinant.
| 1
2 |
| -3
4 |
determinant = (1)(4)-(2)(-3) = 10
( Hold this answer. )
Switch the 1 and the 4, the forward diagonal elements.
| 4
| -3
-
2 |
1 |
-
Change the signs of the off - diagonal elements 2 and -3.
| 4
-2 |
| 3
1 |
Finally, divide each element by 10, the matrix determinant.
| 4/10 -2/10 |
| 3/10
1/10 |
The above is the inverse matrix.
Matrix multiplication can be used to verify an inverse.
| 1
| -3
-
2 |
4 |
-

| 4/10
| 3/10
-
-2/10 |
1/10 | =
-
| 1 (4/10) + 2(3/10)
| -3 (4/10) + 4(3/10)
-
1 (-2/10) + 2(1/10)
-3(-2/10) + 4(1/10)
|
|=
-
| (4 / 10) + (6 / 10)
| (-12 / 10) + (12 / 10)
-
(-2 / 10) + (2 / 10)
(6 / 10) + (4 / 10)
|
|=
-
| 1
| 0
-
0
1
|
|
-
For 3 by 3 inversion,
| a
| d
| g
Compute the
b
e
h
c
f
i
determinant:
( Determinant )
|
|
|
-
D = a * (e * i - h * f) - b * (d * i - f * g) + c * (d * h - e * g)
Inverse Elements
Row 1 Column 1 = (e * i - f * h) / D
Row 1 Column 2 = -(b * i - c * h) / D
Row 1 Column 3 = (b * f - c * e) / D
Row 2 Column 1 = -(d * i - f * g) / D
Row 2 Column 2 = (a * i - c * g) / D
Row 2 Column 3 = -(a * f - c * d) / D
Row 3 Column 1 = (d * h - e * g) / D
Row 3 Column 2 = -(a * h - b * g) / D
Row 3 Column 3 = (a * e - b * d) / D
Writing Complete Computer Programs
MP6 Vectors
Vectors defined
A quantity that has both magnitude and direction represented by arrow.
Length of arrow indicates its magnitude
The arrows direction is the direction of its quantity
Examples of vectors: displacement, velocity, acceleration
Scalars (ordinary vectors) are used in equations., time , mass
You can add, subtract, multiply vectors. You multiply a vector by either a positive or negative scalar or
you can multiply a vector by another vector.
Vector Illustration
Vector going from A to B
All 3D vectors can be represented by a directed line segment in 3D space R3, which has a start point and
an end point. This gives each vector a magnitude (the length of the line segment) and direction (from
the start point to the end point).
If we take the origin as out starting point we can describe any vector, a, by specifying its end point in
Cartesian coordinates (x, y, z). So any vector can be described by an ordered 3-tuple (x, y, z) where x, y
and z are real numbers.
The position vector of a point A is the vector represented by the line segment from the origin to A, and
is written a.
3D vector
Addition of Vectors (helps to determine 3rd vector for the resultant magnitude, and angle (theta))
Given the points A, B, C, let the u be the vector from A to B, and v be the vector from B to C. The vector
u + v is the vector from point A to point C. On paper, if
u = (a, b, c) and
v = (x, y, z) then
u + v = (a + x, b + y, c + z)
Pseudocode - loop over dimension of the space
for i = 0 to 3
// vector sum
vsum[i] = v[i]+w[i];
// vector difference
vdiff[i] = v[i]-w[i];
// unit vector v
uv[i] = v[i]/nv;
// unit vector w
uw[i] = w[i]/nw;
To calculate the magnitude and direction of vectors we use the dot product and cross product of the
vectors.
Dot product (A · B) - A scalar that measures the tendancy of the 2 vectors that point in the same
direction aka the magnitude!
The dot product is implemented in Mathematica as Dot[a, b], or simply by using a period, a . b.
The graphic below gives its definition for two vectors a and b depicting a very practical
application.
The man is pulling the block with a constant force a so that it moves along the horizontal ground.
The work done in moving the block through a distance b is then given by the distance moved
through multiplied by the magnitude of the component of the force in the direction of motion.
This is |a| |b| cos t so we define the scalar or dot product as
a·b = |a||b| cos t
where t is the angle between a and b when they are placed tail to tail.
To use the least amount of force possible, we would need to pull horizontally, so that we are
pulling in the same direction as we want the object to move. Then we would have t=0 and cos
t=1 so that
work done = a·b = |a||b| = magnitude of the force x distance moved in the direction of the force.
Example - work (W)= force(pounds) F x displacement (feet) X
W= F · X = 50i x 3i = 150 foot lbs.
?
1
4
3
1
Find length of vector
u= 3i + 4j where i and j can be seen as x,y
||u||= sqrt(3^2+4^2) = 5
Degree can be found by trig.
u · v = ||u|| ||v|| cos theta
theta = acos(u · v) / ||u|| ||v||)
Cross product - measure of tendency of two vectors to be perpendicular. It is used to describe
spinning or rotating objects, etc. It shows both magnitude and direction!
Calculations- magnitude and direction (Engineering style without any angles given!)
dot product (finding dot product- multiply corresponding i, j, k components and add each
corresponding result)
where i is vector is in x direction
j is vector is in y direction
k is vector is in z direction
a = 5i - 6j + 3k [ <5, -6, 3> magnitudes of the x,y,z direction for a
b = 2i + 7j +4k [ <-2, 7, 4> magnitudes of the x,y,z direction for b
a · b = (5*-2) +(6*7) + (3*4) = -10 - 42 + 12 [ -40
Cross product - can be found by taking the determinant of (a x b). Result shows how much of the
vectors are perpendicular to each other. To get the result we will multiple those magnitudes and apply
right hand rule will then tell you the direction your pointing.
axb=
i
j
k
5
-6
3
-2
7
4
deal with sub-determinant for i, j, k (taking out row and column of each)
[
-6 3 * i -
5 3 *j
7
-2 4
4
+
5 -6 * k
-2 7
= (-24-21) i - ( 20--6) j + (35-12) k = -45i - 26j + 23k
result shows vector that is perpendicular to a and b
Review of Binary Numbers
Here are some examples of bitwise manipulation:
NOT 1001 ( decimal 9 ) = 0110 ( decimal 6 )
0101 ( decimal 5 ) OR 0110 ( decimal 6 ) = 0111 ( decimal 7 )
0101 ( decimal 5 ) XOR 0110 ( decimal 6 ) = 0011 ( decimal 3 )
0101 ( decimal 5 ) AND 0110 ( decimal 6 ) = 0100 ( decimal 4 )
00110101 LEFT - SHIFT = 01101010
00110101 RIGHT - SHIFT = 00011010
Shifts in C, C++ and Java
Bit – wise manipulation
/* The obvious method */
if (x < y)
r = x;
else
r = y;
/* A method using bit manipulation */
r = y + ((x - y) & -(x < y));
Source code that performs bit manipulation makes use of the bitwise operations: AND, OR,
XOR, NOT, and bit shifts.
Review of Hexadecimal Numbers
Binary to Hex Conversion
Here are some examples to convert from an integer binary number to hexadecimal.
Split the binary number into 4 - bit sections from the least significant bit to the most significant
bit.
Convert the 4 - bit binary number to its hexadecimal equivalent.
For example, the binary value 1010111110110 100 will be written:
1001
9
1111
F
1011
B
0100
4
Hex to Binary Conversion
Here are some examples to convert from an integer hexadecimal number to binary.
Convert the hexadecimal number to its 4 - bit binary equivalent.
Combine the 4 - bit sections by removing the spaces.
For example, the hexadecimal value 9FB4 will be written:
9FB4
1001 1111 1011 0100
This yields the binary number 1001111110110100 .
Using FTP
What is FTP? File Transfer Protocol
To connect to your personal campus network drive:
ftp://ftp.oakton.edu
To connect to the F:\ Samples drive:
ftp://samples.oakton.edu
A Preview of MP 6 ( One - Dimensional Arrays )
Machine Problem 6 deals with reading two three - dimensional vectors and obtaining various
computations surrounding them.
Here is a sample run of the MP6 problem. Review the Mathematical Background for this
machine problem.
( Information for the Above Sample Run of MP6 )
Input Two Vectors
u  1i  2j  3k
or
u  ( 1 , 2 , 3 )
v  2i  3j  4k
or
v  ( 2 , 3 , 4 )
Compute Their Unit Vectors
First Compute Their Magnitudes ( Lengths )
|| u ||  [ ( 1 )
2
 ( 2 )
2
 ( 3 )
2
] 1/2
|| u ||  [ 1  4  9 ] 1 / 2
|| u ||  [ 14 ] 1 / 2
|| u ||  3.741657387
|| v ||  [ ( 2 )
2
 ( 3 )
2
 (4)
2
] 1/2
|| v ||  [ 4  9  16 ] 1 / 2
|| v ||  [ 29 ]
1/2
|| v ||  5.385164807
The Unit Vectors Are:
The unit vector having the same direction as a given ( nonzero ) vector u is defined by
u / || u || .
u / || u ||  ( 1 / 3.74 ,  2 / 3.74 ,  3 / 3.74 )
u / || u ||  ( 0.27 ,  0.53 ,  0.80 )
The unit vector having the same direction as a given ( nonzero ) vector v is defined by
v / || v || .
v / || v ||  ( 2 / 5.39 ,  3 / 5.39 , 4 / 5.39 )
v / || v ||  ( 0.37 ,  0.56 , 0.74 )
Compute Their Vector Sum
u  v  ( 1 , 2 , 3 )  ( 2 , 3 , 4 )
u  v  ( 1  2 , 2  3 , 3  4 )
u  v  ( 3 , 5 , 1 )
Compute Their Vector Difference
u  v  ( 1 , 2 , 3 )  ( 2 , 3 , 4 )
u  v  ( 1  2 , 2  3 , 3  4 )
u  v  ( 1 , 1 , 7 )
Compute Their Vector Cross Product
u  v  (u2 v3  u3 v2)i  (u3 v1  u1 v3)j  (u1 v2  u2 v1)k
u  v  [ ( 2 )(4)  ( 3 )( 3 )] i  [ ( 3 )(2)  (1 )(4 )] j  [ (1 )( 3 )  ( 2 )(2 )] k
u  v  [ ( 8 )  (9 )] i  [ ( 6 )  (4 )] j  [ ( 3 )  ( 4 )] k
u  v  [ (  17 ) ] i  [ (  10 ) ] j  [ ( 1 ) ] k
u  v   17 i   10 j  1 k
Compute the Angle Between the Vectors
First Compute the Vector Dot Product
u ∙ v  ( u1 , u2 , u3 ) ∙ ( v1 , v2 , v3 )
u ∙ v  ( u1 ) ∙ ( v1 )  ( u2 ) ∙ ( v2 )  ( u3 ) ∙ ( v3 )
u ∙ v  ( 1 , 2 , 3 ) ∙ ( 2 , 3 , 4 )
u ∙ v  ( 1 ) ∙ ( 2 )  ( 2 ) ∙ ( 3 )  ( 3 ) ∙ ( 4 )
u ∙ v  2  6  12
u ∙ v  4
Now Use the Cosine Version of the Dot Product
u ∙ v  || u || || v || cos 
( Vector Dot Product )
cos   ( u ∙ v ) / ( || u || || v || )
( Vector Dot Product )
  cos  1 [ ( u ∙ v ) / ( || u || || v || ) ]
( Arc Cosine )
  cos  1 [ (  4 ) / ( 3.74 ∙ 5.39 ) ]
( Arc Cosine )
  cos  1 [ (  4 ) / ( 20.1586 ) ]
  cos  1 [  0.198426478 ]
  1.8
( Radians )
Note: you can use the MS Excel ACOS() function to calculate the inverse cosine.
Download