In Physics we deal a lot with vector quantities. A vector is a measurement with a magnitude and a direction and drawn as an arrow. By direction we mean an angle. For example, 50 Newtons @ 23˚. Our direction will be what we call a "heading" which means we will measure clockwise from North. Therefore due North is 0˚, due East is 90˚, due South is 180˚ and due West is 270˚.
When we have multiple vectors all working on the same object we often want to determine what single vector applied to the object would give the same result. To determine this, we "add" all the component vectors together to give us what we call a "resultant" vector.
Let's do an example problem to review how to add vectors. What is the resultant vector that you would get by adding the following three vectors together?
Vector A is 100.0 units @ 63.0 deg S of W
Vector B is 200.0 units @ 40.0 deg S of E
Vector C is 150.0 units @ 20.0 deg N of E
In college you most likely will use linear algebra to solve this problem. To begin, we will draw all three of these vectors on a grid with the angles to each of them from North as seen below.
We then can use trig functions to "resolve" all three of them into their x and y components. A unit length in the x-direction is written as î and pronounced i-hat. A unit length in the y-direction is written as ĵ and pronounced j-hat. If you always use the angles from North then the following formulas will always work where θ is the angle from North and V is the magnitude of the vector.
î = Vsinθ
ĵ = Vcosθ
Anything to the right is +x and to the left is -x and anything up is +y and down is -y. If you convert the three vectors into i-hat and j-hat form then you should arrive at the following:
A = -45.4î - 89.1ĵ
B = 152î - 127ĵ
C = 141î + 51.3ĵ
To get your resultant (R) we simply add the above components of each vector, which gives:
R = 248î - 165ĵ
Since î and ĵ are the two perpendicular components, to determine the actual magnitude of the resultant you simply use Pythagorean's theorem as seen below.
So, how do we get our direction? We can now draw out our resultant î and ĵ components as seen below. To find the angle we would of course take the inverse tangent of î/ĵ. In the lower right and upper left quadrants this would mean that our angle is negative. Don't worry about this right now. You can just take the absolute value and call it 56.4˚. Of course, we want to always give the heading from north so we will subtract it from 180˚ for a final heading of 124˚.
If you are still a little fuzzy on the i-hat, j-hat method of vector addition perhaps you should watch this video:
Now that we hopefully have a better grasp of vector addition let's look at the criteria for this program.
INPUTS:
A user can enter up to 5 vectors
A magnitude and a direction in degrees from North will be entered for each vector.
OUTPUTS:
Graphical output of the vector components added head to tail.
Resultant vector drawn from the tail of the first to the head of the last.
Magnitude and direction in degrees from north of resultant vector should be printed to the tenths place.
CRITERIA & CONSTRAINTS:
Your program should have an impressive interface. (colors and graphics)
Specify in what units the user is entering data.
If the resultant less than 0.05, print "The resultant is zero." instead of the resultant and the angle.
(This program might be challenging. Feel free to ask your instructor questions as you are working through it.)
Create a function called vectors( )
Create a variable called sheet to get the active sheet.
Create a list variable called magnitudes and use the .getValues on the sheet to hold the 5 magnitudes the user enters.
Create a list variable called directions and use the .getValues on the sheet to hold the 5 angles from North the user enters.
Create two empty array variables called ihats and jhats.
Create five variable set to zero with the names: ihat_total, jhat_total, resultant, angle and anglefromN.
Create an empty string variable called statement.
We need to create a table for our graph and print it to our sheet way off screen to the right. Create a for loop with i=0 up to i<6 incrementing i by 1.
Push ihat_total to the ihats list.
setValue(ihats[i]) to getRange(i+1,23). This will put the first value of the ihats list (which is 0) into the 23rd column (W) and the 1st cell. Each time through the loop the next item in the list will drop down a cell in the 23rd column.
Calculate the next ihat (Vsinø) and add it to the ihat_total using the following line of code. If you don't understand what we are doing here ask your instructor.
ihat_total += magnitudes[i]*Math.sin(directions[i]*Math.PI/180);
Push jhat_total to the jhats list.
setValue(jhats[i]) to getRange(i+1,24). This will put the first value of the jhats list (which is 0) into the 24th column (X) and the 1st cell. Each time through the loop the next item in the list will drop down a cell in the 24th column.
Calculate the next jhat (Vcosø) and add it to the jhat_total using the following line of code. If you don't understand what we are doing here ask your instructor.
jhat_total += magnitudes[i]*Math.cos(directions[i]*Math.PI/180);
Close the for loop.
.setValue(0) to Y1.
.setValue(jhats[5]) to Y6.
Calculate the resultant using ihats[5] and jhats[5] and assign it to the resultant variable.
Calculate the angle by doing the inverse tangent of ihats[5] / jhats[5] and assign it to the angle variable.
To get the actual angle from North we will have to do a series of if - else if statements to see what quadrant the resultant is in.
The first condition is if ihats[5]>0 && jhats[5]<0. If this is true the angle will be negative and it is in the 4th quadrant. To get the angle from North we will add 180 to it and assign to the anglefromN variable.
The second condition is if ihats[5]<0 && jhats[5]<0. If this is true the angle will be positive and it is in the 3rd quadrant. To get the angle from North we will again add 180 to it and assign to the anglefromN variable.
The third condition is if ihats[5]<0 && jhats[5]>0. If this is true the angle will be negative and it is in the 2nd quadrant. To get the angle from North we will add 360 to it and assign to the anglefromN variable.
The final else means the angle is positive and in the first quadrant so we we will just assign angle to anglefromN
Make an if statement to see if the resultant<0.05. If so, assign the statement variable to "The resultant vector is zero." else make a statement including the resultant and anglefromN. Use .toFixed(1) to round each variable to the tenths place.
.setValue the statement to the spreadsheet.
On the spreadsheet embed a Line Graph. For the x-axis use W1:W6. For Series 1 use X1:X6 and for Series 2 use Y1:Y6. Series 2 is only two points and simply the resultant. To make Series 2 show up go under Customize-Chart Style-Plot Null Values.
To make your vectors look somewhat correct in relative lengths we will need to dynamically enter the same Max Value on the Horizontal and Vertical Axes. In our program we will make two variables called ihats_max and jhats_max
We will set them to the highest numbers in the ihats and jhats arrays by assigning them the following: =Math.max.apply(null, ihats); and =Math.max.apply(null, jhats);
We then will create a variable called: themax and set it =Math.max(ihats_max,jhats_max);
Next we can update the chart with code in the following way:
var chart = sheet.getCharts( )[0]; // [0] means the first chart on the page.
chart = chart.modify( )
.setOption('vAxes', {0: {viewWindow: {max: themax}}}) // sets max value of v axis
.setOption('hAxis.viewWindow.max',themax) // sets max value of h axis
.build( );
sheet.updateChart(chart);