CSC 170 Introduction to Numerical Methods MP6 Instructions / Hints

advertisement
CSC 170 Introduction to Numerical Methods
MP6 Instructions / Hints
Student Name ________________________________________ Section ___ Date _______
Machine Problem 6 ( MP6 ) Instructions / Hints
Part 1 Description
This machine problem basically performs the following tasks:
3
•
Two vectors v and w in R ( three - dimensional ) are input into the program. The
vectors have the Cartesian form v = ( v 1 , v 2 , v 3 ) and w = ( w 1 , w 2 , w 3 ) , i.e. for
each vector three elements are entered into the program.
•
The elements of the above vectors are input using looping structures.
•
The program outputs each of the following:
•
v + w
the vector sum
v − w
the vector difference
v · w
the vector dot product
v  w
the vector cross product
unit v
the unit vector for vector v
unit w
the unit vector for vector w
θ
the angle between the two vectors
Each of the above is computed as follows:
v + w = (v 1 + w 1, v 2 + w 2, v 3 + w 3)
the vector sum, output as a vector
v − w = (v 1 − w 1, v 2 − w 2, v 3 − w 3)
the vector difference, output as a vector
v · w= v1·w1 + v2·w2 + v3·w3
the vector dot product, output as a scalar
v · w = ( v 2 w 3 − v 3 w 2 , v 3 w 1 − v 1 w 3 , v 1 w 2 − v 2 w 1 ) the vector cross product,
output as a vector
|| v || = sqrt ( v 1
2
|| w || = sqrt ( w 1
+v2
2
2
+w2
2
+v3 )
the magnitude of v
2
the magnitude of w
2
+w3 )
unit v = ( v 1 , v 2 , v 3 ) / || v ||
the unit vector for vector v
unit w = ( w 1 , w 2 , w 3 ) / || w ||
the unit vector for vector w
θ = ArcCosine [ ( v · w ) / ( || v || || w || ) ]
the angle between the two vectors
© Copyright 2013 by P.E.P.
1
CSC 170 Introduction to Numerical Methods
MP6 Instructions / Hints
Student Name ________________________________________ Section ___ Date _______
Part 2 Navigation and File Construction
Here are some steps to complete MP6 :
(1)
Logon your Linux account using your Oakstar login name and your Bxxxxxxxx as your
password.
telnet csc.oakton.edu
(2)
Use the following command to travel to the mps directory that you created for MP0:
cd mps
(3)
Use the following command to create a new folder for MP 6 :
mkdir 6
(4)
Use the following command to travel to the 6 directory that you just created for M P6 :
cd 6
(5)
Use the following command to copy the existing skeletal code for MP 5 :
cp /samples/csc/171/mps/6/* .
CSC 171 command
cp /samples/csc/172/mps/6/* .
CSC 172 command
cp /samples/csc/173/mps/6/* .
CSC 173 command
(6) use the following command to open the MP 6 file using the pico text editor:
pico mp6.cpp
CSC 171 command
pico mp6.f90
CSC 172 command
pico mp6.java
CSC 173 command
(7)
Modify the initial MP 6 code within pico to complete the program.
(8)
Save your M P 6 file within pico by using this pico command: Ctrl + o
(9)
Exit pico by using this pico command: Ctrl + x
(10)
Compile your file with this command:
g++ mp6.cpp -o mp6
CSC 171 command
f90 mp6.f90 -o mp6
CSC 172 command
javac mp6.java
CSC 173 command
© Copyright 2013 by P.E.P.
2
CSC 170 Introduction to Numerical Methods
MP6 Instructions / Hints
Student Name ________________________________________ Section ___ Date _______
(11)
Execute ( run ) the program with this command:
mp6
CSC 171 command
mp6
CSC 172 command
java mp6
CSC 173 command
(12)
Enter some sample data values.
(13)
Observe the output of your program.
(14)
Submit the file with this command:
submit csc1710c1 6
CSC 171 command
submit csc1720c1 6
CSC 172 command
submit csc1730c1 6
CSC 173 command
Part 2 Starter Code
CSC 171 ( C ++ ) A Version of MP6
/************************************************************************
* Name: Your name
CSC 171
* Date: Today's date
MP 6
************************************************************************
* Statement: Produce vector sum, difference, unit vectors, cross product
*
and the angle between to R^3 vectors
* Specifications:
* Input - v and w, two vectors in R^3
* Output - v+w, v-w, vxw, unitv, unitw, theta between v and w
*************************************************************************/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//function declarations
void output(const char [], const double []);
void input(const char[], double[]);
double dot(const double[], const double[]);
int main()
{
// vector declarations
double v[3], w[3], vsum[3], vdiff[3], vcross[3], uv[3], uw[3];
// scalar declarations
double theta, nv, nw;
© Copyright 2013 by P.E.P.
3
CSC 170 Introduction to Numerical Methods
MP6 Instructions / Hints
Student Name ________________________________________ Section ___ Date _______
// 1) prompt and read vectors
input("v", v);
input("w", w);
// 2) determine mangnitude of vectors
nv = sqrt(dot(v, v));
nw = sqrt(dot(w, w));
// 3) loop over dimension of the space
for (int i = 0; i < 3; i++)
{
// 4) vector sum
vsum[i] = v[i] + w[i];
// 5) vector difference
vdiff[i] = v[i] - w[i];
// 6) unit vector v
uv[i] = v[i] / nv;
// 7) unit vector w
uw[i] = w[i] / nw;
}
// 8) vector cross product
vcross[0] = v[1]*w[2]-v[2]*w[1];
vcross[1] = v[2]*w[0]-v[0]*w[2];
vcross[2] = v[0]*w[1]-v[1]*w[0];
// 9) angle theta
theta = acos(dot(v,w)/(nv*nw));
//10) display vectors
//PLACE CODE HERE TO OUTPUT VECTOR w
//PLACE CODE HERE TO OUTPUT unit VECTOR uv
//PLACE CODE HERE TO OUTPUT unit VECTOR uw
//PLACE CODE HERE TO OUTPUT VECTOR SUM v + w
//PLACE CODE HERE TO OUTPUT VECTOR DIFFERENCE v - w
//PLACE CODE HERE TO OUTPUT VECTOR CROSS PRODUCT v x w
//11) display scalar theta
cout << "theta
= " << theta << endl;
}
/* Function dot
*
* receives - two R^3 vector reference parameters
* returns
- dot product
*************************************************************************/
// Define function dot below
double dot(const double x[], const double y[])
{ double d;
d = 0.0;
for(int i=0; i<3; i++)
d = d + x[i]*y[i];
© Copyright 2013 by P.E.P.
4
CSC 170 Introduction to Numerical Methods
MP6 Instructions / Hints
Student Name ________________________________________ Section ___ Date _______
return d;
}
/* function output
*
* receives - string description value parameter
*
- R^3 vector value parameter
* purpose
- displays description and formatted vector
*************************************************************************/
void output(const char s[], const double vec[])
{
// display vector and description
cout << s << " = ";
for (int i = 0; i < 3; i++)
cout << setiosflags(ios::fixed || ios::showpoint) << setw(8)
<< setprecision(2) << vec[i];
cout << endl;
}
/* Function input
*
* receives - string description value parameter
*
- R^3 vector reference parameter
* purpose
- prompts with string
*
- inputs vector
*************************************************************************/
void input(const char s[], double vec[])
{
// prompt
cout << "Enter the components for " << s << endl;
// read
for (int i =0; i < 3; i++)
cin >> vec[i];
}
CSC 172 ( FORTRAN ) A Version of MP6
!***********************************************************************
! Name: Your name
CSC 172
! Date: Today's date
MP 6
!***********************************************************************
! Statement: Produce vector sum, difference, unit vectors, cross product
!
and the angle between to R^3 vectors
! Specifications:
! Input - v and w, two vectors in R^3
! Output - v+w, v-w, vxw, unitv, unitw, theta between v and w
!***********************************************************************
program mp6
implicit none
© Copyright 2013 by P.E.P.
5
CSC 170 Introduction to Numerical Methods
MP6 Instructions / Hints
Student Name ________________________________________ Section ___ Date _______
! vector declarations
real, dimension(3)::v, w, vsum, vdiff, vcross, uv, uw
! scalar and function declarations
real :: theta, nv, nw
integer :: i
real, external :: dot
! 1) prompt and read vectors
call input("v", v)
call input("w", w)
! 2) determine mangnitude of vectors
nv = sqrt(dot(v, v))
nw = sqrt(dot(w, w))
! 3) loop over dimension of the space
do i = 1, 3
!
4) vector sum
vsum(i) = v(i)+w(i)
!
5) vector difference
vdiff(i) = v(i)-w(i)
!
6) unit vector v
uv(i) = v(i)/nv
!
7) unit vector w
uw(i) = w(i)/nw
enddo
! 8) vector cross product
vcross = (/ v(2)*w(3)-v(3)*w(2),
&
v(3)*w(1)-v(1)*w(3),
&
v(1)*w(2)-v(2)*w(1) /)
! 9) angle theta
theta = acos(dot(v,w)/(nv*nw))
!10) display vectors
!PLACE CODE HERE TO OUTPUT VECTOR v
!PLACE
!PLACE
!PLACE
!PLACE
!PLACE
!PLACE
CODE
CODE
CODE
CODE
CODE
CODE
HERE
HERE
HERE
HERE
HERE
HERE
TO
TO
TO
TO
TO
TO
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
OUTPUT
VECTOR w
unit VECTOR uv
unit VECTOR uw
VECTOR SUM v + w
VECTOR DIFFERENCE v - w
VECTOR CROSS PRODUCT v x w
!11) display scalar theta
write(*,3)"theta
= ", theta
3 format(a,2x,3f8.2)
end program mp6
! Function dot
!
© Copyright 2013 by P.E.P.
6
CSC 170 Introduction to Numerical Methods
MP6 Instructions / Hints
Student Name ________________________________________ Section ___ Date _______
! receives - two R^3 vector reference parameters
! returns
- dot product
!*************************************************************************
! Define function dot below
real function dot(x, y)
implicit none
real, dimension(*), intent(in)::x,y
integer :: i
real :: d
d = 0.0
do i=1,3
d = d + x(i)*y(i)
enddo
dot = d
end function dot
! Subroutine output
!
! receives - string description value parameter
!
- R^3 vector value parameter
! purpose
- displays description and formatted vector
!*************************************************************************
subroutine output(s, vec)
implicit none
real, dimension(1:3), intent(in) :: vec
character(*), intent(in) :: s
! display vector and description
write(*,3) s, vec
3 format(a,1x,"=",1x,3f8.2)
end subroutine output
! Subroutine input
!
! receives - string description value parameter
!
- R^3 vector reference parameter
! purpose
- prompts with string
!
- inputs vector
!*************************************************************************
subroutine input(s, vec)
implicit none
real, dimension(1:3), intent(out) :: vec
character(*), intent(in) :: s
! prompt
write(*,*) "Enter the 3 components for vector ", s
! read
read(*,*) vec
end subroutine input
© Copyright 2013 by P.E.P.
7
CSC 170 Introduction to Numerical Methods
MP6 Instructions / Hints
Student Name ________________________________________ Section ___ Date _______
CSC 173 ( Java ) A Version of MP6
/************************************************************************
* Name: Your name
CSC 173
* Date: Today's date
MP 6
************************************************************************
* Statement: Produce vector sum, difference, unit vectors, cross product
*
and the angle between to R^3 vectors
* Specifications:
* Input - v and w, two vectors in R^3
* Output - v+w, v-w, vxw, unitv, unitw, theta between v and w
*************************************************************************/
/* Sample output for v = <1, 2, 3> and w = <4, 5, 6>
uv = <0.27, 0.53, 0.8>, uw = <0.46, 0.57, 0.68>
v+w = <5, 7, 9>, v-w = <-3, -3, -3>, vxw = <-3, 6, -3>
theta
= 0.23
*/
import java.io.*;
public class mp6
{
public static void main(String[] args)
{
// vector declarations
double v[] = new double[3], w[] = new double[3], vsum[] = new double[3],
vdiff[] = new double[3], vcross[] = new double[3],
uv[] = new double[3], uw[] = new double[3];
// scalar and object declarations
double theta, nv, nw;
mp6 MP6 = new mp6();
// 1) prompt and read vectors
MP6.input("v", v);
MP6.input("w", w);
// 2) determine mangnitude of vectors
nv = Math.sqrt(MP6.dot(v, v));
nw = Math.sqrt(MP6.dot(w, w));
// 3) loop over dimension of the space
for (int i = 0; i < 3; i++)
{
// 4) vector sum
vsum[i] = v[i]+w[i];
// 5) vector difference
vdiff[i] = v[i]-w[i];
© Copyright 2013 by P.E.P.
8
CSC 170 Introduction to Numerical Methods
MP6 Instructions / Hints
Student Name ________________________________________ Section ___ Date _______
// 6)
uv[i]
// 7)
uw[i]
unit vector v
= v[i]/nv;
unit vector w
= w[i]/nw;
}
// 8) vector cross product
vcross[0] = v[1]*w[2]-v[2]*w[1];
vcross[1] = v[2]*w[0]-v[0]*w[2];
vcross[2] = v[0]*w[1]-v[1]*w[0];
// 9) angle theta
theta = Math.acos(MP6.dot(v,w)/(nv*nw));
// 10) display vectors
//PLACE CODE HERE TO OUTPUT
//PLACE CODE HERE TO OUTPUT
//PLACE CODE HERE TO OUTPUT
//PLACE CODE HERE TO OUTPUT
//PLACE CODE HERE TO OUTPUT
//PLACE CODE HERE TO OUTPUT
VECTOR w
unit VECTOR uv
unit VECTOR uw
VECTOR SUM v + w
VECTOR DIFFERENCE v - w
VECTOR CROSS PRODUCT v x w
// 11) display scalar theta
System.out.println("theta
= " + theta);
}// main ends here
/* Method dot
*
* receives - two R^3 vector reference parameters
* returns
- dot product
*************************************************************************/
// Define method dot below
public double dot(double x[], double y[])
{ double d;
d = 0.0;
for(int i=0; i<3; i++)
d = d + x[i]*y[i];
return d;
}// dot ends here
/* Method output
*
* receives - string description value parameter
*
- R^3 vector value parameter
* purpose
- displays description and formatted vector
*************************************************************************/
public void output(String s, double vec[])
{
© Copyright 2013 by P.E.P.
9
CSC 170 Introduction to Numerical Methods
MP6 Instructions / Hints
Student Name ________________________________________ Section ___ Date _______
// display vector and description
System.out.print( s + " = ");
for (int i=0; i < 3; i++)
System.out.print(vec[i] + " ");
System.out.println("\n");
}// output ends here
/* Method input
*
* receives - string description value parameter
*
- R^3 vector reference parameter
* purpose
- prompts with string
*
- inputs vector
*************************************************************************/
public void input(String s, double vec[])
{
// prompt
System.out.println("Enter the components for " + s);
try
{
// input objects
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
StreamTokenizer st = new StreamTokenizer(in);
// Customise the StreamTokenizer to treat all tokens as Strings.
st.resetSyntax();
st.wordChars('!','~');
st.whitespaceChars(0,' ');
// read tokens as String and convert to double
for (int i =0; i < 3; i++)
{
st.nextToken();
vec[i] = Double.parseDouble(st.sval);
}
}// try ends here
// catch IO exceptions
catch ( IOException e )
{
System.err.println("IO Exception");
}
catch ( NumberFormatException e )
{
System.err.println("Invalid format for double found.\n");
© Copyright 2013 by P.E.P.
10
CSC 170 Introduction to Numerical Methods
MP6 Instructions / Hints
Student Name ________________________________________ Section ___ Date _______
}
}// input ends here
}// mp6 ends here
Part 3 Sample Program Run
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 )
In essence a vector carries with it two pieces of information: who we
are and where are we going.
Input Two Vectors ( written in two different types of notation )
v = 1i − 2j − 3k
or
w = ( 1 , −2 , −3 )
v = 2i − 3j + 4k
or
w = ( 2 , −3 , 4 )
Compute Their Unit Vectors
First Compute Their Magnitudes ( Pythagorean Length )
© Copyright 2013 by P.E.P.
11
CSC 170 Introduction to Numerical Methods
MP6 Instructions / Hints
Student Name ________________________________________ Section ___ Date _______
|| v || = [ ( 1 )
2
+ (−2 )
2
+ ( −3 )
2
]
1/2
|| v || = [ 1 + 4 + 9 ] 1 / 2
|| v || = [ 14 ] 1 / 2
|| v || = 3.741657387 ( length of vector v )
|| w || = [ ( 2 )
2
+ (−3)
2
+ (4)
2
] 1/2
|| w || = [ 4 + 9 + 16 ] 1 / 2
|| w || = [ 29 ] 1 / 2
|| w || = 5.385164807 ( length of vector w )
The Unit Vectors Are:
Unit vectors tell us where we are going without affecting who you are:
The unit vector having the same direction as a given ( nonzero ) vector v is defined by
v / || v || .
v / || v || = ( 1 / 3.74 , − 2 / 3.74 , − 3 / 3.74 )
v / || v || = ( 0.27 , − 0.53 , − 0.80 )
The unit vector having the same direction as a given ( nonzero ) vector w is defined by
w / || w || .
w / || w ||  ( 2 / 5.39 , − 3 / 5.39 , 4 / 5.39 )
w / || w ||  ( 0.37 , − 0.56 , 0.74 )
Compute Their Vector Sum
We add like components.
v + w = ( 1 , −2 , −3 )  ( 2 , −3 , 4 )
v + w = ( 1  2 , −2 − 3 , −3  4 )
v + w = ( 3 , −5 , 1 )
© Copyright 2013 by P.E.P.
12
CSC 170 Introduction to Numerical Methods
MP6 Instructions / Hints
Student Name ________________________________________ Section ___ Date _______
Compute Their Vector Difference
v − w  ( 1 , −2 , −3 ) − ( 2 , −3 , 4 )
v − w  ( 1 − 2 , −2  3 , −3 − 4 )
v − w  ( −1 , 1 , −7 )
Compute Their Vector Cross Product
A vector cross product must yield a vector entity.
v  w = (v2 w3 − v3 w2)i + (v3 w1 − v1 w3)j + (v1 w2 − v2 w1)k
v  w = [ (− 2 )(4) − (− 3 )(− 3 )] i + [ (− 3 )(2) − (1 )(4 )] j +
[ (1 )(− 3 ) − (− 2 )(2 )] k
v  w = [ (− 8 ) − (9 )] i + [ (− 6 ) − (4 )] j + [ (− 3 ) − (− 4 )] k
v  w = [ ( − 17 ) ] i + [ ( − 10 ) ] j + [ ( 1 ) ] k
v  w = − 17 i − 10 j + 1 k
Compute the Angle Between the Vectors
First Compute the Vector Dot Product
A dot product yields a scalar entity, i.e. magnitude only like speed and
time.
v ∙ w  ( v1 , v2 , v3 ) · ( w1 , w2 , w3 )
v ∙ w  ( u1 ) · ( v1 ) + ( u2 ) · ( v2 ) + ( u3 ) · ( v3 )
v ∙ w  ( 1 , −2 , −3 ) ∙ ( 2 , −3 , 4 )
v ∙ w  ( 1 ) ∙ ( 2 ) + ( −2 ) ∙ ( −3 ) + ( −3 ) ∙ ( 4 )
v ∙ w  2 + 6 − 12
v ∙ w  −4
© Copyright 2013 by P.E.P.
13
CSC 170 Introduction to Numerical Methods
MP6 Instructions / Hints
Student Name ________________________________________ Section ___ Date _______
Now Use the Cosine Version of the Dot Product
v ∙ w = || v || || w || cos 
( Vector Dot Product )
cos  = ( v ∙ w ) / ( || v || || w || )
( Vector Dot Product )
 = cos − 1 [ ( v ∙ w ) / ( || v || || w || ) ] ( 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.
Note also that cos − 1 means the arc - cosine or inverse cosine and is not the
same as 1 / cos , which reciprocates the cosine ratio.
© Copyright 2013 by P.E.P.
14
Download