<< Chapter < Page Chapter >> Page >

Trigonometric solutions

I'm going to write a JavaScript program such that we can simply plug numbers into the values of variables in order to solvefor the sum and difference of two vectors. This will be easier and less error prone than doing lots of calculations using the Google calculator.

The code for the JavaScript program is shown in Listing 1 . There is nothing in Listing 1 that you haven't seen in earlier modules, so it shouldn't require an explanation.

Listing 1 . Add and subtract vectors using trigonometry.
<!------------ File JavaScript01.html ------------------><html><body><script language="JavaScript1.3">document.write("Start Script</br>"); //The purpose of this function is to receive the adjacent// and opposite side values for a right triangle and to // return the angle in degrees in the correct quadrant.function getAngle(x,y){ if((x == 0)&&(y == 0)){ //Angle is indeterminate. Just return zero.return 0; }else if((x == 0)&&(y>0)){ //Avoid divide by zero denominator.return 90; }else if((x == 0)&&(y<0)){ //Avoid divide by zero denominator.return -90; }else if((x<0)&&(y>= 0)){ //Correct to second quadrantreturn Math.atan(y/x)*180/Math.PI + 180; }else if((x<0)&&(y<= 0)){ //Correct to third quadrantreturn Math.atan(y/x)*180/Math.PI + 180; }else{//First and fourth quadrants. No correction required. return Math.atan(y/x)*180/Math.PI;}//end else }//end function getAngle//Modify these values and run for different cases. var Bm = 10;var Ba = 90; var Am = 10;var Aa = 0; //Do not modify any of the following code.//Convert angles to radians var bAng = Ba*Math.PI/180;var aAng = Aa*Math.PI/180; //Compute horizontal and vertical components// for each vector. var Bx = Bm*Math.cos(bAng);var By = Bm*Math.sin(bAng); var Ax = Am*Math.cos(aAng);var Ay = Am*Math.sin(aAng); //Compute sum of vectorsvar Cx = Bx + Ax; var Cy = By + Ay;var Ca = getAngle(Cx,Cy); var Cm = Math.sqrt(Cx*Cx + Cy*Cy);//Compute difference between vectors var Dx = Bx - Ax;var Dy = By - Ay; var Da = getAngle(Dx,Dy);var Dm = Math.sqrt(Dx*Dx + Dy*Dy); document.write("Bm = " + Bm.toFixed(2) + "</br>"); document.write("Ba = " + Ba.toFixed(2) + " deg</br>"); document.write("Am = " + Am.toFixed(2) + "</br>"); document.write("Aa = " + Aa.toFixed(2) + " deg</br>"); document.write("Bx = " + Bx.toFixed(2) + "</br>"); document.write("By = " + By.toFixed(2) + "</br>"); document.write("Ax = " + Ax.toFixed(2) + "</br>"); document.write("Ay = " + Ay.toFixed(2) + "</br>"); document.write("Cx = " + Cx.toFixed(2) + "</br>"); document.write("Cy = " + Cy.toFixed(2) + "</br>"); document.write("Ca = " + Ca.toFixed(2) + " deg</br>"); document.write("Cm = " + Cm.toFixed(2) + "</br>"); document.write("Da = " + Da.toFixed(2) + " deg</br>"); document.write("Dm = " + Dm.toFixed(2) + "</br>"); document.write("End Script");</script></body></html>

The one unique thing

The only thing that is unique about Listing 1 is that we can plug values into the variables named Bm, Ba, Am, and Aa to specify the magnitudes and angles for thevectors named B and A. (The same naming scheme is used for the variables as was described earlier.) Then when we open the script in ourbrowser, the sum and difference of the vectors B and A (plus some additional information) will be computed and displayed.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Accessible physics concepts for blind students. OpenStax CNX. Oct 02, 2015 Download for free at https://legacy.cnx.org/content/col11294/1.36
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Accessible physics concepts for blind students' conversation and receive update notifications?

Ask