Week 8 - papademas.net

advertisement
CSC 170 SESSION 8
Welcome Message
Welcome to Session 8 .
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://d2l.oakton.edu
What are the Deliverables for this week?
Exam 1 ( CSC 170 ) ( a sample is available on
http://www.papademas.net/occ/csc170 as sample test 1)
What are the Deliverables for next week?
Homework Assignment 3 ( CSC 170 ) CAS ( Mathematica ) ( Drop Box )
MP3 ( CSC 171 / 172 / 173 ) ( Numerical Integration )
List of Topics for this Week’s Lecture

Some Linux Commands

Using Mathematica

Web Page Design Hints and Tips

Using JavaScript

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 3 ( MP3 )
( Numerical Quadrature )
1
__________________________________________________________________
Some Linux Commands
Review of the UNIX File System
Directions: Circle the letter that indicates the best answer for the question.
(1)
The command to show your current directory pathname is __________ .
(a) pcd
(b) ls -a
(c) pwd
(d) lp pwd (e) ls pwd
(2)
(3)
(4)
The command to create a directory called ab is __________ .
(a) mkdir ab (b) mk ab dir (c) md ab
(d) dir ab
(e) MKDIR ab
If your current directory is /usr/david , the command cd / changes your current
directory to __________ .
(a)
root
(b)
usr
(c)
david
(d)
/usr
(e)
None of the above
Which one of the following pathnames is considered an absolute pathname?
(a)
absolute/xyz
(b)
/bin/usr/u/se
(c)
david/memo/c
(d)
./../xyz
(e)
../xyz/xxx/memo
(5)
True
The rmdir command ( with no options ) can only remove empty directories.
(6)
True
The / at the beginning of a pathname stands for the root directory.
(7)
True
The name .HIDDEN is an example of a hidden file.
(8)
False
To cancel a print job, you only have to turn the printer off.
(9)
True . ( dot ) and .. ( dot dot ) represent the current and a level higher directories
respectively.
Using Mathematica
For our CAS Homework Assignments, we can use either or both of Mathematica
( onsite ) or WolframAlpha ( online ) .
For Homework 2 hints, please visit the CAS2Shell Mathematica file located in the campus
Samples directory. To access the Samples directory remotely, log onto:
https://myfiles.oakton.edu
Then open the Course Resources L:\ drive, open the CSC folder and then the CSC 170 folder.
Here are some examples from Homework 2 .
(* these commands declare two functions *)
f[x_] = x^3 + x^2 + x + 1
g[x_] = x^5 + x^4 + x^3 + x^2 + x + 1
1+x+x2+x3
2
1+x+x2+x3+x4+x5
f[3]
(* this command evaluates a function *)
(* this command plots the function on a 2D grid *)
Plot[f[x], {x, -10, 10}, AspectRatio -> 1,
PlotRange -> {{-10, 10}, {-10, 10}}]
10
7.5
5
2.5
-10
-7.5
-5
-2.5
2.5
5
7.5
10
-2.5
-5
-7.5
-10
Web Page Design Hints and Tips
Review of HTML
The World Wide Web and Other Internet Topics
For each problem, select the correct answer.
(1)
The first portion of a URL identifies the communication __________ .
(a) prototype
(2)
(b) index
(c) protocol
(d) resource
(e) length
Which of the following is a legitimate prefix for a URL?
(a) http:\\www.
(b) http://www.
(c) http//:www.
(d) http\\:www.
3
(3)
A communication _________ is a set of rules that governs how information is exchanged.
(a) prototype
(4)
(5)
(b) index
(c) protocol
(d) resource
(e) length
Web pages use the communication protocol __________ .
(a)
HTTP (b)
HTML
(c)
URL
(d)
FTP
(e)
DSL
All Web page URLs begin with the letters " __________ ".
(a)
html
(b) http
(c)
url
(d)
www
(e)
w3c
Using JavaScript
(1)
The Preliminaries of JavaScript
Inside which HTML element do we put the JavaScript?
(a)
<js>
(b)
<javascript>
(c)
<script>
(d)
<scripting>
(2)
What is the correct JavaScript syntax to write " Casino Royale " ?
(a) document.write("Casino Royale"); (b) "Casino Royale"
(c) Response.write("Casino Royale")
(d) ("Casino Royale")
(3)
Where is the correct place to insert a JavaScript?
(a)
In the <head> section.
(b)
In the <body> section.
(c)
Both (a) and (b)
(d)
Neither (a) nor (b)
(e)
None of the above
What is the correct syntax for referring to an external script called " myFile.js " ?
(a)
<script src = "myFile.js">
(b)
<script name = "myFile.js">
(c)
<script href = "myFile.js">
(d)
<script type = "myFile.js">
(4)
(5)
(6)
(7)
(8)
(9)
An external JavaScript must contain the <script> tag.
(a)
True
(b) False
How do you write " Hello There " in an alert box?
(a)
msgBox("Hello There") (b)
alertBox("Hello There")
(c)
alert("Hello There") (d)
alertBox = "Hello There"
How do you create a function?
(a) function:myFunction()
(c) function myFunction()
(b) function = myFunction()
(d) myFunction() As function
How do you call a function named " myFunction " ?
(a)
(c)
call myFunction()
call function myFunction
5?
(a)
(c)
if i = = 5 then
if i = 5 then
(b)
(d)
myFunction()
function myFunction()
How do you write a conditional statement for executing some statements only if " i " is equal to
(b)
(d)
if (i = = 5)
if i = 5
4
(10)
How can you add a comment in a JavaScript?
(a)
//This is a comment
(b)
(c)
'This is a comment
(d)
<!-- This is a comment -->
"This is a comment"
Algorithm Design Hints and Tips
What is an Algorithm? Review Vector Products in Mathematics SP5 ( Special Project
5 ).
Consider the vectors u  u 1 i  u 2 j  u 3 k and v  v 1 i  v 2 j  v 3 k .
The scalar dot product between vectors u and v is defined as:
u  v  u1 v1  u2 v2  u3 v3
The vector cross product between vectors u and v is defined as:
u  v  (u2 v3  u3 v2)i  (u3 v1  u1 v3)j  (u1 v2  u2 v1)k
Writing Complete Computer Programs
( The shaded elements in a given row represent elements which have been swapped, i.e. exchanged. )
50
30
20
40
60
10
← unsorted array
30
50
20
40
60
10
first pass
30
20
50
40
60
10
first pass
30
20
40
50
60
10
first pass
30
20
40
50
60
10
first pass
30
20
40
50
10
60
first pass
20
30
40
50
10
60
second pass
20
30
40
50
10
60
second pass
20
30
40
50
10
60
second pass
20
30
40
10
50
60
second pass
20
30
40
10
50
60
second pass
20
30
40
10
50
60
third pass
20
30
40
10
50
60
third pass
20
30
10
40
50
60
third pass
20
30
10
40
50
60
third pass
5
20
30
10
40
50
60
third pass
20
30
10
40
50
60
fourth pass
20
10
30
40
50
60
fourth pass
20
10
30
40
50
60
fourth pass
20
10
30
40
50
60
fourth pass
20
10
30
40
50
60
fourth pass
Review of Binary Numbers
What are some uses for binary numbers?
Review of Hexadecimal Numbers
Hexadecimals are use in HTML Coloring.
Three ways to write colors into an HTML document.
-As a Named Color
-As a straight hexadecimal color
-As an RGB triplett
Using FTP
We can create a file in Notepad and ftp it to our Linux account for compiling purposes. In this
manner we can create a FORTRAN, Java or C + + file within Notepad and transfer it to our Linux
accounts. Here are some generic steps followed by screen snapshots.
6
Open Notepad
Type the Program Code
Save the File to your C:\ Drive
Open the Windows Command Prompt
Open an ftp Connection
ftp the File to your Linux Account
Open the Windows Command Prompt
Telnet into your Linux Server
Verify that the file was Transferred
Compile the File
Execute the File
A Preview of MP3 ( Numerical Quadrature )
We find an area under a curve for various physical reasons. To calculate a
probability or to calculate a consumer commodity value or reorder point.
How do we find an area for non – standard regions?
For standard regions:
Triangle
Rectangle
Square
Trapezoid
Circle
( 1 /2 ) b h
L*W
s*s
(1/2)h(b+B)
r2
One method to solve an area under curve problem is to divide the region in question
by a number of approximating rectangles.
To find the value of the area under the curve of the function below, we can use
Mathematica and integrate from x = 0 to x = 5 .
7
An arctangent is a function that returns an angle in radians for the sides for a
right triangle.
2
MP3 pertains to finding the area under the curve f ( x )  [ 1 / ( 1  x ) ] .
Our goal is to approximate the area using various algorithms including the Trapezoidal Rule and
Simpson’s Rule.
Can we use a Web page to approximate the area under the curve?
( Rectangular Approximation – Looping Techniques )
<html>
<head><title>Area Under the Curve</title></head>
<body>
<script>
//area under the curve of 1/(1+x^2)
var number = parseInt(prompt("how many rectangles",""));
8
var count = 0;
var width = parseFloat(prompt("what's my width?",""));
var area = 0;
var length = 0;
var a = 0; // left end point
var b = 0; // right end point
a = parseFloat(prompt("what's my left endpoint?",""));
b = parseFloat(prompt("what's my right endpoint?",""));
//enter a loop
for ( count = 0; count < number; count++)
{
length = 1/(1 + a * a);
area += width * length;
a += width;
alert("the area is " + area);
if ( a > b ) break;
}
//output statement
document.write("the area is " + area);
</script>
</body>
</html>
_____________________________________________________________________________
Trial run of the Above Program for …
a=0
b=5
deltaX = 0.5 ( step increment )
width = 0.5 ( fixed )
n = 4 rectangles
for n = 1 ( rectangle 1 )
length = 1 /( 1+0^2) = 1
Area = length * width = 1 * 0.5 = 0.50
for n = 2 ( rectangle 2 )
a => a + deltaX = 0 + 0.5 = 0.50
length = 1 /( 1+0.50^2) = 0.80
Area = length * width = 0.80 * 0.5 = 0.40
Accumulated Area =0.50 + 0.40 = 0.90
for n = 3 ( rectangle 3 )
a => a + deltaX = 0.50 + 0.50 = 1
length = 1 /( 1+1^2) = 0.50
Area = length * width = 0.50 * 0.50 = 0.25
Accumulated Area =0.90 + 0.25 = 1.15
9
for n = 4 ( rectangle 4 )
a => a + deltaX = 1 + 0.50 = 1.5
length = 1 /( 1+1.5^2) = 0.307692308
Area = length * width = 0.307692308 * 0.50 = 0.153846154
Accumulated Area =1.15 + 0.153846154 = 1.303846154
Modified Program – The Width is Automatically Computed
<html>
<head><title>Area Under the Curve</title></head>
<body>
<script>
//area under the curve of 1/(1+x^2)
var number = parseInt(prompt("how many rectangles",""));
var count = 0;
//var width = parseFloat(prompt("what's my width?",""));
var width = 0;
var area = 0;
var length = 0;
var a = 0; // left end point
var b = 0; // right end point
a = parseFloat(prompt("what's my left endpoint?",""));
b = parseFloat(prompt("what's my right endpoint?",""));
width = ( b - a ) / number;
//enter a loop
for ( count = 0; count < number; count++)
{
length = 1/(1 + a * a);
area += width * length;
a += width;
alert("the area is " + area);
if ( a > b ) break;
}
//output statement
document.write("the area is " + area);
</script>
</body>
</html>
10
To Verify an Answer on a Graphing Calculator
[Y=]
[Y1=1/(1+X^2)]
[ZOOM]->[ZStandard]
[2nd]->[Calculate]->[ Integrate  f(x) dx ]
Lower Limit ( LL ) = 0
Upper Limit ( UL ) = 5
1.3734007
What is our relative error?
Here is a sample run of the MP3 problem.
11
Two forms of Simpson’s Rule to compute the area under the curve of f ( x ) from x  a to
x  b:
Composite Simpson's rule
k  1
( h / 3 ) [ f (x 0 )  f (x n )  4  f (x 2i
k  1

i 0
1 )  2  f (x 2i ) ]
i 1
where h   x and n = 2 k
Approximate Simpson's rule
[ ( b  a ) / 6] [ f (a )  4 f ( [ ( a  b ) / 2] )  f (b )]
Here, let a = 1 and b = 4 and n = 10 as the number of panels.
Given f ( x )  [ 1 / ( 1  x 2 ) ] and a  1 , b  4 , the Approximate Simpson's Rule yields:
[ ( 4  1 ) / 6] [ f (1 )  4 f ([ ( 1  4 ) /2 ])  f (4 )] 
[ ( 3 ) / 6] [ f (1 )  4 f ( [ (5 ) / 2])  f (4) ] 
[ 0.50 ] [ f ( 1 )  4 f ( 2.5 )  f ( 4 ) ] 
2
2
2
[ 0.50 ] { [ 1 / ( 1  1 ) ]  4 [ 1 / ( 1  2.5 ) ]  [ 1 / ( 1  4 ) ] } 
[ 0.50 ] { [ 1 / ( 2 ) ]  4 [ 1 / ( 7.25 ) ]  [ 1 / ( 17 ) ] } 
[ 0.50 ] { 0.5  4 [ 0.137931 ]  [ 0.058824 ] } 
[ 0.50 ] { 0.5  0.551724  0.058824 } 
[ 0.50 ] { 1.110548 } 
[ 0.50 ] { 1.110548 } 
12
0.555274
Compare this to
0.540426 as given in the above screen snapshot from MP3 , which uses the Composite
Simpson’s Rule formula.
For the composite or 1 / 3 rule,
Composite Simpson's rule
k  1
( h / 3 ) [ f (x 0 )  f (x n )  4  f (x 2i
k  1
 1)
i 0
 2  f (x 2i ) ]
i 1
where h   x = ( b - a ) / n = ( 4 – 1 ) / 10 = 3 / 10 = 0.30
k  1
k  1
( 0.30 / 3 ) [ f ( 1 )  f ( 4 )  4  f ( x 2 i
 1)
i 0
 2  f (x 2i ) ]
i 1
here k = n / 2 or 10 / 2 = 5
5  1
( 0.30 / 3 ) [ f ( 1 )  f ( 4 )  4  f ( x 2 i
i 0
5 1
 1)
 2  f (x 2i ) ]
i 1
( 0.30 / 3 ) [ f ( 1 )  f ( 4 )  4 [ f ( x 2 ( 0 )  1 ) + f ( x 2 ( 1 )  1 ) + f ( x 2 ( 2 )  1 ) +
f (x 2( 3 )  1 ) + f (x 2( 4 )  1 ) ] + 2 [ f (x 2( 0 ) ) + f (x 2( 1 ) ) + f (x 2( 2 ) ) + f (x 2( 3 ) ) +
f (x 2(4 ) ) ] ]
( 0.30 / 3 ) [ f ( 1 )  f ( 4 )  4 [ f ( x 1 ) + f ( x 3 ) + f ( x 5 ) +
f (x 7 ) + f (x 9 ) ] + 2 [ f (x 0 ) + f (x 2 ) + f (x 4 ) + f (x 6 ) +
f (x 8 ) ] ]
13
Download